我在 Interface Builder 中内置了一个具有默认标签的 UIButton.在 Xcode 中,我正在动态更改标签文本,如下所示:
I have a UIButton built in Interface Builder that has a default label. In Xcode, I'm changing the label text dynamically like so:
myButton.titleLabel.text = @"this is the new label";
但是,当文本更新时,新字符串被剪裁到与原始字符串相同的大小,最终看起来像:
However, when the text updates, the new string is being clipped down to the same size as the original string and ends up looking like:
this...label
有人知道为什么会这样吗?
Anyone know why this is happening?
你应该使用 setTitle:forState: 更改 UIButton
的标题.如果您自己更改标题,则按钮不会显示需要调整标签大小 - 您最终将不得不执行以下操作:
You should use setTitle:forState: to change the title of a UIButton
. If you change the title yourself, the button has no indication that it needs to resize the label –you'd end up having to do something like this:
myButton.titleLabel.text = @"this is the new label";
[myButton setNeedsLayout];
但我什至不确定这是否适用于所有情况.提供了诸如 setTitle:forState:
之类的方法,以便您可以为多个状态提供标题,而无需手动更新按钮,并且按钮知道需要使用新标题对其进行布局.
but I'm not even sure that would work in all cases. Methods like setTitle:forState:
are provided so that you can provide titles for multiple states without having to update the button manually, and so that the button knows that it needs to be laid out with a new title.
这篇关于UIButton 标签文本被剪裁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!