-
Notifications
You must be signed in to change notification settings - Fork 0
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
Comments
// 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
} |
为 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()
}
}
|
添加字体信息类 // 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 |
你好,android手机中会有PingFang字体? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
全局适配
The text was updated successfully, but these errors were encountered: