iOS使用UITextview實(shí)現(xiàn)富文本編輯

代碼使用swift4 Xcode9及以上

主要思路是

Textview使用NSMutableAttributedString 轉(zhuǎn)化為 html字符 給后臺(tái)

其中圖片另外 截取htmlStr里相應(yīng)的圖片標(biāo)示 組成字典imgs[“標(biāo)示Str”] = “圖片Base64” 給后臺(tái)

再編輯時(shí)拿到后臺(tái)的 帶圖html字符串轉(zhuǎn)為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!)!]
             }
          })

實(shí)現(xiàn)富文本字體格式

 //加黑
 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網(wǎng)絡(luò)

         let markHtmlStr = myHaq.attStr.toHtmlStr!//轉(zhuǎn)為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!)!]
 
             }
 
         })
 
         varimgIdx =0
         forstrinmarkHtmlStr.components(separatedBy:"><") {
             if str.contains("file:") {//截取轉(zhuǎn)化后的htmlStr中的所有img標(biāo)示
                 varstr1 = str
                 print(str1)
                 ifstr.contains("http://Attachment") {
                     str1 = String(str1[str1.range(of: "img src")!.lowerBound..<str1.range(of: ".png")!.upperBound])
                 }else{//網(wǎng)絡(luò)圖片 修改時(shí)是后臺(tái)傳來(lái)的圖片地址
                     str1 = String(str1[str1.range(of: "img src")!.lowerBound..<str1.range(of: ".jpg")!.upperBound])
                 }
                 print(str1)
                 
                    DCNetSir.upIconImg(imgs[imgIdx]) { [self] fileUrl in
                        markHtmlStr = markHtmlStr.replacingOccurrences(of: str1, with: "img src=\"\(fileUrl)")//用上傳后的圖片url替換html字符串里圖片標(biāo)識(shí)字符

                        upCount += 1
                        if imgs.count == upCount {//上傳圖片完成計(jì)數(shù) 上傳所有后保存html字符串
                            saveDo()
                        }
                    }

                 imgIdx +=1
             }
         }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1、通過(guò)CocoaPods安裝項(xiàng)目名稱項(xiàng)目信息 AFNetworking網(wǎng)絡(luò)請(qǐng)求組件 FMDB本地?cái)?shù)據(jù)庫(kù)組件 SD...
    陽(yáng)明AI閱讀 16,186評(píng)論 3 119
  • 03199陳俊玲 《朱家故事》這一個(gè)故事,我一直認(rèn)為是有一定的趣味性和一定的“科幻性”,在這種趣味和科幻中讓我們讀...
    郡靈靈閱讀 452評(píng)論 0 1
  • 夏天,是炎熱的。走出家門,只見(jiàn)鄰居家的小狗懶洋洋地躺在地上,伸出舌頭,仿佛在說(shuō):“今天實(shí)在太熱了?!弊咴?..
    小呆呆的煩惱閱讀 344評(píng)論 0 1
  • "but in the long run we can try to change the norms where...
    011000閱讀 309評(píng)論 0 0
  • 我追索人心的深度,卻看到了人心的淺薄。從前的那個(gè)我,如果找到現(xiàn)在的我,會(huì)得到很好的款待。歲月不饒人,我亦未曾饒過(guò)歲...
    艾小墨閱讀 647評(píng)論 0 0

友情鏈接更多精彩內(nèi)容