【问题标题】:how to set font size for different IOS devices in react native如何在本机反应中为不同的IOS设备设置字体大小
【发布时间】:2015-10-05 11:03:58
【问题描述】:

在 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
    }
})

当我签入 iphone-6 和 5s 字体问题时 任何人都可以帮助我解决这个问题,如何在 react-native 中为不同的 IOS 设备设置字体大小,非常感谢

【问题讨论】:

    标签: javascript reactjs react-native


    【解决方案1】:

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

    存档: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;
    }
    

    然后在我做的每个 react-native 组件上:

    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
    

    然后在我的styles 我做:

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

    这是自定义的,但对我来说效果很好。

    另外(与您的问题无关,但我还是要说),我使用与 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
    }
    

    到目前为止,这就是我在项目中支持不同屏幕尺寸的方式。

    新答案:

    随着时间的推移,我们会学习。 在我写这篇文章的时候,除了使用自定义代码之外,我没有其他解决方案来管理字体大小,但现在我不必这样做:

    我只是告诉我们的设计师针对可用的最低分辨率 320x480 进行设计(并以点而不是像素为单位提供字体大小),然后在设计 320x480 时我只使用点而不是像素。

    所有 320x480 以上的屏幕都将由 react-native 自动处理,我根本不需要编写任何花哨的代码来自动管理它。

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

    BUTTON_TEXT: {
      textAlign: 'center',
      fontSize: 16, // this is 16 points
      color: 'white',
      backgroundColor: 'transparent',
    },
    

    【讨论】:

    • 我要把这段代码放到node包里。太好了,谢谢!
    • 老兄,太棒了。感谢您的解决方案
    • 我仍在使用旧的解决方案。如果您为 5 设计,则在较大的所有内容上,文字都太小了
    • 我现在正在为 5 设计,文字对我来说很好..! RN 0.39.2
    【解决方案2】:

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

    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>
      }
    }
    

    【讨论】:

    • iPhone 6/s plus (宽度: 414 scale 3 相乘: 1242) -- iPhone 6/s (width: 375 scale 2 相乘: 750) -- iphone 5/s (width : 320 scale 2 相乘: 640) -- iphone 4s (宽度: 320 scale 2 相乘: 640
    • 你也可以使用来自npmjs.com/package/react-native-device-info的DeviceInfo.getDeviceId()。
    【解决方案3】:

    为应用程序创建通用样式。在所有样式组件中重用此默认样式属性。

    src/common/style.js

    import { Dimensions } from 'react-native';
    const { width, height } = Dimensions.get('window');
    
    export const ColorPrimary  = '#5CFE48';
    export const SmallPhone = () => {
      if(width<=320) // Here you could use "react-native-device-info" to get device information.
         return true
      else 
         return false
    }
    

    在组件样式中导入通用样式文件。

    import { SmallPhone, ColorPrimary } from '../../common/style';
    ... 
    ...
    const styles =  StyleSheet.create({
      headerLabel: {
        fontSize: SmallPhone() ? 14:26,
        color: ColorPrimary
      }
    });
    

    您也可以在 common style.js 中直接为每个设备指定特定的字体大小,只需调用 SmallPhone 函数而不是使用三元运算符。

    【讨论】:

    • 干净整洁。非常感谢可重复使用的样式。
    猜你喜欢
    • 2017-09-15
    • 2018-08-24
    • 2015-03-20
    • 2017-08-24
    • 2020-03-01
    • 2016-09-19
    • 1970-01-01
    • 2016-04-15
    • 2013-11-17
    相关资源
    最近更新 更多