Swift-富文本使用

image.png

定義公用的屬性

        let str = "我不要種田 我要當(dāng)老板"
        let attrStr = NSMutableAttributedString.init(string: str)

修改顏色(可多段)

// MARK: - 修改顏色
        let textLabel = UILabel.init(frame: CGRect.init(x: 20, y: 100, width: UIScreen.main.bounds.size.width - 40, height: 30))
        textLabel.textColor = UIColor.red
        view.addSubview(textLabel)
        attrStr.addAttribute(NSAttributedStringKey.foregroundColor, value:UIColor.orange, range:NSRange.init(location:0, length: 5))
        attrStr.addAttribute(NSAttributedStringKey.foregroundColor, value:UIColor.red, range:NSRange.init(location:6, length: 5))
        textLabel.attributedText = attrStr

刪除線

 // MARK: - 刪除線
        let textLabel2 = UILabel.init(frame: CGRect.init(x: 20, y: 130, width: UIScreen.main.bounds.size.width - 40, height: 30))
        textLabel2.textColor = UIColor.red
        view.addSubview(textLabel2)
        attrStr.addAttribute(NSAttributedStringKey.strikethroughStyle, value: NSNumber.init(value: 1), range: NSRange.init(location:0, length: str.count))
        textLabel2.attributedText = attrStr

文字大小

// MARK: - 文字大小
        let textLabel3 = UILabel.init(frame: CGRect.init(x: 20, y: 160, width: UIScreen.main.bounds.size.width - 40, height: 30))
        textLabel3.textColor = UIColor.red
        view.addSubview(textLabel3)
        textLabel3.attributedText = NSAttributedString.init(string: str, attributes: [NSAttributedStringKey.font:UIFont.systemFont(ofSize:28)])

字體顏色

// MARK: - 字體顏色
        let textLabel4 = UILabel.init(frame: CGRect.init(x: 20, y: 190, width: UIScreen.main.bounds.size.width - 40, height: 30))
        textLabel4.textColor = UIColor.red
        view.addSubview(textLabel4)
        textLabel4.attributedText =  NSAttributedString.init(string:str, attributes: [NSAttributedStringKey.foregroundColor:UIColor.blue])

背景色

// MARK: - 背景色
        let textLabel5 = UILabel.init(frame: CGRect.init(x: 20, y: 220, width: UIScreen.main.bounds.size.width - 40, height: 30))
        textLabel5.textColor = UIColor.red
        view.addSubview(textLabel5)
        textLabel5.attributedText =  NSAttributedString.init(string:str, attributes: [NSAttributedStringKey.backgroundColor:UIColor.green])

陰影

// MARK: - 陰影
        let textLabel6 = UILabel.init(frame: CGRect.init(x: 20, y: 250, width: UIScreen.main.bounds.size.width - 40, height: 30))
        textLabel6.textColor = UIColor.red
        view.addSubview(textLabel6)
        let shadow = NSShadow.init()
        shadow.shadowColor = UIColor.red
        shadow.shadowOffset = CGSize.init(width: 2, height: 2)
        textLabel6.attributedText =  NSAttributedString.init(string:str, attributes: [NSAttributedStringKey.foregroundColor:UIColor.red,  NSAttributedStringKey.font:UIFont.systemFont(ofSize:18), NSAttributedStringKey.shadow: shadow])

下劃線

// MARK: - 下劃線
        let textLabel7 = UILabel.init(frame: CGRect.init(x: 20, y: 280, width: UIScreen.main.bounds.size.width - 40, height: 30))
        textLabel7.textColor = UIColor.red
        view.addSubview(textLabel7)
        textLabel7.attributedText =  NSAttributedString.init(string:str, attributes: [NSAttributedStringKey.foregroundColor:UIColor.purple,  NSAttributedStringKey.font:UIFont.systemFont(ofSize:18), NSAttributedStringKey.underlineStyle:NSUnderlineStyle.styleSingle.rawValue])

Dome

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容