<br>
,<h1>
,<p>
标记不影响我在UILabel中使用html字符串时应该提供1行换行的属性文本
下面是我的代码
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:UYLCellIdentifier forIndexPath:indexPath];
UILabel* lineLabel = [cell viewWithTag:100];
lineLabel.numberOfLines=0;
NSString* html = self.offerArray[indexPath.row][@"description"];
NSString * htmlString = [NSString stringWithFormat: @"%@%@</body></html>",kHTMLHeadOffer,html];
NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType} documentAttributes:nil error:nil];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init] ;
[paragraphStyle setAlignment:NSTextAlignmentCenter];
[attrStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [attrStr length])];
lineLabel.attributedText = attrStr;
Html示例
<h2><font color="gray”>Offer</font></h2>
<b><font color="red">Offer 1:</font></b><br>
<b>1 kg Sugar Free!</b><br>
<p>Every purchase above 500 <br>
valid till 23rd April</p>
以上在模拟器中的结果
浏览器结果(w3school编辑器)
每行图像中有1行的差异
答案 0 :(得分:1)
您的HTML似乎有问题,我通过复制粘贴您的示例HTML来做到这一点,它对我来说很好。
NSString* html = @"<h2><font color=\"gray\">Offer</font></h2><b><font color=\"red\">Offer 1:</font></b><br><b>1 kg Sugar Free!</b><br><p>Every purchase above 500 <br> valid till 23rd April</p>";
NSString*kHTMLHeadOffer = @"<html><body>";
NSString * htmlString = [NSString stringWithFormat: @"%@%@</body></html>",kHTMLHeadOffer,html];
NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType} documentAttributes:nil error:nil];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init] ;
[paragraphStyle setAlignment:NSTextAlignmentCenter];
[attrStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [attrStr length])];
lineLabel.attributedText = attrStr;
我必须添加"\"
以便在color
标记之后提供qoutes。
这就是我的样子
在阅读了已添加图片的编辑答案后,我尝试了您在w3school编辑器中提供的示例html,它看起来与在模拟器中看起来相同。这是它的SS
正如您在图片中看到的那样,标题报价后只有1次休息,而不是2次。