Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

react native 适配(1)字体 #33

Open
gaowei1012 opened this issue Sep 4, 2020 · 4 comments
Open

react native 适配(1)字体 #33

gaowei1012 opened this issue Sep 4, 2020 · 4 comments

Comments

@gaowei1012
Copy link
Owner

gaowei1012 commented Sep 4, 2020

全局适配

// index.js
const TextRender = Text.render
Text.render = function (...args) {
    const originText = TextRender.apply(this, args)
    const { style } = originText.props
    return React.cloneElement(originText, {
        allowFontScaling: Platform.OS === 'ios' ? false : true, // 设置字体随着屏幕分辨率变化拉伸变化
        style: [{
            ...PlatformStyle({fontFamily: 'PingFangRegular'}),
        }, style]
    })
}
@gaowei1012
Copy link
Owner Author

// PlatformStyle.js

import { Platform } from 'react-native'

/**
 * 根据平台设置字体样式
 * @param {Object} androidStyle
 * @param {Object} iosStyle 
 */
export function PlatformStyle(androidStyle = {}, iosStyle = {}) {
    if (Platform.OS === 'ios') {
        return iosStyle
    }
    return androidStyle
}

@gaowei1012
Copy link
Owner Author

android 处理 Text 字体样式问题
封装 Android Text 组件

import React from 'react'
import { Text, Platform, StyleSheet } from 'react-native'


/**
 * 封装对 android Text 组件的支持
 */
export default class TextWidget extends React.Component {

    renderAndroidText() {
        const { style, children } = this.props
        let fontStyle = null

        if (style) {
            if (style instanceof Array) {
                style = StyleSheet.flatten(style)
            }
            fontStyle = style.fontWeight ? {
                fontWeight: 'normal',
                fontFamily: 'PingFangBold',
            } : {
                    fontFamily: 'PingFangRegular'
                }
        }

        return (
            <Text {...this.props}
                style={[style, fontStyle]}
            >
                {children}
            </Text>
        )

    }

    render() {
        return Platform.OS === 'ios' ? <Text {...this.props} /> : this.renderAndroidText()
    }
}

@gaowei1012
Copy link
Owner Author

gaowei1012 commented Sep 4, 2020

添加字体信息类

// getFontFamily.js

const fonts = {
    SonglcyFont: {
        fontWeights: {
            300: "Light",
            400: "Regular",
            700: "Bold",
            900: "Black",
            normal: "Regular",
            bold: "Bold"
        },
        fontStyles: {
            normal: "",
            italic: "Italic"
        }
    }
}

/**
 * 返回不同的字体
 * @param {*} baseFontFamily 
 * @param {*} styles 
 */
const getFontFamily = (baseFontFamily, styles = {}) => {
    const { fontWeight, fontStyle } = styles
    const font = fonts[baseFontFamily]
    const weight = fontWeight ? font.fontWeights[fontWeight] : font.fontWeights.normal
    const style = fontStyle ? font.fontStyles[fontStyle] : font.fontStyles.normal

    if (style === font.fontStyles.italic && weight === font.fontWeights.normal) {
        return `${baseFontFamily}-${style}`
    }

    return `${baseFontFamily}-${weight}${style}`
}

export default getFontFamily

@beilunyang
Copy link

你好,android手机中会有PingFang字体?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants