Have half a UILabel text bold and half not?

浪尽此生 提交于 2019-12-24 11:40:18

问题


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


来源:https://stackoverflow.com/questions/7180805/have-half-a-uilabel-text-bold-and-half-not

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!