【问题标题】:Have half a UILabel text bold and half not?有一半的 UILabel 文本加粗,一半没有?
【发布时间】:2011-08-24 18:57:46
【问题描述】:

我希望我的UILabeltext 有一半是粗体,另一半不是粗体。我该怎么做?

【问题讨论】:

    标签: objective-c ios cocoa-touch uilabel text-formatting


    【解决方案1】:

    NSAttributedString 允许在字符串中指定格式,但遗憾的是,UIKit 还没有对此做任何事情。但是,有一些开源实现可以做到这一点。查看OHAttributedLabel

    【讨论】:

    • 感谢克里斯托弗提到我的班级 :)
    • OHAttributedLabel 已不再由创建者维护。
    【解决方案2】:

    除非你继承 UILabel 并弄乱子视图,否则你不能轻易做到这一点。最好改用两个 UILabel。在这里找到了another thread

    【讨论】:

      【解决方案3】:

      UILabel 有attributedText,它采用NSAttributedString,它允许您自定义字符串的多个方面,包括更改字体类型和其他属性

        var attributedString = NSMutableAttributedString(attributedString: NSAttributedString(string: "Not Bold"))
      
        let boldAttrString = NSAttributedString(string: "BOLD", attributes: [NSFontAttributeName: UIFont(name: "Avenir-Medium", size: 15)!])
        attributedString.appendAttributedString(boldAttrString)
        myLabel.attributesText = attributedString
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-03
        • 1970-01-01
        • 2011-06-03
        • 2013-10-24
        • 1970-01-01
        • 2019-04-14
        相关资源
        最近更新 更多