UILabel attributedText html 图片自适应

425 阅读1分钟
//html内容
NSString *htmlString = @"...";

//设置图片大小自适应
NSString *setImgStr = [NSString stringWithFormat:@"<head><style>img{max-width:%@px !important;height:auto !important}</style></head>",@(Screen_WIDTH)];

NSString *content = [NSString stringWithFormat:@"%@%@",setImgStr,htmlString];

NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithData:[content dataUsingEncoding:NSUnicodeStringEncoding]
                                                                                    options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType}
                                                                         documentAttributes:nil
                                                                                      error:nil];

UILabel *lab = [[UILabel alloc] init];

lab.attributedText = attributedText;