如何设置反应原生的不同IOS设备的字体大小

Hus*_*aik 6 javascript reactjs react-native

在react-native我设计了一个样本,当我在不同的IOS设备中检查它时,这是我的代码:

render() {
      return (
        <View style={styles.container}>
             <View style={styles.body}>
             <TouchableHighlight style={styles.facebook} >
             <View style={styles.row}>
             <Image style={styles.icon} source={require('image!fb')}/>
             <Text style={styles.facebookText} >Continue with Facebook</Text>
             </View>
          </TouchableHighlight>
        </View>
        </View>
      )
  }
};
var styles = StyleSheet.create({
  container:{
    marginTop: 65,
    flexDirection:'column',
    flex:1,
    backgroundColor:'transparent'
  },
   body:{
    flex:.5
  },
  facebook:{
    marginTop: 25,
    height: 50,
    padding: 10,
    marginRight: 40,
    marginLeft: 40,
    backgroundColor: '#1A8A29',
  },
    row:{
    flexDirection:'row',
  },
  facebookText:{
    marginLeft: 8,
    fontSize: 20,
    color: 'white',
    alignSelf:'center'
  },
  icon:{
    marginTop: 3,
     marginLeft: 5,
      width: 25,
      height: 25
    }
})
Run Code Online (Sandbox Code Playgroud)

我检查iphone-6和5s字体问题即将到来时, 任何人帮我解决这个问题,如何设置不同IOS设备的字体大小在react-native任何帮助很多appriciated

Sud*_*Plz 11

我为我的项目编写了以下代码:

在文件上:multiResolution.js我有:

export function getCorrectFontSizeForScreen(PixelRatio, screenWidth, screenHeight, currentFont){
  let devRatio = PixelRatio.get();
  let factor = (((screenWidth*devRatio)/320)+((screenHeight*devRatio)/640))/2.0;
  let maxFontDifferFactor = 5; //the maximum pixels of font size we can go up or down
  // console.log("The factor is: "+factor);
  if(factor<=1){
    return currentFont-float2int(maxFontDifferFactor*0.3);
  }else if((factor>=1) && (factor<=1.6)){
    return currentFont-float2int(maxFontDifferFactor*0.1);
  }else if((factor>=1.6) && (factor<=2)){
    return currentFont;
  }else if((factor>=2) && (factor<=3)){
    return currentFont+float2int(maxFontDifferFactor*0.65);
  }else if (factor>=3){
    return currentFont+float2int(maxFontDifferFactor);
  }

}

function float2int (value) {
  return value | 0;
}
Run Code Online (Sandbox Code Playgroud)

然后在每个反应原生组件上我做:

import { PixelRatio } from 'react-native';
import {getCorrectFontSizeForScreen} from './multiResolution' 
import Dimensions from 'Dimensions';
const {height:h, width:w} = Dimensions.get('window'); // Screen dimensions in current orientation
Run Code Online (Sandbox Code Playgroud)

然后我的styles我做:

var portraitStyles = StyleSheet.create({
  titleText: {
    fontSize: getCorrectFontSizeForScreen(PixelRatio, w,h,27),//magic takes place here
  },
});
Run Code Online (Sandbox Code Playgroud)

这是习惯,但它对我来说非常好.

另外(与你的问题无关,但无论如何我都要说),我使用与screenWidth和screenHeight相关的宽度和高度,如下所示:

aStyleForAnElement:{    //this element will occupy
    width: w*0.55, //55% of the screen width
    height:h*0.05  //5% of the screen height
}
Run Code Online (Sandbox Code Playgroud)

这就是我在项目中支持不同屏幕尺寸的方法.

新答案:

随着时间的推移我们学习.在我写这篇文章时,我没有其他解决方案来管理字体大小而不是使用自定义代码,但是现在我不必做任何这样的事情:

我简单的告诉我们的设计师来设计可用小320x480的最低分辨率(和给我点而不是像素字体大小),然后我就用点来代替原来的像素,同时对设计分辨率320x480.

320x480以上的所有屏幕都将自动处理,并且本身是反应原生的,我根本不需要编写任何花哨的代码来自动管理它.

我的组件现在看起来像这样:

BUTTON_TEXT: {
  textAlign: 'center',
  fontSize: 16, // this is 16 points
  color: 'white',
  backgroundColor: 'transparent',
},
Run Code Online (Sandbox Code Playgroud)


zvo*_*ona 3

只是一个概念,但我会尝试以下操作:

const Dimensions = require('Dimensions');
const Viewport = Dimensions.get('window');

const IPHONE6_WIDTH = 750; // check this, since I'm not sure about iPhone 6 width

class YourComponent extends React.Component {
  constructor(props) {
    super(props);
  }

  getFontSize() {
    return ({
      fontSize: ((Viewport.width * Viewport.scale) === IPHONE6_WIDTH) ? 14 : 16
    });
  }

  render() {
    <View>
      <Text style={[styles.facebookText, this.getFontSize()]}>Continue with Facebook</Text>
    </View>
  }
}
Run Code Online (Sandbox Code Playgroud)