[英]Custom Native React Native UI Component which implements text styles
I'm working on a custom native RN UI component in Android based on these docs: https://reactnative.dev/docs/native-components-android我正在根据这些文档在 Android 中开发自定义原生 RN UI 组件: https://reactnative.dev/docs/native-components-android
The component extends TextView
in Android and in order to expose some of that functionality in Javascript (eg text selection, simple HTML styling).该组件在
TextView
中扩展了 TextView 并为了在 Javascript 中公开一些功能(例如文本选择,简单的 Z4C4AD5FCA2E7A3FAA4ZDBB 样式)。
The issue I'm facing is that I can't seem to get text styling to work properly.我面临的问题是我似乎无法让文本样式正常工作。 Let's say my component is called
CustomTextView
假设我的组件名为
CustomTextView
In React Native, I would expect that I could do:在 React Native 中,我希望我可以做到:
<CustomTextView style={{color: 'red'}} text="blah"/>
This will display the text blah
in black, not red.这将以黑色而不是红色显示文本
blah
。 How can I inherit from React Native's text styles in Android?如何从 Android 中的 React Native 的文本 styles 继承? Other styling rules (eg
width
, height
) work properly.其他样式规则(例如
width
、 height
)可以正常工作。
Below is my implementation of CustomTextView
in Java下面是我在 Java 中实现
CustomTextView
CustomTextViewManager.java
public class CustomTextViewManager extends SimpleViewManager<TextView> {
public static final String REACT_CLASS = "RCTCustomTextView";
@Override
public String getName() { return REACT_CLASS; }
@Override
public TextView createViewInstance(ThemedReactContext context){
TextView view = new TextView(context);
view.setTextIsSelectable(true);
return view;
}
@ReactProp(name = "text")
public void setText(TextView view, String text) {
Spanned spanned;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
spanned = Html.fromHtml(text, Html.FROM_HTML_MODE_COMPACT);
} else {
spanned = Html.fromHtml(text);
}
view.setText(spanned);
}
}
Here is a link to a repo reproducing the issue: https://github.com/nsantacruz/CustomTextViewTest这是重现该问题的 repo 的链接: https://github.com/nsantacruz/CustomTextViewTest
I did not found any docs, but for me adding of flex: 1
to styles helped.我没有找到任何文档,但对我来说添加
flex: 1
到样式有帮助。
Resulting code should be like:结果代码应该是这样的:
<CustomTextView
style={{
color: 'red',
flex: 1,
}}
text="blah"
/>
create a react prop in your view manager在您的视图管理器中创建一个反应道具
@ReactProp(name = "textColor")
fun setTextColor(view: View, color: String) {
textView.setTextColor(Color.parseColor(color))
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.