ios - 设置带边距的 UIButton 最小字体大小

标签 ios uibutton

我找到了如何设置与按钮宽度相对应的最小字体大小的解决方案:

button.titleLabel.numberOfLines = 1;
button.titleLabel.adjustsFontSizeToFitWidth = YES;
button.titleLabel.lineBreakMode = NSLineBreakByClipping;

但是如何在左侧和右侧添加边距。

因此按钮标签的宽度为 200 pt,与按钮宽度相同。我需要我的标题标签在右侧和左侧有空格,因此这意味着标签宽度必须为 180pt,然后我将在右侧和左侧各有 10pt。

我怎样才能实现这个目标?

最佳答案

使用titleEdgeInsets:

button.titleEdgeInsets = UIEdgeInsetsMake(top, left, bottom, right);

或者特别是在这种情况下:

button.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 10);

关于ios - 设置带边距的 UIButton 最小字体大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21006067/

相关文章:

ios - UITableView 单元格标题传输到另一个 View 上的 Uitextfield

iphone - 在 iOS 中以两种不同颜色设置按钮标题

ios - 如何使用 prepareForSegue(Swift 4、Xcode 9)将 UIButton 的标签(Int)传递给其他 ViewController

ios - 在 Swift 中使用 UITextView 属性自定义 UIView 的加载时间缓慢

ios - 以编程方式计算 Storyboard UIViewController 中附加的 segues

ios - 均匀分布 4 或 5 个按钮,具体取决于用户设备

ios - 在 UIButton 中创建三角形

ios - 从 Swift 中的 UITableViewCell 子类中获取 IndexPath

ios - UIPickerView 设置选定行的背景颜色(现在也适用于 iOS 14)

ios - 无法将对象从另一个类添加到 NSMutableArray