调整字体大小以适应UIButton中的文本

40

我希望确保按钮文本适合于固定大小的UIButton中。

当然,我可以访问UIButtontitleLabel。 在一个标签中,我会将autoshrink设置为最小字体比例,这似乎对应于

self.myButton.titleLabel.adjustsFontSizeToFitWidth = YES;

但实际上并不表现得相同,因为它只会使文本水平适应边界,而不是垂直适应,从而不改变字体大小。

我该如何通过编程方式调整标签的字体大小,使文本适合于标签的边界(如下图中的目标)?

adjustsFontSizeToFitWidth does not actually adjust the font size to fit the frame, only the width

我已经尝试过

self.myButton.titleLabel.numberOfLines = 0;
self.myButton.titleLabel.minimumScaleFactor = 0.5f;

尝试了许多次,但总以失败告终,就像上图左侧所示的adjustsFontSizeToFitWidth一样。

编辑: 解决方案还必须符合ios7标准。


你设置了 self.myButton.titleLabel.minimumFontSize 属性吗? - Venk
我使用了minimumScaleFactor而不是minimumFontSize,但功能上应该没有什么区别。 - MarkHim
哦,我刚刚看到minimumFontSize被弃用了@Venkat - MarkHim
可能是调整UIButton字体大小以适应宽度的重复问题。 - Nike Kov
9个回答

94
self.mybutton.titleLabel.minimumScaleFactor = 0.5f;
self.mybutton.titleLabel.numberOfLines = 0;   <-- Or to desired number of lines
self.mybutton.titleLabel.adjustsFontSizeToFitWidth = YES;

在viewDidLoad中执行layoutIfNeeded后,问题得到了解决。

事实证明,所有这些都必须设置才能真正调整字体大小,而不仅仅是使其适应框架。

Swift 3更新:

mybutton.titleLabel?.minimumScaleFactor = 0.5
mybutton.titleLabel?.numberOfLines = 0   <-- Or to desired number of lines
mybutton.titleLabel?.adjustsFontSizeToFitWidth = true

10
我必须设置 self.myButton.titleLabel.numberOfLines = 1; 否则可能会出现两行。 - Vladimír Slavík
1
@MarkHim 我的情况是文本在按钮中水平放置正常,但垂直方向不行。这种情况下它就无法工作了 ;-( 有什么想法吗? - mica
2
听起来你有硬高度限制 @mica 我假设内在内容大小应该用我的方法正确计算,所以只需删除高度约束即可正常工作。 - MarkHim
2
我必须添加 self.mybutton.titleLabel.lineBreakMode = NSLineBreakByClipping 才能让这个答案正常工作。 - AnthoPak

11

您可以在接口构建器中为按钮设置用户定义的运行时属性

titleLabel.adjustsFontSizeToFitWidth = true

1
确实,这是正确的答案。请确保没有启用“自动换行”(否则不会起作用),而是启用“截断尾部”或其他选项。您还可以为titleLabel.minimumScaleFactor设置一个数字,但必须有titleLabel.adjustsFontSizeToFitWidth属性。在 Interface Builder中选择按钮时,在Identity Inspector 的User Defined Attributes上点击“+”号。感谢@StackUnderflow。 - codeslapper

11

升级至Swift 3.0的代码:

yourButton.titleLabel?.minimumScaleFactor = 0.5
yourButton.titleLabel?.numberOfLines = 0
yourButton.titleLabel?.adjustsFontSizeToFitWidth = true

很棒的解决方案 :) - steveSarsawa

6

使文本水平和垂直适应按钮边界的方法:

1- 设置按钮对齐方式,如下图所示 (* 非常重要 *)

enter image description here

2- 在您的 ViewController 中添加以下代码行:

btnTest.setTitle("Test for multi line in button show line in button show Test for multi line in button show line in button show", for: .normal)
btnTest.titleLabel!.textAlignment = .center
btnTest.titleLabel!.numberOfLines = 0
btnTest.titleLabel!.adjustsFontSizeToFitWidth = true
btnTest.titleLabel!.minimumScaleFactor = 0.5
// below line to add some inset for text to look better
// you can delete or change that
btnTest.contentEdgeInsets = UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)
  • 请注意,不要在您的代码中使用btnTest.titleLabel!.lineBreakMode = NSLineBreakMode.byWordWrapping或其他BreakMode。要启用按钮的多行模式,请仅使用上述代码。

最终结果:

enter image description here


3
尝试调用以下方法。
button.titleLabel?.baselineAdjustment = UIBaselineAdjustment.AlignCenters

2
在Storyboard中,进入属性检查器中的Link Break,查看自动换行或根据您的选择进行设置。


1

try this:

  [myButton setFont:[[myButton font] fontWithSize:--originalButtonFontSize]];
  textWidth = [text sizeWithFont:[myButton font]].width;

}

0
在Xcode中->打开故事板->进入属性检查器->控件->对齐->水平和垂直都选择第二个。
或者
YourButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;

-2
如果您喜欢使用Storyboard进行操作,只需点击按钮,然后显示属性检查器,并将换行设置为“自动换行”。

word wrap


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接