调整文本的字体大小以适应 UIButton
Adjust font size of text to fit in UIButton
我想确保按钮文本适合 UIButton,而 UIButton 具有固定大小。
我当然可以访问UIButton 的titleLabel。
在标签中,我将 autoshrink
设置为最小字体比例,这似乎对应于
self.myButton.titleLabel.adjustsFontSizeToFitWidth = YES;
,但实际上并不相同,因为它只会使文本水平而不是垂直地适合边界,因此不会改变字体大小。
我如何实际以编程方式调整标签的字体大小以使文本适合标签边界(如下图 目标 所示)?
我已经试过了
self.myButton.titleLabel.numberOfLines = 0;
self.myButton.titleLabel.minimumScaleFactor = 0.5f;
没有成功,总是像上图左侧的adjustsFontSizeToFitWidth
那样结束。
编辑:解决方案也必须ios7兼容
尝试调用以下方法。
button.titleLabel?.baselineAdjustment = UIBaselineAdjustment.AlignCenters
self.mybutton.titleLabel.minimumScaleFactor = 0.5f;
self.mybutton.titleLabel.numberOfLines = 0; <-- Or to desired number of lines
self.mybutton.titleLabel.adjustsFontSizeToFitWidth = YES;
...成功了,在 viewDidLoad 中的 layoutIfNeeded 之后
事实证明,所有这些都必须设置为实际调整字体大小,而不仅仅是使其适合框架。
Swift3 的更新:
mybutton.titleLabel?.minimumScaleFactor = 0.5
mybutton.titleLabel?.numberOfLines = 0 <-- Or to desired number of lines
mybutton.titleLabel?.adjustsFontSizeToFitWidth = true
在Xcode->打开故事板->转到属性检查器->控制->对齐->水平和垂直都选择第二个。
或
YourButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
试试这个:
[myButton setFont:[[myButton font] fontWithSize:--originalButtonFontSize]];
textWidth = [text sizeWithFont:[myButton font]].width;
}
您还可以在 Interface Builder 中为按钮设置用户定义的运行时属性
titleLabel.adjustsFontSizeToFitWidth = true
Swift 3.0 的更新代码:
yourButton.titleLabel?.minimumScaleFactor = 0.5
yourButton.titleLabel?.numberOfLines = 0
yourButton.titleLabel?.adjustsFontSizeToFitWidth = true
如果您更喜欢在情节提要中执行,只需单击按钮,然后显示属性检查器,并将换行符设置为 "Word Wrap"。
使文本水平和垂直适合按钮边界:
1 - 像图像一样设置按钮对齐方式(* 非常重要 *)
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
或其他中断模式。用于在按钮中启用多行。仅使用上面的代码
最终结果:
在 Storyboard 中,转到 Link Break in Attributes Inspector,查看自动换行..或根据您的选择进行设置。
我想确保按钮文本适合 UIButton,而 UIButton 具有固定大小。
我当然可以访问UIButton 的titleLabel。
在标签中,我将 autoshrink
设置为最小字体比例,这似乎对应于
self.myButton.titleLabel.adjustsFontSizeToFitWidth = YES;
,但实际上并不相同,因为它只会使文本水平而不是垂直地适合边界,因此不会改变字体大小。
我如何实际以编程方式调整标签的字体大小以使文本适合标签边界(如下图 目标 所示)?
我已经试过了
self.myButton.titleLabel.numberOfLines = 0;
self.myButton.titleLabel.minimumScaleFactor = 0.5f;
没有成功,总是像上图左侧的adjustsFontSizeToFitWidth
那样结束。
编辑:解决方案也必须ios7兼容
尝试调用以下方法。
button.titleLabel?.baselineAdjustment = UIBaselineAdjustment.AlignCenters
self.mybutton.titleLabel.minimumScaleFactor = 0.5f;
self.mybutton.titleLabel.numberOfLines = 0; <-- Or to desired number of lines
self.mybutton.titleLabel.adjustsFontSizeToFitWidth = YES;
...成功了,在 viewDidLoad 中的 layoutIfNeeded 之后 事实证明,所有这些都必须设置为实际调整字体大小,而不仅仅是使其适合框架。
Swift3 的更新:
mybutton.titleLabel?.minimumScaleFactor = 0.5
mybutton.titleLabel?.numberOfLines = 0 <-- Or to desired number of lines
mybutton.titleLabel?.adjustsFontSizeToFitWidth = true
在Xcode->打开故事板->转到属性检查器->控制->对齐->水平和垂直都选择第二个。
或
YourButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
试试这个:
[myButton setFont:[[myButton font] fontWithSize:--originalButtonFontSize]];
textWidth = [text sizeWithFont:[myButton font]].width;
}
您还可以在 Interface Builder 中为按钮设置用户定义的运行时属性
titleLabel.adjustsFontSizeToFitWidth = true
Swift 3.0 的更新代码:
yourButton.titleLabel?.minimumScaleFactor = 0.5
yourButton.titleLabel?.numberOfLines = 0
yourButton.titleLabel?.adjustsFontSizeToFitWidth = true
如果您更喜欢在情节提要中执行,只需单击按钮,然后显示属性检查器,并将换行符设置为 "Word Wrap"。
使文本水平和垂直适合按钮边界:
1 - 像图像一样设置按钮对齐方式(* 非常重要 *)
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
或其他中断模式。用于在按钮中启用多行。仅使用上面的代码
最终结果:
在 Storyboard 中,转到 Link Break in Attributes Inspector,查看自动换行..或根据您的选择进行设置。