美文网首页
IOS UILab swift富文本 字符间距颜色

IOS UILab swift富文本 字符间距颜色

作者: 简书_旋 | 来源:发表于2017-12-05 16:33 被阅读0次

    let  errStr = "订课成功\r\n开课前2小时内不允许取消订课"

    let strc = NSMutableAttributedString.init(string: errStr)

    //间距

    let paragraphStyle = NSMutableParagraphStyle.init()

    paragraphStyle.lineSpacing = 10

    paragraphStyle.alignment = .center

    strc.addAttributes([NSAttributedStringKey.paragraphStyle : paragraphStyle], range: NSRange.init(location: 0, length: strc.length))

    strc.addAttributes([NSAttributedStringKey.foregroundColor : UIColor.red], range: NSRange.init(location: 0, length: 4))

    let str1M = NSAttributedString.init(string: "开课前2小时内不允许取消订课", attributes: [NSAttributedStringKey.foregroundColor:UIColor.yellow,NSAttributedStringKey.font:UIFont.systemFont(ofSize: 13)])

    strc.replaceCharacters(in: NSRange.init(location: 6, length: 14), with: str1M)

    最终得到的 strc 就是 一个可控各部分文字颜色 换行 对齐 的富文本

    相关文章

      网友评论

          本文标题:IOS UILab swift富文本 字符间距颜色

          本文链接:https://www.haomeiwen.com/subject/tnucixtx.html