简体   繁体   English

React Native Styled组件动态字体大小调整

[英]React Native Styled components dynamic font sizing

Let's say i have a basic, base level StyledText component; 假设我有一个基本的基础级StyledText组件;

import styled from 'styled-components/native'

const StyledText = styled.Text`
  font-size: 16px;
  color: pink;
`
export default StyledText

I then have an extension of that Text component that handles titles; 然后我有一个处理标题的Text组件的扩展;

import StyledText from '../StyledText'

const StyledTitle = StyledText.extend`
  color: black;
  font-weight: bold;
  font-size: 20px;
`

export default StyledTitle

Simple so far. 到目前为止简单。

What i need to do is dynamically increase the font size based on the size of the device. 我需要做的是根据设备的大小动态增加字体大小。 A simple function can handle this. 一个简单的函数可以处理这个。 But is there a way that the sizing function or util can only be called once from the main StyledText component, rather than repeated throughout the app with EVERY instance of StyledText extension? 但有没有一种方法可以只从主StyledText组件调用一次调整大小函数或util,而不是在每个应用程序中使用StyledText扩展的每个实例重复一次?

Just to clarify, there is not a problem surrounding the logic of increasing the size, the problem is importing and using the util for every single use of any extended Text component. 只是为了澄清,围绕增加大小的逻辑没有问题,问题是导入和使用util用于任何扩展Text组件的每次使用。

For example, the util might look like this; 例如,util可能看起来像这样;

// utils
export function fontSize(size) {
  // do some logic here to increase the size, or whatever...
  return `
    font-size: ${size}px;
  `
}

Then used like this; 然后像这样使用;

import styled from 'styled-components/native'
import { fontSize } from 'utils/StyledUtils'

const StyledText = styled.Text`
  ${fontSize(16)}
  color: pink;
`
export default StyledText

The problem is that the util file has to be imported and referenced all the way through the app. 问题是必须在应用程序中一直导入和引用util文件。

I had similar problem a while ago, and I solved it by creating an universal function that takes size as argument and returns new size that fits on different screen resolution. 我前一段时间遇到过类似的问题,我通过创建一个通用函数来解决它,该函数将大小作为参数并返回适合不同屏幕分辨率的新大小。 You can use this function for every size in your app. 您可以对应用中的每个尺寸使用此功能。

Here is the function inside file responsive.js: 这里是文件responsive.js里面的函数:

import { Dimensions } from 'react-native'

const deviceHeight = Dimensions.get('window').height;
const deviceWidth = Dimensions.get('window').height;

export const ResponsiveSize = (size) => {
    if(deviceHeight===568) { return size }
    else if(deviceHeight===667) size*1.17 
    else if(deviceHeight===736) size*1.29 
    else if(deviceHeight===1024) size*1.8 
}

Notice that size is multiplied by some fixed numbers (I did it for iOS devices) that you can modify to fit your needs. 请注意,大小乘以一些固定数字(我为iOS设备做过),您可以修改以满足您的需求。

Then you can style your text as following: 然后,您可以按如下方式设置文本样式:

import styled from 'styled-components/native'
import { ResponsiveSize } from './responsive'

const StyledText = styled.Text`
  font-size: ResponsiveSize(16);
  color: pink;
`
export default StyledText

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM