最近整理了一下CoreText里面的相關(guān)知識點(diǎn),現(xiàn)在剛看到NSAttributedString,也從網(wǎng)上查到了一些資料,不過總感覺有很多文章只是對于Apple Document的翻譯,對于初學(xué)者來說,可能更想要知道落實(shí)到代碼是什么?每一個屬性又是做什么的。廢話不多少,發(fā)車了!
一:源代碼
github地址:NSAttributedString。喜歡的麻煩給顆星??
二:屬性列表
NSAttributedString屬性列表如下:
| 屬性名稱 | 意義 |
|---|---|
| font | 字體 |
| paragraphStyle | 段落設(shè)置,默認(rèn)是:defaultParagraphStyle |
| foregroundColor | 文字顏色,默認(rèn)是黑色的 |
| backgroundColor | 背景顏色,默認(rèn)是沒有背景顏色 |
| ligature | 網(wǎng)上說是連字功能,默認(rèn)是關(guān)閉的,我試了一下,沒啥用,可能沒有看到可以連字的字母吧 |
| kern | 字符間距,0表示字符間距不可用 |
| strikethroughStyle | 刪除線的類型,只有0(無刪除線)1(有刪除線)兩種樣式 |
| underlineStyle | 下劃線樣式0(沒有下劃線),1(有下劃線) |
| strokeColor | 文字的描邊顏色,默認(rèn)是沒有描邊顏色,也就是背景顏色 |
| strokeWidth | 文字的描邊寬度,默認(rèn)是0,沒有描邊 |
| shadow | 陰影,默認(rèn)是沒有陰影的 |
| textEffect | 文字效果,默認(rèn)沒有文字效果 |
| attachment | 附件 |
| link | 鏈接 |
| baselineOffset | 基線的偏移量 |
| underlineColor | 下劃線顏色,默認(rèn)是nil,背景顏色 |
| strikethroughColor | 刪除線顏色,默認(rèn)是nil,背景顏色 |
| obliqueness | 字體傾斜度,默認(rèn)是沒有傾斜度 |
| expansion | 字體的伸縮,小于零是縮小,大于零是擴(kuò)張 |
| writingDirection | 字體的方向,有LRE, RLE, LRO, 和RLO幾種模式,現(xiàn)在還不知道怎么賦值。。。。。( ˇ?ˇ ) |
| verticalGlyphForm | 文字的排版方向 |
三:效果示意圖和代碼實(shí)現(xiàn)
再說一遍,先把上面的demo下載下來:NSAttributedString,下面的代碼都在demo的代碼里面
3.1:font

字體設(shè)定
具體實(shí)現(xiàn)代碼如下:
//代碼位置:BoldSystemFontViewController.swift
func refreshLabel(font:UIFont){
let dict = [NSAttributedStringKey.font:font]
let attrStr = NSMutableAttributedString.init(string: self.titleStr, attributes: dict)
label.attributedText = attrStr
}
3.2:paragraphStyle

paragraphStyle
//代碼位置:ParagraphStyleViewController.swift
func refreshLabel(index:Int){
let style = NSMutableParagraphStyle.init()
switch index {
case 0:
style.lineSpacing = 20.0
break
case 1:
style.paragraphSpacing = 20.0
break
case 2:
style.alignment = .right
break
case 3:
style.firstLineHeadIndent = 20.0
break
case 4:
style.headIndent = 20.0
break
case 5:
style.tailIndent = 20.0
break
case 6:
style.lineBreakMode = .byWordWrapping
break
case 7:
style.minimumLineHeight = 10
break
case 8:
style.maximumLineHeight = 20
break
case 9:
style.baseWritingDirection = .rightToLeft
break
case 10:
style.lineHeightMultiple = 3.0
break
case 11:
style.paragraphSpacingBefore = 20.0
break
case 12:
style.hyphenationFactor = 20.0
break
case 13:
let decimalTab = NSTextTab.init(textAlignment: .right, location: 5, options: [:])
let percentTab = NSTextTab.init(textAlignment: .left, location: 20, options: [:])
style.tabStops = [decimalTab,percentTab]
break
case 14:
style.defaultTabInterval = .infinity*2
break
case 15:
style.allowsDefaultTighteningForTruncation = true
break
case 16:
let decimalTab = NSTextTab.init(textAlignment: .right, location: 5, options: [:])
style.addTabStop(decimalTab)
break
case 17:
let decimalTab = NSTextTab.init(textAlignment: .right, location: 5, options: [:])
style.removeTabStop(decimalTab)
break
case 18:
let newStyle = NSMutableParagraphStyle.init()
newStyle.lineSpacing = 30
style.setParagraphStyle(newStyle)
break
default:
break
}
let dict = [NSAttributedStringKey.paragraphStyle:style]
let attrStr = NSMutableAttributedString.init(string: self.titleStr, attributes: dict)
label.attributedText = attrStr
}
3.3:ForeGroundColor

