UIButton label text is being clipped(UIButton 标签文本被剪裁)
问题描述
我在 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 标签文本被剪裁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:UIButton 标签文本被剪裁
基础教程推荐
- 如何比较两个 NSDate:哪个是最近的? 2022-01-01
- Android Volley - 如何动画图像加载? 2022-01-01
- Xcode UIView.init(frame:) 只能在主线程中使用 2022-01-01
- UIImage 在开始时不适合 UIScrollView 2022-01-01
- 如何将图像从一项活动发送到另一项活动? 2022-01-01
- iOS - UINavigationController 添加多个正确的项目? 2022-01-01
- 为什么姜饼模拟器方向卡在应用程序中? 2022-01-01
- navigationItem.backBarButtonItem 不工作?为什么上一个菜单仍然显示为按钮? 2022-01-01
- SwiftUI-ScrollViewReader的ScrollTo不滚动 2022-01-01
- Play 商店的设备兼容性问题 2022-01-01
