Scaling a font on a multilined UIButton depending on a device size

前提是你 提交于 2019-12-22 11:36:03

问题


Current setup:

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.

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

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

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.

What I have tried:

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?

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


回答1:


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:




回答2:


Sadly you cannot vary the font for the button in interface builder based on the size class; however you can remove entire controls based on the size class. Choose the attributes inspector (Option+command+4) with your button selected. Scroll all the way to the bottom and you will see a check box for installed with a tiny plus button on the left. If you click the plus you can now add variations of size classes to uninstall this control. Specifically uninstall it for Regular Width Regular Height (which is iPad in non split screen). Now make a second button with your iPad attributes and install it ONLY for Regular Width Regular Height. You now have two different buttons: 1) for iPad in no split screen and one for everything else, no code needed. You can set them to invoke the same IBAction. See this link for more.



来源:https://stackoverflow.com/questions/41676962/scaling-a-font-on-a-multilined-uibutton-depending-on-a-device-size

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!