[英]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 :) 我希望有人对我有另一种选择:)
Regards, Patrick 问候,帕特里克
EDIT: 编辑:
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. 这样,所有标签都会调整文本大小。
For adjust button to titleLabel use auto layout constraint for titleLabel. 对于对titleLabel的调整按钮,请对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]];
This constraint define the height of titleLabel to 50% of self.view
height. 这个约束定义titleLabel的高度的50% self.view
高度。 Now you can adapte the constraint for that you need. 现在,您可以根据需要调整约束。
This work in you code? 这项工作在您的代码中吗?
假设单元格的高度和字体相同,则可以通过将“ numberOfLines”设置为非0的值(例如3或4)来限制文本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.