Bold & Non-Bold Text In A Single UILabel?

后端 未结 14 2429
别那么骄傲
别那么骄傲 2020-11-22 08:51

How would it be possible to include both bold and non-bold text in a uiLabel?

I\'d rather not use a UIWebView.. I\'ve also read this may be possible using NSAttribut

14条回答
  •  一整个雨季
    2020-11-22 09:33

    No need for NSRange with the following code I just implemented in my project (in Swift):

        //Code sets label (yourLabel)'s text to "Tap and hold(BOLD) button to start recording."
        let boldAttribute = [
            //You can add as many attributes as you want here.
            NSFontAttributeName: UIFont(name: "HelveticaNeue-Bold", size: 18.0)!]
    
        let regularAttribute = [
            NSFontAttributeName: UIFont(name: "HelveticaNeue-Light", size: 18.0)!]
    
        let beginningAttributedString = NSAttributedString(string: "Tap and ", attributes: regularAttribute )
        let boldAttributedString = NSAttributedString(string: "hold ", attributes: boldAttribute)
        let endAttributedString = NSAttributedString(string: "button to start recording.", attributes: regularAttribute )
        let fullString =  NSMutableAttributedString()
    
        fullString.appendAttributedString(beginningAttributedString)
        fullString.appendAttributedString(boldAttributedString)
        fullString.appendAttributedString(endAttributedString)
    
        yourLabel.attributedText = fullString
    

提交回复
热议问题