Make part of a UILabel bold in Swift

后端 未结 9 1955
面向向阳花
面向向阳花 2020-12-02 12:50

I have a UILabel I\'ve made programmatically as:

var label = UILabel()

I\'ve then declared some styling for the label, includi

9条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-02 13:34

    You will want to use attributedString which allows you to style parts of a string etc. This can be done like this by having two styles, one normal, one bold, and then attaching them together:

    let boldText = "Filter:"
    let attrs = [NSAttributedString.Key.font : UIFont.boldSystemFont(ofSize: 15)]
    let attributedString = NSMutableAttributedString(string:boldText, attributes:attrs)
    
    let normalText = "Hi am normal"
    let normalString = NSMutableAttributedString(string:normalText)
    
    attributedString.append(normalString)
    

    When you want to assign it to a label:

    label.attributedText = attributedString
    

提交回复
热议问题