Upgrade to Pro — share decks privately, control downloads, hide ads and more …

How to convert custom HTML to native styled text

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers. →

How to convert custom HTML to native styled text

Avatar for Yuki Shiho

Yuki Shiho

July 23, 2016

More Decks by Yuki Shiho

Other Decks in Programming

Transcript

  1. 4FSWFS3FTQPOTF+TPO { type: "TEXT", itemId: 1468485519419, text: "ͦ͜ʹ͸ͳΜͱɺ<span class="td-color">ϏϤϯϏϤ ϯʹൽ͕ͨΔΜ͓ͩෲ</span>ͷࣸਅ͕ʂ<br><br>͜Ε

    ʹ͸<span class="td-weight">ʮ͋ͳͨͷਖ਼௚ͳͱ͜Ζ޷ ͖Ͱ͢ὑʯʮ</span><span class="td-weight">શવΦο έʔʂʂʯ</span>ʮ</span><span class="td-weight">ͦ Ε͸ɺͦΕͰັྗͩΑὑʯ</span>ͱɺૉ௚ͳ۽ా͞ΜΛ ৆ࢍ͢Δίϝϯτ͕૬࣍͗·ͨ͠ɻ<br>" }
  2. &NCFE8FC7JFXʢʣ <!DOCTYPE html>
 <html>
 <head>
 <meta charset="utf-8">
 <style>
 * {


    margin: 0;
 padding: 0;
 text-align: center;
 }
 </style>
 </head>
 <body> <iframe class="vine-embed" src="https://vine.co/v/%1$s/embed/simple" width="%2$s" height="%3$s" frameborder="0" slm-video-size class="js-video-size"></iframe>
 <script async src="//platform.vine.co/static/scripts/embed.js" charset=“utf-8"></script> </body>
 </html> assets/view_item_vine.xml 1. Prepare assets/***.html
  3. &NCFE8FC7JFXʢʣ String data = String.format(
 AssetsUtil.loadAssetAsString(getContext(), 
 "embed/article_item_vine.html"),
 videoId, width,

    height); 
 webView.loadDataWithBaseURL(
 "about:blank", data, "text/html", "UTF-8", null); 2. Load assets/**.html
  4. )UNMGSPN)UNM ʢʣ textView.setText(
 Html.fromHtml("<big>BigText</big>")); 1. fromHtml(String source) <BR>, <P>, <DIV>,

    <B>, <EM>, <I>, <STRONG>, <CITE>, <DFN>, <BIG>, <SMALL>, <FONT>, <BLOCKQUOTE>, <TT>, <A>, <U>, <SUP>, <SUB>, <H1ʙH6>, <IMG> ɾAvailable Tags
  5. )UNMGSPN)UNM ʢʣ 2. fromHtml(String source, Html.ImageGetter imageGetter, 
 Html.TagHandler tagHandler)

    ɾHtml.TagHandler void handleTag (boolean opening, String tag, Editable output, XMLReader xmlReader); This method will be called when the HTML parser encounters a tag that it does not know how to interpret.
  6. )UNM5BH)BOEMFSʢʣ @Override
 public void handleTag(boolean opening, String tag, 
 Editable

    output, XMLReader xmlReader) { if (tag.equals("span") { if (opening) { // process open tags } else { // process close tags } }
  7. )UNM5BH)BOEMFSʢʣ // process open tags // get attributes HashMap<String, String>

    attrs = getAttributes(xmlReader); // get class array String[] classes = attrs.get("class").split(" "); for (String clazz : classes) {
 if (clazz.equals("td-weight")) { // process BOLD
 } else if (clazz.equals("td-size")) { // process SIZE }
 }