ios 我如何让文本适合 UIButton?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13666431/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:23:43  来源:igfitidea点击:

how do i let text fit to UIButton?

iosuibuttonuilabel

提问by CCC

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. enter image description here

设计在 nib 文件中。 在此处输入图片说明

initial show in simulator enter image description here

模拟器中的初始显示 在此处输入图片说明

when I change the title text enter image description here

当我更改标题文本时 在此处输入图片说明

tried some ways before

之前试过一些方法

  1. _button.titleLabel.adjustsFontSizeToFitWidth = YES;
    the way will change my font size. I cannot accept the way.

  2. [_button setTitleEdgeInsets:UIEdgeInsetsMake(10.0, 10.0, 0.0,0.0)];
    the way change label's position only, not label size.

  3. [_button.titleLabel sizeToFit];
    result is same with picture(3).

  4. [_button sizeToFit];
    title moved to upper left corner, and the title still the same result.

  1. _button.titleLabel.adjustsFontSizeToFitWidth = YES;
    方式会改变我的字体大小。我不能接受这种方式。

  2. [_button setTitleEdgeInsets:UIEdgeInsetsMake(10.0, 10.0, 0.0,0.0)];
    这种方式只改变标签的位置,而不是标签大小。

  3. [_button.titleLabel sizeToFit];
    结果与图(3)相同。

  4. [_button sizeToFit];
    标题移到左上角,标题还是一样的结果。

Just confused, my button size is big enough, why title size is so small?

只是困惑,我的按钮大小足够大,为什么标题大小如此之小?

回答by Nilesh

Use this.

用这个。

Objective-c

目标-c

button.titleLabel.numberOfLines = 1;
button.titleLabel.adjustsFontSizeToFitWidth = YES;
button.titleLabel.lineBreakMode = NSLineBreakByClipping; 

Swift 2.0

斯威夫特 2.0

button.titleLabel?.numberOfLines = 0
button.titleLabel?.adjustsFontSizeToFitWidth = true
button.titleLabel?.lineBreakMode = NSLineBreakMode.ByWordWrapping

NOTE: Swift code courtesy: @Rachel Harvey

注意:Swift 代码礼貌:@Rachel Harvey

回答by Jonathan P. Diaz

For people who come across with this question:

对于遇到这个问题的人:

Try using the setter:

尝试使用 setter:

 [self.myButton setTitle:@"Title" forState:UIControlStateNormal];
 [self.myButton sizeToFit];

回答by user3099609

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;

回答by Esqarrouth

Here is the Swift version:

这是 Swift 版本:

    let button = UIButton()
    button.titleLabel?.numberOfLines = 1
    button.titleLabel?.adjustsFontSizeToFitWidth = true
    button.titleLabel?.lineBreakMode = NSLineBreakMode.ByClipping

回答by rdelmar

In IB, just select the button, go to the editor menu, and choose "Size To Fit Content"

在 IB 中,只需选择按钮,转到编辑器菜单,然后选择“大小以适合内容”