react-native-ui-lib
Version:
<p align="center"> <img src="https://user-images.githubusercontent.com/1780255/105469025-56759000-5ca0-11eb-993d-3568c1fd54f4.png" height="250px" style="display:block"/> </p> <p align="center">UI Toolset & Components Library for React Native</p> <p a
59 lines (48 loc) • 1.66 kB
JavaScript
import _ from 'lodash';
import Constants from "../commons/Constants";
import TypographyPresets from "./typographyPresets";
export class Typography {
keysPattern = this.generateKeysPattern();
/**
* Load custom set of typographies
* arguments:
* typographies - map of keys and typography values
* e.g {text15: {fontSize: 58, fontWeight: '100', lineHeight: Math.floor(58 * 1.4)}}
*/
loadTypographies(typographies) {
_.forEach(typographies, (value, key) => {
//@ts-ignore
this[key] = value;
});
this.keysPattern = this.generateKeysPattern();
}
getKeysPattern() {
return this.keysPattern;
}
generateKeysPattern() {
return new RegExp(_.chain(this).keys().map(key => [`${key}`]).flatten().join('|').value());
} // TODO: deprecate
async measureWidth(text, typography = TypographyPresets.text70, containerWidth = Constants.screenWidth) {
const size = await this.measureTextSize(text, typography, containerWidth);
if (size) {
return size.width;
}
}
async measureTextSize(text, typography = TypographyPresets.text70, containerWidth = Constants.screenWidth) {
const rnTextSize = require('react-native-text-size').default;
if (text) {
const size = await rnTextSize.measure({
text,
// text to measure, can include symbols
width: containerWidth,
// max-width of the "virtual" container
...typography // RN font specification
});
return size;
}
}
}
const TypedTypography = Typography;
const typography = new TypedTypography();
typography.loadTypographies(TypographyPresets);
export default typography;