[android][TextInput] Placeholder is not respecting custom font family and weight
@narekmalk 已经在做这个了。
开始于 2025年3月20日。
- 主要语言
- C++
- 星标
- 127k
- 派生
- 25.3k
- 平均合并
- 1 天 23 小时
- 30 天内合并 PR
- 4
描述
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
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
评估
这个 Issue 还没有评估数据。