ios开发text kit_IOS开发入门之TextKit详解
本文將帶你了解IOS開(kāi)發(fā)入門(mén)iOS 開(kāi)發(fā) 富文本詳解之TextKit詳解,希望本文對(duì)大家學(xué)IOS有所幫助。
textkit結(jié)構(gòu)
textkit使用步驟
#Mark?-?1.?自定義label??--class?CZLabel:?UILabel---四個(gè)屬性
//1.屬性文本存儲(chǔ)
private?lazy?var?textStorage?=?NSTextStorage()
//2.負(fù)責(zé)文本字形布局對(duì)象
private?lazy?var?layoutManager?=?NSLayoutManager()
//3.設(shè)定文本繪制的范圍
private?lazy?var?textContainer?=?NSTextContainer()
//4.屬性數(shù)組,保存匹配的范圍
private?lazy?var?linkRanges?=?[NSRange]()
#Mark?-?2.?重新init方法--?override?init(frame:?CGRect)?{}
//0.開(kāi)啟用戶交互
userInteractionEnabled?=?true
//1.textStorage接管label的屬性
if?let?attributedText?=?attributedText?{}
//2.設(shè)置對(duì)象關(guān)系
textStorage.addLayoutManager(layoutManager)
layoutManager.addTextContainer(textContainer)
#Mark?-?3.?外界給label的text屬性賦值??label.text?=?@"@好友,#健康#,....."
//重寫(xiě)屬性的text方法--一旦label里的內(nèi)容發(fā)生變化,就可以讓textStorage相應(yīng)變化
//1.段落處理--1.范圍??2.屬性??3.段落樣式
let?attrStringM?=?addLineBreak(attributedText!)
//2.正則匹配--1.清空原有??2.匹配范圍??3.創(chuàng)建正則??4.匹配??5.遍歷匹配結(jié)果,添加到屬性數(shù)組
regexLinkRanges(attrStringM)
//3.連接顏色設(shè)置---1.范圍??2.屬性??3.添加顏色??4.遍歷屬性數(shù)組,改變顏色
addLinkAttribute(attrStringM)
//4.添加到textStorage
textStorage.setAttributedString(attrStringM)
//5.重新繪制
setNeedsDisplay()
#Mark?-?4.?textStorage字形和屬性發(fā)生變化時(shí),通知NSLayoutManager重新布局文本
//MARK:3.設(shè)置布局--制定文本繪制區(qū)域
override?func?layoutSubviews()?{
super.layoutSubviews()
//制定文本繪制區(qū)域
textContainer.size?=?bounds.size
}
#Mark?-?5.?繪制textStorage的文本內(nèi)容--不能調(diào)用super
override?func?drawTextInRect(rect:?CGRect)?{
let?range?=?NSMakeRange(0,?textStorage.length)
//Glyphs--字形---CGPoint()從原點(diǎn)繪制,也就是右上角
layoutManager.drawGlyphsForGlyphRange(range,?atPoint:?CGPoint(x:?0,y:?0))
}
#Mark?-?6.?用戶點(diǎn)擊事件交互
//0.懶加載@?#?URL的匹配的正則法則?三個(gè)屬性數(shù)組
三步法:1.正則表達(dá)式??2.創(chuàng)建正則??3.匹配??4.便利匹配結(jié)果,添加到屬性數(shù)組
//1.獲取用戶點(diǎn)擊的位置
let?location?=?touches.first?.locationInView(self)
//2.獲取當(dāng)前點(diǎn)中字符的索引
let?index?=?layoutManager.glyphIndexForPoint(location,?inTextContainer:?textContainer)
//3.判斷index在哪個(gè)標(biāo)記的range?范圍上
for?range?in?atRange????[]?{
if?NSLocationInRange(index,?range)?{
let?strSub?=?(textStorage.string?as?NSString).substringWithRange(range)
//進(jìn)行結(jié)果處理
}
}
Swift使用
import?UIKit
class?ZYLabel:?UILabel?{????????//attributedText富文本
//MARK:2.重寫(xiě)屬性text方法,可以在ViewController里給文本賦值
//一旦label里的內(nèi)容發(fā)生變化,就可以讓textStorage相應(yīng)變化
override?var?text:String??{
didSet?{
if?attributedText?==?nil?{
return
}
//換行處理屬性
let?attrStringM?=?addLineBreak(attributedText!)
//換行后進(jìn)行--正則匹配
regexLinkRanges(attrStringM)
//換行后進(jìn)行--連接顏色設(shè)置
addLinkAttribute(attrStringM)
//添加到textStorage
textStorage.setAttributedString(attrStringM)
//重新繪制
setNeedsDisplay()
}
}
///MARK:?textKit的三個(gè)核心對(duì)象
//屬性文本存儲(chǔ)
private?lazy?var?textStorage?=?NSTextStorage()
//負(fù)責(zé)文本字形布局對(duì)象
private?lazy?var?layoutManager?=?NSLayoutManager()
//設(shè)定文本繪制的范圍
private?lazy?var?textContainer?=?NSTextContainer()
private?lazy?var?linkRanges?=?[NSRange]()
//純代碼接管Label
override?init(frame:?CGRect)?{
super.init(frame:?frame)
//0.開(kāi)啟用戶交互
userInteractionEnabled?=?true
//1.textStorage接管label的屬性
if?let?attributedText?=?attributedText?{????????//如果原有文本設(shè)置了attribute
textStorage.setAttributedString(attributedText)
}else?if?let?text?=?text?{??????//如果原有文本沒(méi)有設(shè)置attribute
textStorage.setAttributedString(NSAttributedString(string:?text))
}else?{?????//如果原有文本為nil
textStorage.setAttributedString(NSAttributedString(string:?""))
}
//2.設(shè)置對(duì)象關(guān)系
textStorage.addLayoutManager(layoutManager)
本文由職坐標(biāo)整理并發(fā)布,希望對(duì)同學(xué)們有所幫助。了解更多詳情請(qǐng)關(guān)注職坐標(biāo)移動(dòng)開(kāi)發(fā)之IOS頻道!
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的ios开发text kit_IOS开发入门之TextKit详解的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 实例27:python
- 下一篇: 8.26打架被批判一番还是要学习一个