UIButton自动调整button字体大小Swift

我尝试做出一些测验,并对我的答案button有问题。 简单的事实:文本太长,我尝试使用不同的方法自动调整单元格。 我目前的状态:

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) } 

我希望有人有另一种select,使我的工作:)

问候,帕特里克

编辑:

  self.view.addConstraint(NSLayoutConstraint(item: btn.titleLabel!, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: btn, attribute:NSLayoutAttribute.Height, multiplier: 0.99, constant: 0)) 

使用自动布局,您可以设置button之间的空间,以及最大和最小尺寸。 在所有标签的代码中使用:

 self.button.titleLabel.numberOfLines = 0; self.button.titleLabel.adjustsFontSizeToFitWidth = YES; 

有了这个所有的标签调整文字的大小。

对于titleLabel调整button,使用titleLabel的自动布局约束。 举些例子:

  [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%。 现在你可以适应你所需要的约束。

这个工作在你的代码?

假设单元格的高度相同且字体相同,则可以通过将“numberOfLines”设置为0以外的值(如3或4)来限制文本。