React Native中的自定义TextView提供了不变的违规:文本字符串必须在<text>组件内呈现

时间:2018-11-13 13:25:25

标签: android react-native

我要创建带有轮廓文字的标签。

我的方法如下:

class NativeOutlinedTextViewManager: ReactTextViewManager() {

    override fun getName(): String {
        return "NativeOutlinedTextView"
    }

    override fun createViewInstance(context: ThemedReactContext): ReactTextView {
        return NativeOutlinedReactTextView(context)
    }
}

class NativeOutlinedReactTextView(context: Context): ReactTextView(context) {
    override fun setText(update: ReactTextUpdate) {
        update.text.setSpan(OutlineSpan(), 0, update.text.length - 1, 0)
        super.setText(update)
    }
}

class OutlineSpan : CharacterStyle() {
    override fun updateDrawState(paint: TextPaint?) {
        paint?.style = Paint.Style.STROKE
        paint?.strokeJoin = Paint.Join.ROUND
        paint?.strokeMiter = 10f
        paint?.strokeWidth = 1f
    }
}

当我尝试在JSX中使用此组件时,应用程序崩溃并显示给定错误:

Invariant Violation: Text strings must be rendered within a <Text> component

0 个答案:

没有答案