簡體   English   中英

React Native 0.18.1在所有視圖上使用自定義字體

[英]React Native 0.18.1 use custom font on all views

我剛剛升級到RN 0.18.1,我在我的應用程序中使用自定義字體的方式不再適用。 有沒有解決方法?

我曾經這樣做過:

import React, {AppRegistry, Component, StyleSheet, Text, View, TextInput} from 'react-native';

var OldText = Text;

class NewText extends OldText {
  defaultProps = {
    customFont: false
  };

  render() {
    var props = _.clone(this.props);

    if (this.props.customFont) {
      return super.render();
    }

    if (_.isArray(this.props.style)){
      props.style.push({fontFamily: 'Quicksand-Regular'});
    } else if (props.style) {
      props.style = [props.style, {fontFamily: 'Quicksand-Regular'}];
    } else {
      props.style = {fontFamily: 'Quicksand-Regular'};
    }

    this.props = props;

    return super.render();
  }
};

React.Text = NewText;

現在在最后一行React.Text = NewText ,我收到錯誤:“試圖分配給readonly屬性”。 現在有關如何使用自定義字體的任何想法?

感謝@Ben Clayton的回答。 現在正確的方法是創建一個新組件(NewText),它是React.Text的子類,並使用它代替整個應用程序。

我的NewText看起來像這樣:

'use strict';

import React, {Text} from 'react-native';

import _ from 'lodash';

class NewText extends Text {
  defaultProps = {
    customFont: false
  };

  render() {
    var props = _.clone(this.props);

    if (this.props.customFont) {
      return super.render();
    }

    if (_.isArray(this.props.style)){
      props.style.push({fontFamily: 'Quicksand-Regular'});
    } else if (props.style) {
      props.style = [props.style, {fontFamily: 'Quicksand-Regular'}];
    } else {
      props.style = {fontFamily: 'Quicksand-Regular'};
    }

    this.props = props;

    return super.render();
  }
};

export default NewText;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM