weex-nuke
Version:
基于 Rax 、Weex 的高性能组件体系 ~~
76 lines (68 loc) • 2.43 kB
JSX
/** @jsx createElement */
;
import { Component, createElement, PropTypes } from 'rax';
import { calcTextSize } from 'nuke-helper';
import RaxText from './text';
const defaultLineHeightOptimizer = fontSize =>
Math.ceil(9.24 * Math.pow(10, -5) * Math.pow(fontSize, 2) + 1.492 * fontSize + 2.174);
const isWeb = typeof callNative !== 'function';
class Text extends Component {
constructor(props, context) {
super();
this.fixedFont = context.commonConfigs && context.commonConfigs.fixedFont;
this.fontFamily = context.commonConfigs && context.commonConfigs.fontFamily;
this.optimizeLineHeight = context.commonConfigs && context.commonConfigs.optimizeLineHeight;
if ('fixedFont' in props) {
this.fixedFont = props.fixedFont;
}
if ('optimizeLineHeight' in props) {
this.optimizeLineHeight = props.optimizeLineHeight;
}
}
/**
* This function is used to calculate line height when language is Thai or Vietnanese.
* @param {obj} style
*/
calcLineHeight(style) {
const { lineHeightOptimizer } = this.props;
const fontSize = parseInt(style.fontSize, 10);
const lineHeight = lineHeightOptimizer(fontSize);
if (lineHeight && !style.lineHeight) {
if (this.fixedFont && typeof lineHeight === 'number') {
style.lineHeight = lineHeight + (isWeb ? 'px' : 'wx');
} else {
style.lineHeight = lineHeight;
}
}
return style;
}
render() {
const { onPress, onClick, style, ...others } = this.props;
const defaultStyle = { fontSize: '32rem', wordWrap: 'break-word' };
if (this.fontFamily) {
defaultStyle.fontFamily = this.fontFamily;
}
let textStyle = Object.assign(defaultStyle, style);
delete others.optimizeLineHeight;
textStyle = this.fixedFont ? calcTextSize(textStyle) : textStyle;
textStyle = this.optimizeLineHeight ? this.calcLineHeight(textStyle) : textStyle;
return <RaxText {...others} style={textStyle} onClick={onPress || onClick} />;
}
}
Text.contextTypes = {
androidConfigs: PropTypes.any,
commonConfigs: PropTypes.any,
};
Text.propTypes = {
optimizeLineHeight: PropTypes.boolean,
fixedFont: PropTypes.boolean,
style: PropTypes.any,
onPress: PropTypes.func,
onClick: PropTypes.func,
lineHeightOptimizer: PropTypes.func,
};
Text.defaultProps = {
style: {},
lineHeightOptimizer: defaultLineHeightOptimizer,
};
export default Text;