是否有其他替代方法可以为Android调整AdjustsFontSizeToFit? [英] Are there any alternatives to adjustsFontSizeToFit for Android?

查看:84
本文介绍了是否有其他替代方法可以为Android调整AdjustsFontSizeToFit?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在文本字段中使用flex来显示我的值/范围.对于IOS,我正在使用Text的属性AdjustsFontSizeToFit和minimumFontScale道具来实现理想的字体大小和缩放比例.我希望为Android启用相同的缩放比例.有人在Android上使用任何技巧吗?

I am using flex in Text field to display my values/range. For IOS I am using the properties adjustsFontSizeToFit and minimumFontScale props for Text to achieve the ideal font size and scaling. I want the same scaling to be enabled for Android. Does anyone use any tricks for Android?

<View style={{flex: 40}}>
  {
   (Platform.OS === 'ios') ?
    <Text style={{flex: 1, paddingRight: 2}} adjustsFontSizeToFit 
    minimumFontScale={0.5}>
     //if decimal value exists show range
     (pointFivePart_1 === '5') ? 
        <Text style={{ flexDirection: 'row' }} >
          <Text style={styles.mainValueStyle}>{first_1}</Text>
          <Text style=styles.decimalValueStyle}> .pointFivePart_1}</Text>
          <Text style={styles.mainValueStyle}>&deg;</Text>
         </Text>
         : <Text style={styles.mainValueStyle} >{min}&deg;</Text>
        }
        //if two values then show the second value as range
{// render largest value if different than min
            (maxSet === minSet) ? null : (
                (pointFivePart_2 === '5') ?
                <Text style={{ flexDirection: 'row' }} >
                    <Text style={styles.mainValueStyle}> - {first_2}</Text>
                    <Text style={styles.decimalValueStyle}>.{pointFivePart_2}</Text>
                    <Text style={styles.mainValueStyle}>&deg;</Text>
                    </Text>
                :
                    <Text style={styles.mainValueStyle} > - {maxSet}&deg;</Text>
                    )
                }
                </Text>
                :
                //same styling for android

推荐答案

简单但以某种方式有效的解决方案(跨平台):

Simple but somehow working solution (crossplatform):

fontSize = Math.sqrt(textWidth*textHeight/text.length)

其中textWidth,textHeight-您的Text组件的大小,text.length-您的文本的长度.

where textWidth, textHeight - size of your Text component, text.length - length of your text.

minimumFontScale可以通过

minimumFontScale can be achieved with

fontSize = Math.max(fontSize,minimumFontSize)

我通过求解方程组来得出这个公式:

I came to this formula by solving such system of equations:

lettersInLine = textWidth/fontSize
lines = textLength/lettersInLine
lines*fontSize = textHeight

在此假定字体具有方形大小(宽度等于高度)的字母.

assuming here that font has square sized (width equal to height) letters.

即使没有方程式,该公式也很有意义.你只是

Though this formula makes sense even without equations. You just

  1. 计算整个文本元素的平方
  2. 计算每个字母的平方(将整个平方除以字母数字)
  3. 从字母平方中取平方根以获取字体大小

这篇关于是否有其他替代方法可以为Android调整AdjustsFontSizeToFit?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