HTML标签 转 NSAttributedString

NSDictionary *options = @{
                              NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType
                              };
    NSAttributedString *attributeString = [[NSAttributedString alloc] initWithData:[@"改革开放  40年" dataUsingEncoding:NSUnicodeStringEncoding] options:options documentAttributes:nil error:nil];
    
    NSRange range = NSMakeRange(0, attributeString.string.length);
    
    NSMutableAttributedString *mutableAttributeString = [[NSMutableAttributedString alloc] initWithAttributedString:attributeString];
    [attributeString enumerateAttributesInRange:range options:NSAttributedStringEnumerationReverse usingBlock:^(NSDictionary * _Nonnull attrs, NSRange range, BOOL * _Nonnull stop) {
        
        UIFont *oldFont = [attrs objectForKey:NSFontAttributeName];
        
        NSMutableDictionary *newattrs = [attrs mutableCopy];
        [newattrs setValue:[UIFont systemFontOfSize:oldFont.pointSize] forKey:NSFontAttributeName];
        
        [mutableAttributeString setAttributes:newattrs range:range];
        
    }];

你可能感兴趣的:(HTML标签 转 NSAttributedString)