ForeGroundColor
//代碼位置:ForegroundColorViewController.swift
func refreshLabel(color:UIColor){
let dict =
[NSAttributedStringKey.foregroundColor:color]
let attrStr = NSMutableAttributedString.init(string: self.titleStr, attributes: dict)
label.attributedText = attrStr
}
3.4:backgroundColor

backgroundColor
//BackgroundColorViewController.swift
func refreshLabel(color:UIColor){
let dict = [NSAttributedStringKey.backgroundColor:color]
let attrStr = NSMutableAttributedString.init(string: self.titleStr, attributes: dict)
label.attributedText = attrStr
}
3.5:ligature

ligature
//代碼位置:LigatureViewController.swift
func refreshLabel(index:Int){
let dict = [NSAttributedStringKey.ligature:index]
let attrStr = NSMutableAttributedString.init(string: self.titleStr + pinyin, attributes: dict)
label.attributedText = attrStr
}
3.6:kern

kern
//KernViewController.swift
func refreshLabel(index:Int){
let dict = [NSAttributedStringKey.kern:index]
let attrStr = NSMutableAttributedString.init(string: self.titleStr, attributes: dict)
label.attributedText = attrStr
}
3.7:strikethroughStyle

strikethroughStyle
//代碼位置:StrikethroughStyleViewController.swift
func refreshLabel(index:Int){
let dict = [NSAttributedStringKey.strikethroughStyle:index]
let attrStr = NSMutableAttributedString.init(string: self.titleStr, attributes: dict)
label.attributedText = attrStr
}
3.8:UnderlineStyle

UnderlineStyle
//UnderlineStyleViewController.swift
func refreshLabel(index:Int){
let dict = [NSAttributedStringKey.underlineStyle:index]
let attrStr = NSMutableAttributedString.init(string: self.titleStr, attributes: dict)
label.attributedText = attrStr
}
3.9:StrokeColor

StrokeColor
//代碼位置:StrokeColorViewController.swift
func refreshLabel(color:UIColor){
let dict = [NSAttributedStringKey.strokeColor:color,NSAttributedStringKey.strokeWidth:1] as [NSAttributedStringKey : Any]
let attrStr = NSMutableAttributedString.init(string: self.titleStr, attributes: dict)
label.attributedText = attrStr
}
3.10:StrokeWidth

StrokeWidth
//代碼位置:StrokeWidthViewController.swift
func refreshLabel(width:CGFloat){
let dict = [NSAttributedStringKey.strokeColor:UIColor.red,NSAttributedStringKey.strokeWidth:width] as [NSAttributedStringKey : Any]
let attrStr = NSMutableAttributedString.init(string: self.titleStr, attributes: dict)
label.attributedText = attrStr
}
3.11:shadow

shadow
//代碼位置:ShadowViewController.swift
func refreshLabel(){
let red = CGFloat(arc4random_uniform(255))/CGFloat(255.0)
let green = CGFloat( arc4random_uniform(255))/CGFloat(255.0)
let blue = CGFloat(arc4random_uniform(255))/CGFloat(255.0)
let alpha = CGFloat(arc4random_uniform(255))/CGFloat(255.0)
let color = UIColor.init(red:red, green:green, blue:blue , alpha: alpha)
let shadowBlurRadius = arc4random_uniform(5)
let offsetX = arc4random_uniform(2)
let offsetY = arc4random_uniform(5)
let shadow = NSShadow.init()
shadow.shadowOffset = CGSize(width: CGFloat(offsetX), height:CGFloat(offsetY))
shadow.shadowBlurRadius = CGFloat(shadowBlurRadius)
shadow.shadowColor = color
let dict = [NSAttributedStringKey.shadow:shadow]
let attrStr = NSMutableAttributedString.init(string: self.titleStr, attributes: dict)
label.attributedText = attrStr
}
3.12:textEffect

textEffect
感覺這個操作有點(diǎn)卡。。。等有時間了看看咋回事
//代碼位置:TextEffectViewController.swift
func refreshLabel(){
let dict = [NSAttributedStringKey.textEffect:NSAttributedString.TextEffectStyle.letterpressStyle]
let attrStr = NSMutableAttributedString.init(string: self.titleStr, attributes: dict)
label.attributedText = attrStr
}
3.13:Attachment

