ios 调整文本的字体大小以适合 UIButton
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32561435/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adjust font size of text to fit in UIButton
提问by MarkHim
I want to make sure the button text fits into a UIButton, while the UIButton has a fixed size.
我想确保按钮文本适合 UIButton,而 UIButton 具有固定大小。
Of course I can access the titleLabel of the UIButton.
In a label I would set autoshrink
to minimum font scale which seems to correspond to
当然我可以访问 UIButton 的 titleLabel。在标签中,我将设置autoshrink
为似乎对应的最小字体比例
self.myButton.titleLabel.adjustsFontSizeToFitWidth = YES;
, but doesn't really behave the same, since it only makes the text fits horizontally into the bounds, not vertically, thereby not changing the font size.
,但实际上并不相同,因为它只会使文本水平地适合边界,而不是垂直地,因此不会改变字体大小。
How can i actually adjust the font size of a label programmatically to make the text fit into the label bounds (as shown in Goalin the picture below) ?
我如何以编程方式实际调整标签的字体大小以使文本适合标签边界(如下图的 目标所示)?
I already tried
我已经试过了
self.myButton.titleLabel.numberOfLines = 0;
self.myButton.titleLabel.minimumScaleFactor = 0.5f;
without success, always ended up as in adjustsFontSizeToFitWidth
on the left side of the pic above.
没有成功,总是像adjustsFontSizeToFitWidth
上图的左侧那样结束。
Edit: The solution also has to be ios7 compliant
编辑:该解决方案还必须与 ios7 兼容
回答by MarkHim
self.mybutton.titleLabel.minimumScaleFactor = 0.5f;
self.mybutton.titleLabel.numberOfLines = 0; <-- Or to desired number of lines
self.mybutton.titleLabel.adjustsFontSizeToFitWidth = YES;
... did the trick, after layoutIfNeeded in viewDidLoad As it turns out, all those must be set to actually adjust the font-size, not just making it fit into the frame.
... 在 viewDidLoad 中的 layoutIfNeeded 之后,成功了,事实证明,所有这些都必须设置为实际调整字体大小,而不仅仅是使其适合框架。
Update for Swift 3:
Swift 3 更新:
mybutton.titleLabel?.minimumScaleFactor = 0.5
mybutton.titleLabel?.numberOfLines = 0 <-- Or to desired number of lines
mybutton.titleLabel?.adjustsFontSizeToFitWidth = true
回答by CodeBender
Updated code for Swift 3.0:
Swift 3.0 的更新代码:
yourButton.titleLabel?.minimumScaleFactor = 0.5
yourButton.titleLabel?.numberOfLines = 0
yourButton.titleLabel?.adjustsFontSizeToFitWidth = true
回答by StackUnderflow
You can also set a User Defined Runtime Attribute in Interface Builder for the button where
您还可以在 Interface Builder 中为按钮设置用户定义的运行时属性,其中
titleLabel.adjustsFontSizeToFitWidth = true
回答by Jason Nam
Try to call the following method.
尝试调用以下方法。
button.titleLabel?.baselineAdjustment = UIBaselineAdjustment.AlignCenters
回答by Hamid Reza Ansari
to make text horizontally and vertically fit with button bounds :
使文本水平和垂直适合按钮边界:
1 - set button alignment like image (* VERY IMPORTANT *)
1 - 像图像一样设置按钮对齐(* 非常重要 *)
2 - add this lines of code in your ViewController
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)
- note that DON'T using
btnTest.titleLabel!.lineBreakMode = NSLineBreakMode.byWordWrapping
or other BreakMode in your code . for enable multiline in button . just using from above code
- 请注意,不要
btnTest.titleLabel!.lineBreakMode = NSLineBreakMode.byWordWrapping
在您的代码中使用或其他 BreakMode。用于在按钮中启用多行。只是使用上面的代码
final result :
最后结果 :
回答by Wostein
try this:
尝试这个:
[myButton setFont:[[myButton font] fontWithSize:--originalButtonFontSize]];
textWidth = [text sizeWithFont:[myButton font]].width;
}
回答by VJVJ
In Xcode->Open storyboard->Go to attributes inspector->Control->Alignment->Choose second in both horizontal and vertical.
在Xcode->打开故事板->转到属性检查器->控制->对齐->在水平和垂直中选择第二个。
or
或者
YourButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
回答by Dishant Rajput
Write this line after your code:
在你的代码之后写下这一行:
yourButton.SizeToFitWidth=YES;