UITextView 设置同一段文字,多个文字颜色

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qin_shi/article/details/81132551

利用NSMutableAttributedString实现

预览图如下
这里写图片描述

  • 创建 NSAttributedString
 let subjectString1 = NSAttributedString(string: "CLUD:  ", attributes: [NSAttributedStringKey.foregroundColor: UIColor.white, NSAttributedStringKey.font: UIFont.init(name: "Quicksand-Bold", size: ScreenUtil.getRelativeWidth(22))])
 var subjectString2 = NSAttributedString(string: "HAVE FUN", attributes: [NSAttributedStringKey.foregroundColor: UIColor.init(hexString: "#FBE85B"), NSAttributedStringKey.font: UIFont.init(name: "Quicksand-Bold", size: ScreenUtil.getRelativeWidth(22))])
  • 创建 NSMutableAttributedString
var message = NSMutableAttributedString(attributedString: subjectString1)
        message.append(subjectString2)
  • 设置 attributedText
var textView = UITextView()
textView.attributedText = message

猜你喜欢

转载自blog.csdn.net/qin_shi/article/details/81132551