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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

xcode+文字支持html元素,iOS使用UITextview实现富文本编辑

發布時間:2024/9/27 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 xcode+文字支持html元素,iOS使用UITextview实现富文本编辑 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

代碼使用swift4 Xcode9及以上

主要思路是

Textview使用NSMutableAttributedString 轉化為 html字符 給后臺

其中圖片另外 截取htmlStr里相應的圖片標示 組成字典imgs[“標示Str”] = “圖片Base64” 給后臺

再編輯時拿到后臺的 帶圖html字符串轉為NSMutableAttributedString給Textview 使用enumerateAttribute可以獲取圖片

之后再保存和新建的保存差不多

主要注意NSAttachment的使用

attStr.enumerateAttribute(NSAttributedStringKey.attachment, in:NSRange.init(location:0, length:myHaq.attStr.length), options:NSAttributedString.EnumerationOptions(rawValue:0), using: { (value, range, stop)in

if let attachImg = (valueas?NSTextAttachment) {

let img = attachImg.image!

print(img)

imgs += [img]//最終處理 獲取所有圖片

// let attachmentWrapper = attachImg.fileWrapper!

// imgs += [UIImage.init(data: attachmentWrapper.regularFileContents!)!]

}

})

實現富文本字體格式

//加黑

myHaq.texterTypings[NSAttributedStringKey.font.rawValue] = myHaq.isBold ? UIFont.boldSystemFont(ofSize: myHaq.fontSize) : UIFont.systemFont(ofSize: myHaq.fontSize)

//斜體

myHaq.texterTypings[NSAttributedStringKey.obliqueness.rawValue] = sender.tintColor == colorSystem ? 0.25 : 0

//下劃線

myHaq.texterTypings[NSAttributedStringKey.underlineStyle.rawValue] = sender.tintColor == colorSystem ? 1 : 0

//顏色

myHaq.texterTypings[NSAttributedStringKey.foregroundColor.rawValue] = colors[tapG.view!.tag]

//大小

myHaq.texterTypings[NSAttributedStringKey.font.rawValue] = myHaq.isBold ? UIFont.boldSystemFont(ofSize: myHaq.fontSize) : UIFont.systemFont(ofSize: myHaq.fontSize)

save到后臺

let markHtmlStr = myHaq.attStr.toHtmlStr!//轉為html字符串

varimgs = [UIImage]()

myHaq.attStr.enumerateAttribute(NSAttributedStringKey.attachment, in:NSRange.init(location:0, length:myHaq.attStr.length), options:NSAttributedString.EnumerationOptions(rawValue:0), using: { (value, range, stop)in

ifletattachImg = (valueas?NSTextAttachment) {

letimg = attachImg.image!

print(img)

imgs += [img]//最終處理 獲取所有圖片

// let attachmentWrapper = attachImg.fileWrapper!

// imgs += [UIImage.init(data: attachmentWrapper.regularFileContents!)!]

}

})

myHaq.netDic.removeAll()

varimgIdx =0

forstrinmarkHtmlStr.components(separatedBy:"\n") {

print(str)

//str 注意 iOS12 沒有contains("file:") 存儲圖片標示策略需調整

// iOS12之前:

// iOS12:

if str.contains("file:") {//截取轉化后的htmlStr中的所有img標示

varstr1 = str

print(str1)

ifstr.contains("//Attachment") {

str1 =String(str1[str1.range(of:"")!.upperBound])

}else{//網絡圖片 修改時是后臺傳來的圖片地址

str1 =String(str1[str1.range(of:"")!.upperBound])

}

print(str1)

myHaq.netDic[str1] =UIImageJPEGRepresentation(imgs[imgIdx],1.0)!.base64EncodedString(options:NSData.Base64EncodingOptions.lineLength64Characters)

imgIdx +=1

}

}

myHaq.netDic["markHtmlStr"] = markHtmlStr//數據在myHaq.netDic里 發送網絡請求就ok

以上只是截取代碼部分

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

總結

以上是生活随笔為你收集整理的xcode+文字支持html元素,iOS使用UITextview实现富文本编辑的全部內容,希望文章能夠幫你解決所遇到的問題。

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