在多线UIButton上缩放字体,具体取决于设备大小 [英] Scaling a font on a multilined UIButton depending on a device size

查看:100
本文介绍了在多线UIButton上缩放字体,具体取决于设备大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前设置:

我有一个多行 UIButton 添加到它的超级视图(普通的 UIView )。按钮的宽度是其超视图的90%。因此,当超视图的大小发生变化时,由于 Autolayout 约束,标签的宽度也会相应变化。

I have a multilined UIButton which is added to its superview (an ordinary UIView). Button's width is 90% of its superview. So when its superview's size changes, label's width changes accordingly due to Autolayout constraints.

接下来,在viewDidLoad方法中我这样做了:

Next, inside of viewDidLoad method I did this:

  //Line break mode and Font size ( are set in Interface Builder
  myButton.titleLabel?.adjustsFontSizeToFitWidth = true
  myButton.titleLabel?.minimumScaleFactor = 15.0
  myButton.titleLabel?.numberOfLines = 2

所以现在,当我有长文本时,在iPhone设备上我会得到这样的结果:

So now, when I have long text, on iPhone devices I get something like this:

这正是我想要的。当然,这在iPad设备上看起来有所不同。例如,这是在iPad Pro 9.7上:

which is exactly what I want. But of course, this looks different on iPad devices. For example, this is on iPad Pro 9.7":

正如可以看到的,一切看起来都很小而且很糟糕。

And as it can be see, everything looks small and bad.

我尝试过:

我试图设置最大字体大小(将在iPad设备上使用) ),与 minimumScaleFactore = 15 组合使用。因此我将字体大小设置为25,现在iPad和iPhone上的所有内容都更容易接受,但在iPhone设备上,由于minimumScaleFactor的工作方式,文本比我需要的要大一些。

I tried to set maximum font size (which will be used on iPad devices), in combination with minimumScaleFactore = 15. So I've set font size to 25 and now everything looks more acceptable on both iPad and iPhone, but still, on the iPhone devices, text is a bit bigger than I need because of how minimumScaleFactor works.

解决这个问题的方法是什么?

What would be a way to solve this?

我更喜欢这样的事情(在所有的iPad和iPhone上看到类似的外观)设备):

I am more into getting something like this (to get similar look on all iPad and iPhone devices):

推荐答案

您可以使用大小类来指定IB中按钮/标签上的字体。只需按下字体属性左侧的小+号:

You can use size classes to specify the font on your button/label in IB. Just press the little + sign to the left of the font property:

这篇关于在多线UIButton上缩放字体,具体取决于设备大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