how to append Attributed Text String with Attributed String in Swift

后端 未结 4 1268
天涯浪人
天涯浪人 2021-01-31 01:52

I want to append an Attributed Text with another Attributed Text in Swift. Please provide any sample code for appending operation of two attributed String in Swift.

4条回答
  •  再見小時候
    2021-01-31 02:31

    Swift 5

    As per "glace" answer, I just update font attribute and swift version.

        let boldFontAttributes = [NSAttributedString.Key.foregroundColor: UIColor.black, NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 17)]
        let normalFontAttributes = [NSAttributedString.Key.foregroundColor: UIColor.darkGray, NSAttributedString.Key.font: UIFont.systemFont(ofSize: 15)]
        let partOne = NSMutableAttributedString(string: "This is an example ", attributes: boldFontAttributes)
        let partTwo = NSMutableAttributedString(string: "for the combination of Attributed String!", attributes: normalFontAttributes)
    
        let combination = NSMutableAttributedString()
        
        combination.append(partOne)
        combination.append(partTwo)
        lblUserName.attributedText = combination
    

提交回复
热议问题