本文將帶你了解IOS開發入門iOS 開發 富文本詳解之TextKit詳解,希望本文對大家學IOS有所幫助。
textkit結構
textkit使用步驟
#Mark?-?1.?自定義label??--class?CZLabel:?UILabel---四個屬性
//1.屬性文本存儲
private?lazy?var?textStorage?=?NSTextStorage()
//2.負責文本字形布局對象
private?lazy?var?layoutManager?=?NSLayoutManager()
//3.設定文本繪制的范圍
private?lazy?var?textContainer?=?NSTextContainer()
//4.屬性數組,保存匹配的范圍
private?lazy?var?linkRanges?=?[NSRange]()
#Mark?-?2.?重新init方法--?override?init(frame:?CGRect)?{}
//0.開啟用戶交互
userInteractionEnabled?=?true
//1.textStorage接管label的屬性
if?let?attributedText?=?attributedText?{}
//2.設置對象關系
textStorage.addLayoutManager(layoutManager)
layoutManager.addTextContainer(textContainer)
#Mark?-?3.?外界給label的text屬性賦值??label.text?=?@"@好友,#健康#,....."
//重寫屬性的text方法--一旦label里的內容發生變化,就可以讓textStorage相應變化
//1.段落處理--1.范圍??2.屬性??3.段落樣式
let?attrStringM?=?addLineBreak(attributedText!)
//2.正則匹配--1.清空原有??2.匹配范圍??3.創建正則??4.匹配??5.遍歷匹配結果,添加到屬性數組
regexLinkRanges(attrStringM)
//3.連接顏色設置---1.范圍??2.屬性??3.添加顏色??4.遍歷屬性數組,改變顏色
addLinkAttribute(attrStringM)
//4.添加到textStorage
textStorage.setAttributedString(attrStringM)
//5.重新繪制
setNeedsDisplay()
#Mark?-?4.?textStorage字形和屬性發生變化時,通知NSLayoutManager重新布局文本
//MARK:3.設置布局--制定文本繪制區域
override?func?layoutSubviews()?{
super.layoutSubviews()
//制定文本繪制區域
textContainer.size?=?bounds.size
}
#Mark?-?5.?繪制textStorage的文本內容--不能調用super
override?func?drawTextInRect(rect:?CGRect)?{
let?range?=?NSMakeRange(0,?textStorage.length)
//Glyphs--字形---CGPoint()從原點繪制,也就是右上角
layoutManager.drawGlyphsForGlyphRange(range,?atPoint:?CGPoint(x:?0,y:?0))
}
#Mark?-?6.?用戶點擊事件交互
//0.懶加載@?#?URL的匹配的正則法則?三個屬性數組
三步法:1.正則表達式??2.創建正則??3.匹配??4.便利匹配結果,添加到屬性數組
//1.獲取用戶點擊的位置
let?location?=?touches.first?.locationInView(self)
//2.獲取當前點中字符的索引
let?index?=?layoutManager.glyphIndexForPoint(location,?inTextContainer:?textContainer)
//3.判斷index在哪個標記的range?范圍上
for?range?in?atRange????[]?{
if?NSLocationInRange(index,?range)?{
let?strSub?=?(textStorage.string?as?NSString).substringWithRange(range)
//進行結果處理
}
}
Swift使用
import?UIKit
class?ZYLabel:?UILabel?{????????//attributedText富文本
//MARK:2.重寫屬性text方法,可以在ViewController里給文本賦值
//一旦label里的內容發生變化,就可以讓textStorage相應變化
override?var?text:String??{
didSet?{
if?attributedText?==?nil?{
return
}
//換行處理屬性
let?attrStringM?=?addLineBreak(attributedText!)
//換行后進行--正則匹配
regexLinkRanges(attrStringM)
//換行后進行--連接顏色設置
addLinkAttribute(attrStringM)
//添加到textStorage
textStorage.setAttributedString(attrStringM)
//重新繪制
setNeedsDisplay()
}
}
///MARK:?textKit的三個核心對象
//屬性文本存儲
private?lazy?var?textStorage?=?NSTextStorage()
//負責文本字形布局對象
private?lazy?var?layoutManager?=?NSLayoutManager()
//設定文本繪制的范圍
private?lazy?var?textContainer?=?NSTextContainer()
private?lazy?var?linkRanges?=?[NSRange]()
//純代碼接管Label
override?init(frame:?CGRect)?{
super.init(frame:?frame)
//0.開啟用戶交互
userInteractionEnabled?=?true
//1.textStorage接管label的屬性
if?let?attributedText?=?attributedText?{????????//如果原有文本設置了attribute
textStorage.setAttributedString(attributedText)
}else?if?let?text?=?text?{??????//如果原有文本沒有設置attribute
textStorage.setAttributedString(NSAttributedString(string:?text))
}else?{?????//如果原有文本為nil
textStorage.setAttributedString(NSAttributedString(string:?""))
}
//2.設置對象關系
textStorage.addLayoutManager(layoutManager)
本文由職坐標整理并發布,希望對同學們有所幫助。了解更多詳情請關注職坐標移動開發之IOS頻道!