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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

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

本文將帶你了解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頻道!

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

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

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。