problem:
Button size is enough, however when I change title, the title text cannot fit the button width. Any SDK function can solve this problem, or I need to manually code to solve it?
按钮大小就足够了,但是当我更改标题时,标题文本无法适应按钮宽度。任何SDK功能都可以解决这个问题,或者我需要手动编码来解决它?
Please refer to following pictures.
请参考以下图片。
design in the nib file.
在nib文件中设计。
initial show in simulator
最初在模拟器中显示
when I change the title text
当我改变标题文字时
tried some ways before
-
_button.titleLabel.adjustsFontSizeToFitWidth = YES;
the way will change my font size. I cannot accept the way._button.titleLabel.adjustsFontSizeToFitWidth = YES;这种方式会改变我的字体大小。我不能接受的方式。
-
[_button setTitleEdgeInsets:UIEdgeInsetsMake(10.0, 10.0, 0.0,0.0)];
the way change label's position only, not label size.[_button setTitleEdgeInsets:UIEdgeInsetsMake(10.0,10.0,0.0,0.0)];改变标签位置的方式,而不是标签尺寸。
-
[_button.titleLabel sizeToFit];
result is same with picture(3).[_button.titleLabel sizeToFit];结果与图片(3)相同。
-
[_button sizeToFit];
title moved to upper left corner, and the title still the same result.[_button sizeToFit];标题移到左上角,标题仍然是相同的结果。
Just confused, my button size is big enough, why title size is so small?
只是困惑,我的按钮大小足够大,为什么标题尺寸这么小?
5 个解决方案
#1
38
Use this.
用这个。
Objective-c
Objective-C的
button.titleLabel.numberOfLines = 1;
button.titleLabel.adjustsFontSizeToFitWidth = YES;
button.titleLabel.lineBreakMode = NSLineBreakByClipping;
Swift 2.0
Swift 2.0
button.titleLabel?.numberOfLines = 0
button.titleLabel?.adjustsFontSizeToFitWidth = true
button.titleLabel?.lineBreakMode = NSLineBreakMode.ByWordWrapping
NOTE: Swift code courtesy: @Rachel Harvey
注意:Swift代码礼貌:@Rachel Harvey
#2
11
For people who come across with this question:
对于遇到这个问题的人:
Try using the setter:
尝试使用setter:
[self.myButton setTitle:@"Title" forState:UIControlStateNormal];
[self.myButton sizeToFit];
#3
3
Make sure also that the label doesn't adjust later spacing, as that seems to take precedence over adjusting font size. Also make sure the minimumScaleFactor < 1.
还要确保标签不会调整以后的间距,因为这似乎优先于调整字体大小。还要确保minimumScaleFactor <1。
button.titleLabel.adjustsFontSizeToFitWidth = YES;
button.titleLabel.adjustsLetterSpacingToFitWidth = NO;
button.titleLabel.minimumScaleFactor = 0.5;
#4
1
Here is the Swift version:
这是Swift版本:
let button = UIButton()
button.titleLabel?.numberOfLines = 1
button.titleLabel?.adjustsFontSizeToFitWidth = true
button.titleLabel?.lineBreakMode = NSLineBreakMode.ByClipping
#5
0
In IB, just select the button, go to the editor menu, and choose "Size To Fit Content"
在IB中,只需选择按钮,转到编辑器菜单,然后选择“尺寸适合内容”
#1
38
Use this.
用这个。
Objective-c
Objective-C的
button.titleLabel.numberOfLines = 1;
button.titleLabel.adjustsFontSizeToFitWidth = YES;
button.titleLabel.lineBreakMode = NSLineBreakByClipping;
Swift 2.0
Swift 2.0
button.titleLabel?.numberOfLines = 0
button.titleLabel?.adjustsFontSizeToFitWidth = true
button.titleLabel?.lineBreakMode = NSLineBreakMode.ByWordWrapping
NOTE: Swift code courtesy: @Rachel Harvey
注意:Swift代码礼貌:@Rachel Harvey
#2
11
For people who come across with this question:
对于遇到这个问题的人:
Try using the setter:
尝试使用setter:
[self.myButton setTitle:@"Title" forState:UIControlStateNormal];
[self.myButton sizeToFit];
#3
3
Make sure also that the label doesn't adjust later spacing, as that seems to take precedence over adjusting font size. Also make sure the minimumScaleFactor < 1.
还要确保标签不会调整以后的间距,因为这似乎优先于调整字体大小。还要确保minimumScaleFactor <1。
button.titleLabel.adjustsFontSizeToFitWidth = YES;
button.titleLabel.adjustsLetterSpacingToFitWidth = NO;
button.titleLabel.minimumScaleFactor = 0.5;
#4
1
Here is the Swift version:
这是Swift版本:
let button = UIButton()
button.titleLabel?.numberOfLines = 1
button.titleLabel?.adjustsFontSizeToFitWidth = true
button.titleLabel?.lineBreakMode = NSLineBreakMode.ByClipping
#5
0
In IB, just select the button, go to the editor menu, and choose "Size To Fit Content"
在IB中,只需选择按钮,转到编辑器菜单,然后选择“尺寸适合内容”