Attachment
//代碼位置:AttachmentViewController.swift
func refreshLabel(index:Int){
let attach = NSTextAttachment.init()
switch index {
case 0:
attach.contents = Data.init(base64Encoded: "zhangfangtao", options: .ignoreUnknownCharacters)
break
case 1:
attach.fileType = "txt"
break
case 2:
attach.image = UIImage.init(named: "icon")
break
case 3:
attach.bounds = CGRect(x: 0, y: 0, width: 50, height: 50)
break
case 4:
let filewrapper = FileWrapper.init(symbolicLinkWithDestinationURL: URL.init(string: "www.baidu.com")!)
attach.fileWrapper = filewrapper
default:
break
}
attach.bounds = CGRect(x: 0, y: 0, width: 50, height: 50)
let imageStr = NSAttributedString.init(attachment: attach)
let attrStr = NSMutableAttributedString.init(string: self.titleStr)
attrStr.insert(imageStr, at: 6)
label.attributedText = attrStr
}
3.14:Link

Link
//代碼位置:LinkViewController.swift
func refreshLabel(){
let dict = [NSAttributedStringKey.link:URL.init(string: "www.baidu.com")]
let attrStr = NSMutableAttributedString.init(string: self.titleStr, attributes: dict)
label.attributedText = attrStr
}
3.15:BaselineOffset

BaselineOffset
//代碼位置:BaselineOffsetViewController.swift
func refreshLabel(index:Int){
let dict = [NSAttributedStringKey.baselineOffset:self.offsetArray[index]]
let attrStr = NSMutableAttributedString.init(string: self.titleStr, attributes: dict)
label.attributedText = attrStr
}
3.16:UnderlineColor

UnderlineColor
//代碼位置:UnderlineColorViewController.swift
func refreshLabel(color:UIColor){
let dict = [NSAttributedStringKey.underlineStyle:1,NSAttributedStringKey.underlineColor:color] as [NSAttributedStringKey : Any]
let attrStr = NSMutableAttributedString.init(string: self.titleStr, attributes: dict)
label.attributedText = attrStr
}
3.17:StrikethroughColor

StrikethroughColor
//代碼位置:StrikethroughColorViewController.swift
func refreshLabel(color:UIColor){
let dict = [NSAttributedStringKey.strikethroughStyle:1,NSAttributedStringKey.strikethroughColor:color] as [NSAttributedStringKey : Any]
let attrStr = NSMutableAttributedString.init(string: self.titleStr, attributes: dict)
label.attributedText = attrStr
}
3.18:ObliquenessView

ObliquenessView
//代碼位置:ObliquenessViewController.swift
func refreshLabel(index:Float){
let dict = [NSAttributedStringKey.obliqueness:index]
let attrStr = NSMutableAttributedString.init(string: self.titleStr, attributes: dict)
label.attributedText = attrStr
}
3.19:ExpansionView

ExpansionView
//代碼位置:ExpansionViewController.swift
func refreshLabel(index:Float){
let dict = [NSAttributedStringKey.expansion:index]
let attrStr = NSMutableAttributedString.init(string: self.titleStr, attributes: dict)
label.attributedText = attrStr
}
3.20:WritingDirectionView

WritingDirectionView
這個還有些小問題,近期也沒時間找答案了,怎么賦值?麻煩知道的人留言告訴一下唄。
//代碼位置:WritingDirectionViewController.swift
func refreshLabel(index:NSNumber){
let alertController = UIAlertController.init(title: "這個我目前還不知道怎么設(shè)置", message: "正在查詢相關(guān)的資料", preferredStyle: .alert)
let cancelAction = UIAlertAction(title: "取消", style: UIAlertActionStyle.cancel, handler: nil)
let okAction = UIAlertAction(title: "好的", style: UIAlertActionStyle.default, handler: nil)
alertController.addAction(cancelAction)
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)
return
let dict = [NSAttributedStringKey.writingDirection:index]
let attrStr = NSMutableAttributedString.init(string: self.titleStr, attributes: dict)
label.attributedText = attrStr
}
3.21:VerticalGlyphForm

VerticalGlyphForm
//代碼位置:VerticalGlyphFormViewController.swift
func refreshLabel(index:Float){
let dict = [NSAttributedStringKey.verticalGlyphForm:index]
let attrStr = NSMutableAttributedString.init(string: self.titleStr, attributes: dict)
label.attributedText = attrStr
}
寫在最后
時間有限,很多細(xì)節(jié)還沒有接觸到,如果有入門想了解的可以看看。源代碼github地址:NSAttributedString。喜歡的麻煩給顆星??。