AliSoftware / OHAttributedLabel

UILabel that supports NSAttributedString

Home Page:https://github.com/AliSoftware/OHAttributedLabel/wiki

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Can it support "table", "tr", "td", "align=right"?

gclsoft opened this issue · comments

commented

Like this:

<table >
        <tr>
            <td align="right"
commented

I have to use this way by UIWebView:

        UIWebView *webview=[[UIWebView alloc]initWithFrame:rcNew];
        [self.view addSubview:webview];
    MLControl* ctl=[ MLControl shared];
    NSString* strTitle=[NSString stringWithFormat:@"<html><body padding='0'>\
                        <h2>收费信息:</h2>\
                        <table border='0' cellpadding='0' cellspacing='3' width='100\%'>\
                           <tr >\
                         <td align='right'>姓名:</td>\
                        <td>%@\
                        <td align='right'>费用合计:</td>\
                        <td>%.2f\
                        </tr></table>\
                        折扣:%.2f,应付款:%.2f Some <b>basic</b> <font name=\"Courier\" size=\"14\">HTML</font> support is provided for <u>convenience</u> too. <font color='red'>You can add your own >parsers easily</font> by subclassing <font name=\"Courier\" size=\"14\">OHASMarkupParserBase</font> if needed.</body></html>"
                        ,ctl.chargeUserName
                        ,ctl.moneyAllOrigin
                        ,ctl.moneyDiscount
                        ,ctl.moneyAllNow
                        ];
    [webview loadHTMLString:strTitle baseURL:nil];

OHAttributedLabel has been deprecated for a long time now (see the README for reasons and alternatives, but basically the main being native support for it since recent versions of iOS), so no new features will be added to it.

Besides to my knowledge, what you want to achieve is not possible directly with an NSAttributedString nor with CoreText directly and will need a complex layout engine like the ones used to render a fully-fledged HTML page (so like the ones used in WebKit, not just CoreText and NSAttributedString)

Note that you may still try building an NSAttributedString with your HTML source code (see my new OHAttributedStringAdditions pod which provides convenience methods to do that with calls as simple as NSAttributedString initWithHTMLString:).

But as NSAttributedString itself does not support tables that won't end up the same as when a full WebKit layout engine kicks in.

If you want to implement that yourself you'll need to use separate NSAttributedStrings for each cell of your table and use TextKit to layout them all in each cell properly, which is way more work than just a single text styling like bold, italics and paragraph alignment.

commented

@AliSoftware Thanks so much! :)

-(NSString*)getHtmlTb:(NSArray*)arrLines head:(NSString*)head{

    NSMutableString* strTb=[NSMutableString stringWithCapacity:100];
    int i=0;
    [strTb appendFormat:@"<b>%@</b>   <hr><table border='0' cellpadding='0' cellspacing='3' width='100%%'>",head];
    for (NSString* str in arrLines) {
        BOOL newLine=i%8==0;
        if (newLine) {
            if (i!=0) {
                [strTb appendString:@"</td></tr> "];
            }

            [strTb appendString:@"<tr> "];
        }

        if (i!=0) {
            [strTb appendString:@"</td>"];
        }

        if(i%2==0){


            [strTb appendString:@"<td align='right'>"];
        }else{
            [strTb appendString:@"<td>"];
        }

        [strTb appendString:str];
//        if (i%2==0) {
//            [strTb appendString:@":"];
//        }
        ++i;
    }

    [strTb appendString:@"</td></tr></table>"];
    return strTb;
}