I want half of my UILabel
's text
to be bold and half to not be bold. How can I do this?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
NSAttributedString allows for specification of formatting within a string, but sadly, UIKit does not yet do anything with this. However, there are a few open-source implementations that do. Check out OHAttributedLabel.
回答2:
You can't do this easily unless you subclass UILabel and mess with the subviews. Better to use two UILabels instead. Found another thread about it here.
回答3:
UILabel has attributedText
which takes a NSAttributedString
which allows you to customize multiple aspects of a string including changing the font type and other attributes
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