简体   繁体   中英

Adjust font size of text to fit in UIButton

I want to make sure the button text fits into a UIButton, while the UIButton has a fixed size.

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

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 Goal in the picture below) ?

adjustsFontSizeToFitWidth 实际上并没有调整字体大小以适应框架,只是调整了宽度

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.

Edit : The solution also has to be ios7 compliant

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.

Update for Swift 3:

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

您还可以在 Interface Builder 中为按钮设置用户定义的运行时属性,其中

titleLabel.adjustsFontSizeToFitWidth = true

Updated code for Swift 3.0:

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

to make text horizontally and vertically fit with button bounds :

1 - set button alignment like image (* VERY IMPORTANT *)

在此处输入图片说明

2 - add this lines of code in your 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

final result :

在此处输入图片说明

尝试调用以下方法。

button.titleLabel?.baselineAdjustment = UIBaselineAdjustment.AlignCenters

try this:

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

}

In Xcode->Open storyboard->Go to attributes inspector->Control->Alignment->Choose second in both horizontal and vertical.

or

YourButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;

在 Storyboard 中,转到 Attributes Inspector 中的 Link Break,查看自动换行...或根据您的选择进行设置。

在代码后面写下这一行:

yourButton.SizeToFitWidth=YES;

If you prefer do in storyboard, just click the button, then show attributes inspector, and set the line break to "Word Wrap".

自动换行

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM