正在剪切UIButton标签文本

正在剪切UIButton标签文本

本文介绍了正在剪切UIButton标签文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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?

推荐答案

更改 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标签文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 13:49