How to make part of a string bold in iOS?

后端 未结 3 2365
栀梦
栀梦 2021-02-20 07:25

I want to make some part of a text string bold.

Eg: This is to be bold. This is normal string.

In Android, it can be easily achieved by using spannable s

3条回答
  •  攒了一身酷
    2021-02-20 08:09

    Swift version:

    @IBOutlet var theLabel: UILabel!
    @IBOutlet var theTextview: UITextView!
    
            let theString = "Please satisfy three of the following password conditions" as NSString
            let theAttributedString = NSMutableAttributedString(string: theString as String)
    
            let boldString = "three"
            let boldRange = theString.range(of: boldString)
            let font = UIFont.boldSystemFont(ofSize: 20)
    
            theAttributedString.addAttribute(NSFontAttributeName, value: font, range: boldRange)
            theLabel.attributedText = theAttributedString
            theTextview.attributedText = theAttributedString
    

提交回复
热议问题