UIButton 自动调整 Button 字体大小 Swift [英] UIButton auto-adjust Button font size Swift
问题描述
我试着做一些测验,但我的回答按钮有问题.简单的事实:文本太长,我尝试使用不同的方法为单元格自动调整它.我现在的状态:
I try to make out some quiz and have a problem with my answer buttons. Simple fact: The text is too long and I try to auto-adjust it for cells using different methods. My current status:
for btn in btnArr{
btn.titleLabel!.minimumScaleFactor = 0.3
btn.titleLabel!.numberOfLines = 0
btn.titleLabel!.adjustsFontSizeToFitWidth = true
btn.titleLabel?.baselineAdjustment = UIBaselineAdjustment.AlignCenters
//btn.contentEdgeInsets = UIEdgeInsets(top: 3.0,left: 3.0,bottom: 3.0,right: 3.0)
}
我希望有人给我另一种选择来完成这项工作:)
I hope somebody has another option for me to make that work :)
问候,帕特里克
self.view.addConstraint(NSLayoutConstraint(item: btn.titleLabel!, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: btn, attribute:NSLayoutAttribute.Height, multiplier: 0.99, constant: 0))
推荐答案
通过自动布局,您可以设置按钮之间的间距,以及最大和最小尺寸.在所有标签的代码中使用:
With auto layout you can set the space between buttons, and the max and min size. In code for all labels use:
self.button.titleLabel.numberOfLines = 0;
self.button.titleLabel.adjustsFontSizeToFitWidth = YES;
有了这个,所有标签都会调整文本的大小.
With this all labels adjust the text will size.
要将按钮调整为 titleLabel,请使用 titleLabel 的自动布局约束.例如:
For adjust button to titleLabel use auto layout constraint for titleLabel. For examples:
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.button.titleLabel
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeHeight
multiplier:0.5
constant:0]];
此约束将 titleLabel 的高度定义为 self.view
高度的 50%.现在您可以根据需要调整约束.
This constraint define the height of titleLabel to 50% of self.view
height. Now you can adapte the constraint for that you need.
这在你的代码中起作用吗?
This work in you code?
这篇关于UIButton 自动调整 Button 字体大小 Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!