[英]How to auto-move/auto-adjust the save button for a note-taking app in swift?
[英]UIButton auto-adjust Button font size Swift
我尝试进行一些测验,并且回答按钮有问题。 简单事实:文本太长,我尝试使用不同的方法针对单元格自动调整文本。 我目前的状态:
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)
}
我希望有人对我有另一种选择:)
问候,帕特里克
编辑:
self.view.addConstraint(NSLayoutConstraint(item: btn.titleLabel!, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: btn, attribute:NSLayoutAttribute.Height, multiplier: 0.99, constant: 0))
使用自动布局,您可以设置按钮之间的间距以及最大和最小尺寸。 在所有标签的代码中使用:
self.button.titleLabel.numberOfLines = 0;
self.button.titleLabel.adjustsFontSizeToFitWidth = YES;
这样,所有标签都会调整文本大小。
对于对titleLabel的调整按钮,请对titleLabel使用自动布局约束。 举些例子:
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.button.titleLabel
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeHeight
multiplier:0.5
constant:0]];
这个约束定义titleLabel的高度的50% self.view
高度。 现在,您可以根据需要调整约束。
这项工作在您的代码中吗?
假设单元格的高度和字体相同,则可以通过将“ numberOfLines”设置为非0的值(例如3或4)来限制文本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.