按钮大小足够了,但是当我更改标题时,标题文本无法适应按钮宽度.任何SDK功能都可以解决这个问题,还是我需要手动编码才能解决?
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?
请参考以下图片.
在 nib 文件中设计.
design in the nib file.
模拟器中的初始显示
当我更改标题文本时
_button.titleLabel.adjustsFontSizeToFitWidth = YES;
这种方式会改变我的字体大小.我不能接受这种方式.
_button.titleLabel.adjustsFontSizeToFitWidth = YES;
the way will change my font size. I cannot accept the way.
[_button setTitleEdgeInsets:UIEdgeInsetsMake(10.0, 10.0, 0.0,0.0)];
方式只改变标签的位置,不改变标签的大小.
[_button setTitleEdgeInsets:UIEdgeInsetsMake(10.0, 10.0, 0.0,0.0)];
the way change label's position only, not label size.
[_button.titleLabel sizeToFit];
结果与图片(3)相同.
[_button.titleLabel sizeToFit];
result is same with picture(3).
[_button sizeToFit];
标题移到左上角,标题还是一样的结果.
[_button sizeToFit];
title moved to upper left corner, and the title still the same result.
一头雾水,我的按钮大小够大,标题怎么这么小?
Just confused, my button size is big enough, why title size is so small?
使用这个.
目标-c
button.titleLabel.numberOfLines = 1;
button.titleLabel.adjustsFontSizeToFitWidth = YES;
button.titleLabel.lineBreakMode = NSLineBreakByClipping;
斯威夫特 2.0
button.titleLabel?.numberOfLines = 0
button.titleLabel?.adjustsFontSizeToFitWidth = true
button.titleLabel?.lineBreakMode = NSLineBreakMode.ByWordWrapping
注意:Swift 代码礼貌:@Rachel Harvey
NOTE: Swift code courtesy: @Rachel Harvey
斯威夫特 5.0
button.titleLabel?.numberOfLines = 0
button.titleLabel?.adjustsFontSizeToFitWidth = true
button.titleLabel?.lineBreakMode = .byWordWrapping
这篇关于如何让文本适合 UIButton?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!