HTML字符串与NSAttributeString互相转换

html转为NSAttributedString
[[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUTF8StringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)} documentAttributes:nil error:nil];
复制代码
NSAttributedString 转为 html(作为NSAttributedString的分类)
- (NSString *)htmlString
{
    if (!self.string || self.string.length <= 0) {
        return @"";
    }
 NSMutableString *htmlString = [NSMutableString string];
 NSArray *paragraphRanges = [self rangeOfParagraphsFromTextRange:NSMakeRange(0, self.string.length-1)];
 
 for (int i=0 ; i<paragraphRanges.count ; i++)
 {
  NSValue *value = [paragraphRanges objectAtIndex:i];
  NSRange range = [value rangeValue];
  NSDictionary *paragraphDictionary = [self attributesAtIndex:range.location effectiveRange:nil];
  NSParagraphStyle *paragraphStyle = [paragraphDictionary objectForKey:NSParagraphStyleAttributeName];
  NSString *textAlignmentString = [self htmlTextAlignmentString:paragraphStyle.alignment];
  
  [htmlString appendString:@"<p "];
  
  if (textAlignmentString)
   [htmlString appendFormat:@"align=\"%@\" ", textAlignmentString];
  
  [htmlString appendFormat:@"style=\""];
  
  if (paragraphStyle.firstLineHeadIndent > 0)
   [htmlString appendFormat:@"text-indent:%.0fpx; ", paragraphStyle.firstLineHeadIndent - paragraphStyle.headIndent];
  
  if (paragraphStyle.headIndent > 0)
   [htmlString appendFormat:@"margin-left:%.0fpx; ", paragraphStyle.headIndent];
   
  
  [htmlString appendString:@" \">"];
  
  [self enumerateAttributesInRange:range
         options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired
         usingBlock:^(NSDictionary *dictionary, NSRange range, BOOL *stop){
             
          NSMutableString *fontString = [NSMutableString string];
          UIFont *font = [dictionary objectForKey:NSFontAttributeName];
          UIColor *foregroundColor = [dictionary objectForKey:NSForegroundColorAttributeName];
          UIColor *backGroundColor = [dictionary objectForKey:NSBackgroundColorAttributeName];
          NSNumber *underline = [dictionary objectForKey:NSUnderlineStyleAttributeName];
          BOOL hasUnderline = (!underline || underline.intValue == NSUnderlineStyleNone) ? NO :YES;
          NSNumber *strikeThrough = [dictionary objectForKey:NSStrikethroughStyleAttributeName];
          BOOL hasStrikeThrough = (!strikeThrough || strikeThrough.intValue == NSUnderlineStyleNone) ? NO :YES;
          
          [fontString appendFormat:@"<font "];
          [fontString appendFormat:@"face=\"%@\" ", font.familyName];
          
          // Begin style
          [fontString appendString:@" style=\" "];
          
          [fontString appendFormat:@"font-size:%.0fpx; ", font.pointSize];
          
          if (foregroundColor && [foregroundColor isKindOfClass:[UIColor class]])
           [fontString appendFormat:@"color:%@; ", [self htmlRgbColor:foregroundColor]];
          
          if (backGroundColor && [backGroundColor isKindOfClass:[UIColor class]])
           [fontString appendFormat:@"background-color:%@; ", [self htmlRgbColor:backGroundColor]];
          
          [fontString appendString:@"\" "];
          // End Style
          
          [fontString appendString:@">"];
          [fontString appendString:[[self.string substringFromIndex:range.location] substringToIndex:range.length]];
          [fontString appendString:@"</font>"];
          
          if ([font isBold])
          {
           [fontString insertString:@"<b>" atIndex:0];
           [fontString insertString:@"</b>" atIndex:fontString.length];
          }
          
          if ([font isItalic])
          {
           [fontString insertString:@"<i>" atIndex:0];
           [fontString insertString:@"</i>" atIndex:fontString.length];
          }
          
          if (hasUnderline)
          {
           [fontString insertString:@"<u>" atIndex:0];
           [fontString insertString:@"</u>" atIndex:fontString.length];
          }
          
          if (hasStrikeThrough)
          {
           [fontString insertString:@"<strike>" atIndex:0];
           [fontString insertString:@"</strike>" atIndex:fontString.length];
          }
          
          
          [htmlString appendString:fontString];
         }];
  
  [htmlString appendString:@"</p>"];
 }
 
 return htmlString;
}
复制代码
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值