【问题标题】:Attributed titleLabel in UIButton (IB) does not respect setFontUIButton(IB)中的属性titleLabel不尊重setFont
【发布时间】:2016-09-26 14:42:14
【问题描述】:

当我在 IB 中将 UIButton 的标题设置为属性并尝试以编程方式更改字体时:

button.titleLabel?.font = UIFont.systemFontOfSize(20)

字体不增加/减少...

有什么办法可以解决这个问题吗?

【问题讨论】:

  • 一种方法是在 titleLabel 的 'attributedText' 属性上设置 NSFontAttributeName

标签: ios swift uibutton swift2


【解决方案1】:

调用 UIButton 的 setAttributedTitle:forState: 设置属性标题

【讨论】:

  • 我不想改变标题...只是字体。基本上想要 button.adjustsFontSizeToFitWidth = true 但这不适用于属性文本
【解决方案2】:

当您为按钮标题使用属性字符串时,您必须在创建属性字符串时使用NSFontAttributeName 属性设置字体。

【讨论】:

    猜你喜欢
    • 2016-09-10
    • 2015-12-03
    • 2013-10-09
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-21
    相关资源
    最近更新 更多