简体   繁体   English

React原生iOS中的垂直居中自定义字体

[英]Vertical center custom font in React native iOS

Using React Native with Expo.在 Expo 中使用 React Native。 Having difficulties centering custom imported font, at iOS.在 iOS 中难以将自定义导入的字体居中。 Android rendering with no issues, the text is vertically centered perfectly. Android 渲染没有问题,文本垂直居中完美。 Using iOS it is slightly upper than the center.使用 iOS 它比中心略高。

(Native Font centering well on both emulators - Android and iOS). (原生字体很好地集中在两个模拟器上 - Android 和 iOS)。

Any ideas how this could be solved?任何想法如何解决?

Code below:代码如下:

import React, { Component } from 'react';
import { StyleSheet, Text, View } from 'react-native';
import { Font } from 'expo';

export default class  extends Component {
  constructor(props) {
    super(props);
    this.state = {
      isReady: false
    };
  }

  async componentDidMount() {
    await Font.loadAsync({
      'KlavikaBold': require('./KlavikaBold.otf'),
    });
    this.setState({ isReady: true });
  }

  render() {
    if (!this.state.isReady) {
      return <Expo.AppLoading />;
      }
    return (
      <View style={styles.container}>
        <Text style={styles.content}>Home!</Text>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
  },
  content: {
    backgroundColor: 'green',
    color: 'white',
    padding: 10,
    textAlignVertical: 'center',
    fontFamily: 'KlavikaBold',
    fontSize: 20,
  }

})

在此处输入图片说明 在此处输入图片说明

Here's an easy solution that worked for me...这是一个对我有用的简单解决方案......

  1. Download Font Tools for Xcode下载 Xcode 字体工具
  2. In Terminal run $ ftxdumperfuser -t hhea -A d pathtoyourfont.ttf在终端运行 $ ftxdumperfuser -t hhea -A d pathtoyourfont.ttf
  3. Edit dumped pathtoyourfont.hhea.xml and set lineGap="0"编辑转储的 pathtoyourfont.hhea.xml 并设置 lineGap="0"
  4. If lineGap was 200 and ascender="800", set ascender to the sum ot these two 1000如果 lineGap 是 200 并且 Ascender="800",则将 Assender 设置为这两个 1000 的总和
  5. In Terminal run $ ftxdumperfuser -t hhea -A f pathtoyourfont.ttf在终端运行 $ ftxdumperfuser -t hhea -A f pathtoyourfont.ttf

Done.完毕。 Your new values are fused back the font file.您的新值融合回字体文件。

Repeat steps 4 and 5 until the rendering is OK.重复步骤 4 和 5,直到渲染正常。 Do not change other values.不要更改其他值。 Those should be OK.那些应该没问题。

Value that finally worked for me was ascender="1050".最终对我有用的值是 assender="1050"。 Try to change the sum until Android and iOS render the component the same height.尝试更改总和,直到 Android 和 iOS 将组件呈现相同的高度。

Source: https://medium.com/@martin_adamko/consistent-font-line-height-rendering-42068cc2957d来源: https : //medium.com/@martin_adamko/consistent-font-line-height-rendering-42068cc2957d

On iOS textAlignVertical: 'center' has no effect, but you can achieve a similar result when setting the lineHeight to the doubled height of the fontSize .在 iOS 上textAlignVertical: 'center'没有效果,但是当将lineHeight设置为fontSize的两倍高度时,您可以获得类似的结果。

We only need to apply the doubled lineHeight on iOS, therefore we import Platform我们只需要在 iOS 上应用加倍的 lineHeight,因此我们导入Platform

import { StyleSheet, Text, View, Platform } from 'react-native';   // add Platform at the beginning 

and then change the following:然后更改以下内容:

<Text style={[styles.content, {lineHeight: Platform.OS === 'ios' ? 40 : 20 }]}>Home!</Text>

you can make vertical center using following 2 style您可以使用以下两种样式制作垂直中心

...Platform.select({ios: {lineHeight: verticalScale(55)}, android: {textAlignVertical: 'center',}}),
    height: verticalScale(55),

here height and lineHeight must be same.这里 height 和 lineHeight 必须相同。

Same concept as detailed above but I found that using Glyphs was easy as well.与上面详述的概念相同,但我发现使用Glyphs也很容易。 Simply open the custom font you are working with and then once opened in Glyphs hit the "info" icon to bring you to a screen that allows you to adjust the ascender, descender, and lineGap properties.只需打开您正在使用的自定义字体,然后在 Glyphs 中打开后点击“信息”图标,即可将您带到一个屏幕,您可以在该屏幕上调整上升、下降和 lineGap 属性。 Export the new settings and keep adjusting until you get it to look as needed.导出新设置并不断调整,直到达到所需外观为止。

{{alignSelf:“中心”}}

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

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