问题描述
我在 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 标签文本被剪裁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)