Skip to content

[android][TextInput] Placeholder is not respecting custom font family and weight #50137

Open
@lorenc-tomasz

Description

@lorenc-tomasz

Description

On some Android devices user can change default font to e.g. Cool jazz.

In the app we don't want to allow user to change font family, so we apply fontFamily and fontWeight.

Example:

           <TextInput style={{
                height: 60,
                borderColor: 'gray',
                borderWidth: 1,
                marginBottom: 20,
                fontFamily: 'Roboto',
                fontWeight: '400',
                fontSize: 20,
            }} placeholder={"Search..."} />

We are expecting the placeholder will be using the same fontFamilyas set in styles. However it's not a case. Placeholder will use device's default font instead.

To workaround this I manage to find this solution:

import React, { forwardRef, useRef, useCallback } from 'react';
import {Platform, StyleSheet, TextInput, TextInputProps} from 'react-native';

const CustomTextInput = forwardRef<TextInput, TextInputProps>((props, ref) => {
  const inputRef = useRef<TextInput | null>(null);

  const setRef = useCallback(
    (node: TextInput | null) => {
      if (node) {
        inputRef.current = node;

        if (typeof ref === 'function') {
          ref(node);
        } else if (ref) {
          const mutableRef = ref as React.MutableRefObject<TextInput | null>;
          mutableRef.current = node;
        }

        // Workaround for Android to apply style before setting placeholder
        // to apply font family correctly
        if (Platform.OS === 'android') {
          node.setNativeProps({
            style: StyleSheet.flatten([{
                fontFamily: 'Roboto',
                fontWeight: '400',
            }, props.style ?? {}]),
          });
        }
      }
    },
    [ref],
  );

  return <TextInput ref={setRef} {...props} />;
});

export default CustomTextInput;

so the fontFamily is applied correctly.

        <View
          style={{
            backgroundColor: isDarkMode ? Colors.black : Colors.white,
            paddingHorizontal: safePadding,
            paddingTop: safePadding,
            paddingBottom: safePadding,
          }}>
            <TextInput style={{
                height: 60,
                borderColor: 'gray',
                borderWidth: 1,
                marginBottom: 20,
                fontFamily: 'Roboto',
                fontWeight: '400',
                fontSize: 20,
            }} placeholder={"Search..."} />
        </View>
          <View
              style={{
                  backgroundColor: isDarkMode ? Colors.black : Colors.white,
                  paddingHorizontal: safePadding,
                  paddingTop: safePadding,
                  paddingBottom: safePadding,
              }}>
              <CustomTextInput style={{
                  height: 60,
                  borderColor: 'gray',
                  borderWidth: 1,
                  marginBottom: 20,
                  fontFamily: 'Roboto',
                  fontWeight: '400',
                  fontSize: 20,
              }} placeholder={"Custom search..."} />
          </View>

As you can see the first text input has wrong fontFamily and the second one uses the correct one.

However expected behaviour is that the custom font will be applied based on style.

All React-Native versions are affected.

Steps to reproduce

Set custom font into TextInput and set placeholder.
Change Font Style on device.
You will see that placeholder is not respecting the custom font.

React Native Version

0.78.1

Affected Platforms

Runtime - Android

Output of npx @react-native-community/cli info

System:
  OS: macOS 15.3.2
  CPU: (12) arm64 Apple M2 Pro
  Memory: 218.94 MB / 32.00 GB
  Shell:
    version: "5.9"
    path: /bin/zsh
Binaries:
  Node:
    version: 22.13.1
    path: /usr/local/bin/node
  Yarn:
    version: 1.22.22
    path: /usr/local/bin/yarn
  npm:
    version: 10.9.2
    path: /usr/local/bin/npm
  Watchman:
    version: 2025.02.17.00
    path: /opt/homebrew/bin/watchman
Managers:
  CocoaPods:
    version: 1.16.2
    path: /Users/tomaszlorenc/.rbenv/shims/pod
SDKs:
  iOS SDK:
    Platforms:
      - DriverKit 24.2
      - iOS 18.2
      - macOS 15.2
      - tvOS 18.2
      - visionOS 2.2
      - watchOS 11.2
  Android SDK:
    API Levels:
      - "28"
      - "31"
      - "34"
      - "35"
    Build Tools:
      - 30.0.3
      - 34.0.0
      - 35.0.0
      - 35.0.1
    System Images:
      - android-26 | Google APIs ARM 64 v8a
      - android-28 | Google ARM64-V8a Play ARM 64 v8a
      - android-34 | Google APIs ARM 64 v8a
      - android-34 | Google Play ARM 64 v8a
      - android-35 | Google APIs ARM 64 v8a
      - android-35 | Google Play ARM 64 v8a
    Android NDK: Not Found
IDEs:
  Android Studio: 2024.3 AI-243.22562.218.2431.13114758
  Xcode:
    version: 16.2/16C5032a
    path: /usr/bin/xcodebuild
Languages:
  Java:
    version: 17.0.14
    path: /usr/bin/javac
  Ruby:
    version: 3.3.0
    path: /Users/tomaszlorenc/.rbenv/shims/ruby
npmPackages:
  "@react-native-community/cli":
    installed: 15.0.1
    wanted: 15.0.1
  react:
    installed: 19.0.0
    wanted: 19.0.0
  react-native:
    installed: 0.78.1
    wanted: 0.78.1
  react-native-macos: Not Found
npmGlobalPackages:
  "*react-native*": Not Found
Android:
  hermesEnabled: true
  newArchEnabled: true
iOS:
  hermesEnabled: Not found
  newArchEnabled: false

Stacktrace or Logs

No logs.

Reproducer

https://github.com/lorenc-tomasz/react-native-text-input-placeholder-font-issue-reproducer

Screenshots and Videos

No response

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions