日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ios开发text kit_IOS开发入门之TextKit详解

發(fā)布時(shí)間:2023/12/10 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ios开发text kit_IOS开发入门之TextKit详解 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

本文將帶你了解IOS開發(fā)入門iOS 開發(fā) 富文本詳解之TextKit詳解,希望本文對大家學(xué)IOS有所幫助。

textkit結(jié)構(gòu)

textkit使用步驟

#Mark?-?1.?自定義label??--class?CZLabel:?UILabel---四個(gè)屬性

//1.屬性文本存儲(chǔ)

private?lazy?var?textStorage?=?NSTextStorage()

//2.負(fù)責(zé)文本字形布局對象

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.開啟用戶交互

userInteractionEnabled?=?true

//1.textStorage接管label的屬性

if?let?attributedText?=?attributedText?{}

//2.設(shè)置對象關(guān)系

textStorage.addLayoutManager(layoutManager)

layoutManager.addTextContainer(textContainer)

#Mark?-?3.?外界給label的text屬性賦值??label.text?=?@"@好友,#健康#,....."

//重寫屬性的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.重寫屬性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è)核心對象

//屬性文本存儲(chǔ)

private?lazy?var?textStorage?=?NSTextStorage()

//負(fù)責(zé)文本字形布局對象

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.開啟用戶交互

userInteractionEnabled?=?true

//1.textStorage接管label的屬性

if?let?attributedText?=?attributedText?{????????//如果原有文本設(shè)置了attribute

textStorage.setAttributedString(attributedText)

}else?if?let?text?=?text?{??????//如果原有文本沒有設(shè)置attribute

textStorage.setAttributedString(NSAttributedString(string:?text))

}else?{?????//如果原有文本為nil

textStorage.setAttributedString(NSAttributedString(string:?""))

}

//2.設(shè)置對象關(guān)系

textStorage.addLayoutManager(layoutManager)

本文由職坐標(biāo)整理并發(fā)布,希望對同學(xué)們有所幫助。了解更多詳情請關(guān)注職坐標(biāo)移動(dòng)開發(fā)之IOS頻道!

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)

總結(jié)

以上是生活随笔為你收集整理的ios开发text kit_IOS开发入门之TextKit详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。