iOS Series tutorial Textkit Implementation of text-to-picture mixed reading PostScript
The first two days to see Sohu home Mingo wrote "Textkit realize the picture and text mixed row" Tonight home to see the next API found a more trickery implementation. You can directly display the HTML Rich text returned in the background with TextView. I remember two years ago when this was done with the help of heavy webview.
> Textkit is iOS7 new class library, in fact, the Coretext on the previous release of the package, with this textkit, no longer take coretext to do dirty, the following is my Uitextview and UIWebView respectively Display a text mixed with a graphic
Api
(ID) Initwithfileurl:(nsurl *) URL options:(nsdictionary *) options Documentattributes:( Nsdictionary * *) dict error:(nserror *) error
Description
Initializes a new attributed string object from the data at the given URL. The HTML importer should not being called from a background thread (so is, the options dictionary includes Nsdocumenttypedo Cumentattribute with a value of Nshtmltextdocumenttype). It would try to synchronize with the main thread, fail, and time out. Calling it from the main thread works (but can still time off if the HTML contains references to external resources, which Should is avoided at all costs). The HTML import mechanism is meant for implementing something like Markdown (that's, text styles, colors, and so on), not For general HTML Import.
The HTML used for the test
<Metacharset="UTF-8" ><Divstyle="Background-color:lightgrey; font-size:14px; Color: #304182; Text-align:center; margin-left:5px; padding-right:5px "><P>hi<Spanstyle= "FONT-SIZE:18PX; Color: #E88834; " >taobao</span> <img span class= "attribute" >src= "hufeng.png" height= " "width=" + "/> <p> static picture <img src= "Taobao.gif" span class= "attribute" >height= "all" width= "52" > <p> Dynamic picture </p> </DIV>
- It is important to note that the first line needs to indicate the encoding format otherwise the Chinese will show garbled
- GIF image TextView automatically converted to a static image for display
If you remove the meta charset= "UTF-8" Display effect
function function
-(void) loadhtmltowebview{ [[NSBundle Mainbundle] urlforresource:@ "text" withextension:@ "html"]; [_webview loadrequest:[nsurlrequest requestwithurl:url];}
-(void) loadhtmltotextview{ //Create attributed string from HTML nsurl *url = [nsbundle Mainbundle] urlforresource:@"text" withextension:@"html"]; nsattributedstring *attrstr = [[Nsattributedstring alloc] Initwithfileurl:url options:@{ Nsdocumenttypedocumentattribute:nshtmltextdocumenttype} documentattributes:nil Error:nil]; [_textview setattributedtext:attrstr];}
Demo code Download HTTP://VDISK.WEIBO.COM/S/AOCPGYPXA04G2
W This article if there is no special instructions, are original, reproduced please link the way to indicate this article address: http://hufeng825.github.com/2014/01/21/ios37/
iOS Series tutorial Textkit Implementation of text-to-picture mixed reading PostScript