Swift生成屬性文本AttributedString

1.話不多說(shuō),直接上代碼

        let label = UILabel(frame: view.bounds)
        label.textAlignment = .center
        view.addSubview(label)
        
        let myString = "Swift"
        //設(shè)置文字顏色成藍(lán)色,屬性文本別的設(shè)置也幾乎就是在這個(gè)字典中設(shè)置
        let myAttribute = [NSForegroundColorAttributeName: UIColor.blue]
        let myAttrString = NSAttributedString(string: myString, attributes: myAttribute)
        label.attributedText = myAttrString

生成效果:

屏幕快照 2017-05-05 下午2.31.57.png

2.設(shè)置文本屬性

從上面代碼看出,設(shè)置文本的屬性,是通過(guò)myAttribute這個(gè)字典來(lái)實(shí)現(xiàn)的。所以下面一一列舉一些常用的設(shè)置:

1.設(shè)置背景顏色

//設(shè)置成黃色
let myAttribute = [NSBackgroundColorAttributeName: UIColor.yellow]
屏幕快照 2017-05-05 下午2.35.04.png

2.設(shè)置字體和字體大小

let myAttribute = [NSFontAttributeName: UIFont(name: "Chalkduster", size: 18.0)]
屏幕快照 2017-05-05 下午2.39.20.png

3.設(shè)置下劃線

let myAttribute = [ NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleSingle.rawValue ]
屏幕快照 2017-05-05 下午2.40.24.png

4.設(shè)置陰影

let myShadow = NSShadow()
myShadow.shadowBlurRadius = 3
myShadow.shadowOffset = CGSize(width: 3, height: 3)
myShadow.shadowColor = UIColor.gray

let myAttribute = [ NSShadowAttributeName: myShadow ]
屏幕快照 2017-05-05 下午2.40.46.png

3.Attributes創(chuàng)建

文本的屬性可以單獨(dú)一個(gè)一個(gè)的創(chuàng)建,如下:

let singleAttribute1 = [ NSForegroundColorAttributeName: UIColor.green ]
let singleAttribute2 = [ NSBackgroundColorAttributeName: UIColor.yellow ]
let singleAttribute3 = [ NSUnderlineStyleAttributeName: NSUnderlineStyle.styleDouble.rawValue ]

也可以一次創(chuàng)建多個(gè)屬性,因?yàn)閍ttributes是一個(gè)字典

let multipleAttributes: [String : Any] = [
    NSForegroundColorAttributeName: UIColor.green,
    NSBackgroundColorAttributeName: UIColor.yellow,
    NSUnderlineStyleAttributeName: NSUnderlineStyle.styleDouble.rawValue ]

同時(shí),也可以自定義屬性上去

let customAttribute = [ "MyCustomAttributeName": "Some value" ]

或者創(chuàng)建出一個(gè)屬性字典,然后依次添加key-value進(jìn)去

var multipleAttributes = [String : Any]()
multipleAttributes[NSForegroundColorAttributeName] = UIColor.green
multipleAttributes[NSBackgroundColorAttributeName] = UIColor.yellow
multipleAttributes[NSUnderlineStyleAttributeName] = NSUnderlineStyle.styleDouble.rawValue

4.創(chuàng)屬性字符串

1.通過(guò)string創(chuàng)建

let attrString1 = NSAttributedString(string: "Hello.")

2.通過(guò)string創(chuàng)建,同時(shí)附加自定義屬性

let attrString2 = NSAttributedString(string: "Hello.", attributes: ["MyCustomAttribute": "A value"])

3.通過(guò)string和特定的屬性創(chuàng)建

let myAttributes1 = [ NSForegroundColorAttributeName: UIColor.green ]
let attrString3 = NSAttributedString(string: "Hello.", attributes: myAttributes1)

4.如果需要改變屬性或者改變內(nèi)容,則可以創(chuàng)建出可變的屬性字符串

let mutableAttrString1 = NSMutableAttributedString()

5.可變屬性文本使用

首先,創(chuàng)建出可變屬性文本

let myString = "Swift"
        //設(shè)置文字顏色成藍(lán)色,富文本別的設(shè)置也幾乎就是在這個(gè)字典中設(shè)置
        let myAttribute = [ NSForegroundColorAttributeName: UIColor.red ]
        let myAttrString = NSMutableAttributedString(string: myString, attributes: myAttribute)
        label.attributedText = myAttrString

如圖:

屏幕快照 2017-05-05 下午2.59.28.png

然后給屬性文本添加新的樣式的屬性文本

let afterString = NSAttributedString(string: "是Apple力推的語(yǔ)言!")
        myAttrString.append(afterString)
        
        label.attributedText = myAttrString
屏幕快照 2017-05-05 下午3.02.11.png

然后給指定區(qū)間的字符串設(shè)定特定的屬性

        let myRange = NSRange(location: 6, length: 5)
        myAttrString.addAttribute(NSForegroundColorAttributeName, value: UIColor.green, range: myRange)
        
        label.attributedText = myAttrString
屏幕快照 2017-05-05 下午3.05.55.png

可以在已有的屬性基礎(chǔ)上再次添加屬性

        let range = NSRange(location: 0, length: myAttrString.length)
        myAttrString.addAttribute(NSBackgroundColorAttributeName, value: UIColor.yellow, range: range)
        
        label.attributedText = myAttrString
屏幕快照 2017-05-05 下午3.08.53.png

5.添加圖片

使用

        let attachment = NSTextAttachment(data: nil, ofType: nil)
        attachment.image = UIImage(named: "image.png")
        attachment.bounds = CGRect(x: 0, y: 0, width: 20, height: 20)
        let image_text = NSAttributedString(attachment: attachment)
        myAttrString.append(image_text)

6.Swift4的改變

在Swift4下,NSFontAttributeName等等,已經(jīng)被統(tǒng)一成NSAttributedStringKey結(jié)構(gòu)體的類屬性中去了,所以,應(yīng)該寫成NSAttributedStringKey.font。

[NSAttributedStringKey.underlineStyle: NSUnderlineStyle.styleSingle.rawValue]
最后編輯于
?著作權(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)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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