html代码的两种显示方法

1用uiwebview

NSString *strHTML = @ "

你好

        这是一个例子,请显示

外加一个table

aaaabbbbcccc

"
;
     
     UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.frame];
     [self.view addSubview:webView];
     
     [webView loadHTMLString:strHTML baseURL:nil];


2.用uilabel

NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[_gameItemDetail.gameDescription dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];

        UILabel * myLabel = [[UILabel alloc] initWithFrame:CGRectMake(Gap_12*2, 0, SCREEN_WIDTH-Gap_12*4, _tableViewMain.frame.size.height-_heightOfTableViewMainHeader)];

        myLabel.attributedText = attrStr;

        myLabel.numberOfLines = 0;

        myLabel.backgroundColor = [UIColor clearColor];

        CGSize sizeLabelText = [NSString sizeLabelWidth:myLabel.frame.size.width attributedText:attrStr];

        myLabel.frame = CGRectMake(Gap_12*2, 0, SCREEN_WIDTH-Gap_12*4, sizeLabelText.height);


+ (CGSize)sizeLabelWidth:(CGFloat)width

          attributedText:(NSAttributedString *)attributted

{

    if(width<=0){

        return CGSizeZero;

    }

    

    UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, 3000)];

    lab.attributedText = attributted;

    lab.numberOfLines = 0;

    

    CGSize labSize = [lab sizeThatFits:lab.bounds.size];

    return labSize;

}


你可能感兴趣的:(ios)