问题描述
我正在尝试在UIButton中放入字母"6",以便当程序在小屏幕设备上运行时,字体会自动缩小,以使字母6的所有面都在里面.
I'm trying to fit the letter '6' inside a UIButton so that the font automatically scales down when the program is run on a small screen device, such that the letter 6 is inside from all sides.
我发现了其他几个线程可以为同一问题提供解决方案,但是由于某些原因,它们对我不起作用.我发现了一些线程:
I found several other threads offering solutions to the same problem but for some reason they don't work for me. A few threads I found were:
我尝试了以下两个代码序列,但没有一个能完成工作:
I have tried the following two code sequences but none gets the job done:
我该怎么办?
self.sixButton.titleLabel!.numberOfLines = 0;
self.sixButton.titleLabel!.adjustsFontSizeToFitWidth = true;
self.sixButton.titleLabel!.lineBreakMode = NSLineBreakMode.ByWordWrapping
2:
self.sixButton.titleLabel!.minimumScaleFactor = 0.5;
self.sixButton.titleLabel!.numberOfLines = 0;
self.sixButton.titleLabel!.adjustsFontSizeToFitWidth = true;
3:与上述相同,但带有self.sixButton.titleLabel!.numberOfLines = 1;
.
我正在将Swift 2.2与Xcode 7.3.1结合使用.
3: Same as above but with self.sixButton.titleLabel!.numberOfLines = 1;
.
I'm using Swift 2.2 with Xcode 7.3.1.
设计时按钮的图片:
运行时按钮的图片:
推荐答案
我要做的是以连续较小的字体大小测量"6"的大小,直到达到适合所需范围的大小,然后使用那个大小.
What I would do is measure the size of the "6" in successively smaller font sizes until we get to a size that fits in the desired bounds, and use that size.
这篇关于更改字体大小以完全适合按钮内的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!