iOS富文本以及與后臺的交互問題

雖然對iOS工作有幾年了但是沒怎么寫過文章,一直覺得自己技術(shù)還不夠,寫文章出來有點丟人現(xiàn)眼,但是發(fā)現(xiàn)有關(guān)iOS富文本編輯這塊網(wǎng)上的資料比較少所以想寫篇文章分享下自己的心得。

下面是公司要求實現(xiàn)的符文編輯的效果界面

公司要求實現(xiàn)的界面

當時網(wǎng)上調(diào)研了很多發(fā)現(xiàn)有轉(zhuǎn)html的也有直接原生的,一開始我比較欣賞的的例子是專程html與后臺進行交互的貼上地址,還是比較詳細的http://www.itdecent.cn/p/b2a0528659bd

但是因為還有個安卓端的開發(fā)所以我也會 征求下安卓端的意見,他們對于html的不是很滿意,安卓對于webview的實現(xiàn)性能比較差,而且還要考慮以后的維護工作,所以最后討論下來的結(jié)果是,移動端轉(zhuǎn)成json文本傳輸給后臺具體結(jié)構(gòu)如下

{

"dataList": [

{

"type": 1,

"content": "我是第一條文本",

"imageWidth": 0,

"imageHeight": 0

},

{

"type": 1,

"content": "我是第二條文本",

"imgWidth": 0,

"imgHeight": 0

},

{

"type": 2,

"content": "http://img.baidu.com/xxx1.jpg",

"imgWidth": 200,

"imgHeight": 200

},

{

"type": 1,

"content": "我是第三條文本",

"imgWidth": 0,

"imgHeight": 0

},

{

"type": 2,

"content": "http://img.baidu.com/xxx2.jpg",

"imgWidth": 200,

"imgHeight": 200

}

]

}


根據(jù)type來確定是文本還是圖片

而實現(xiàn)這個效果使用原生的開發(fā)效果,最后選擇了YYText的富文本編輯,最后實現(xiàn)效果是這樣子的

實現(xiàn)效果


實現(xiàn)效果

上圖為要傳輸?shù)臄?shù)據(jù),具體就是進行序列化,轉(zhuǎn)成json文本傳給后臺大概就這樣子了,希望能夠幫到大家再附上 yytext附文圖片插入的代碼

NSMutableAttributedString*string = [[NSMutableAttributedStringalloc]initWithAttributedString:self.textView.attributedText];

UIImage*image = photos[0];

CGFloatscale = image.size.width/(SCREEN_WIDTH-32);

CGFloatphotoHeight = image.size.height/ scale;

YYAnimatedImageView*imageView = [[YYAnimatedImageViewalloc]initWithFrame:CGRectMake(0,16,SCREEN_WIDTH-32, photoHeight)];

//imageView.image = image;

[imageViewyy_setImageWithURL:[NSURLURLWithString:@"https://imgsa.baidu.com/baike/c0%3Dbaike80%2C5%2C5%2C80%2C26/sign=f330fb6cf2d3572c72ef948eeb7a0842/fcfaaf51f3deb48f75b50d5bf01f3a292cf57853.jpg"]options:YYWebImageOptionSetImageWithFadeAnimation];

NSMutableAttributedString*attachText = [NSMutableAttributedStringyy_attachmentStringWithContent:imageViewcontentMode:UIViewContentModeScaleAspectFillattachmentSize:imageView.sizealignToFont:[UIFontsystemFontOfSize:14.fweight:UIFontWeightRegular]alignment:YYTextVerticalAlignmentCenter];

attachText.yy_lineSpacing=5;

[stringappendAttributedString:attachText];

[stringappendAttributedString:[[NSMutableAttributedStringalloc]initWithString:@"\n"]];

//修改行間距

string.yy_font= [UIFontsystemFontOfSize:14.fweight:UIFontWeightRegular];

string.yy_lineSpacing=5;

_textView.font= [UIFontsystemFontOfSize:14.fweight:UIFontWeightRegular];

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

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

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