关于ios:如何为UILabel设置”粗体”动态字体类型? | 珊瑚贝

How to set a “bold” dynamic font type for UILabel?


Apple 文档说,对于 iOS,应用程序可以使用文本样式和动态字体类型,因此文本大小会根据用户的阅读偏好自动调整。

我有一个文本样式 Body 并将 Dynamic Type 设置为 true 以便它自动调整文本大小。这很好用。

我也想让这个文本出现bold。我还没有找到如何通过代码或在 Interface Builder 中实现这一点的方法。

有没有办法实现”粗体动态字体类型”?

  • 您不能将粗体标记为 Body,它是一种文本样式。使用系统/自定义字体选择粗斜体等。
  • @omerfarukozturk 感谢您的评论。看来您为我指出了正确的方向,认为在我的情况下,粗体字体可以被视为”自定义字体”。该文档显示了如何处理自定义字体。请看下面我的回答。


好的,文档似乎提供了提示。对于使用自定义字体,我现在考虑将 bold 字体作为自定义字体,可以使用 UIFontMetrics 类。

如果我将”粗体自定义字体”传递给 UIFontMetrics 类的 scaledFontForFont 方法,我会使用动态字体类型 UIFontTextStyleBody 获得所需的结果。

1
2
UIFont* boldSystemFont = [UIFont boldSystemFontOfSize:17];
myLabel.font           = [[UIFontMetrics metricsForTextStyle:UIFontTextStyleBody] scaledFontForFont:boldSystemFont];

到目前为止,我只看到这种使用代码的方式。不知道这是否也可以在 Interface Builder 中设置。


来源:https://www.codenong.com/63071511/

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_9099.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?