美文网首页iOS开发
NSAttributedString和HTMLString之间的

NSAttributedString和HTMLString之间的

作者: TimBao | 来源:发表于2017-10-27 17:50 被阅读299次
自制富文本编辑器
  • NSAttributedString to HTMLString
NSDictionary *documentAttributes = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType};
NSData *htmlData = [attributeString dataFromRange:NSMakeRange(0, attributeString.length) documentAttributes:documentAttributes error:NULL];
NSString *htmlString = [[NSString alloc] initWithData:htmlData encoding:NSUTF8StringEncoding];
  • HTMLString to NSAttributedString
NSAttributedString* attributeString = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUTF8StringEncoding]
options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)}
                              documentAttributes:nil error:nil];

注意事项:

NSAttributedString 转为htmlstring后,再转换回NSAttributedString字体会变大,经过查找资料发现是系统问题。stackoverflow上面给出了解决办法:

static CGFloat const kListsLeading = 10.0;
static CGFloat const kListsAdditionalShift = 4.0;

+ (NSAttributedString *)decreaseFontSizeBecauseOfAppleMagic:(NSAttributedString *)astr
{
    NSMutableAttributedString *mastr = [astr mutableCopy];
    
    [mastr enumerateAttribute:NSFontAttributeName inRange:NSMakeRange(0, astr.length) options:0 usingBlock:^(UIFont *_Nullable value, NSRange range, BOOL *_Nonnull stop) {
        if (value) {
            UIFont *font = [value fontWithSize:value.pointSize * 0.75];
            [mastr addAttribute:NSFontAttributeName value:font range:range];
        }
    }];
    
    [mastr enumerateAttribute:NSParagraphStyleAttributeName inRange:NSMakeRange(0, astr.length) options:0 usingBlock:^(NSParagraphStyle *_Nullable value, NSRange range, BOOL *_Nonnull stop) {
        if (value) {
            NSMutableParagraphStyle *style = [value mutableCopy];
            style.minimumLineHeight *= 0.75;
            if (style.firstLineHeadIndent == style.headIndent) {
                style.firstLineHeadIndent *= 0.75;
                style.headIndent *= 0.75;
            }
            else if (style.firstLineHeadIndent < kListsLeading) {
                CGFloat shift = (kListsLeading - style.firstLineHeadIndent);
                style.headIndent += shift + kListsAdditionalShift;
                style.firstLineHeadIndent = kListsLeading;
                NSMutableArray *tabs = [NSMutableArray array];
                NSInteger index = 0;
                for (NSTextTab *tab in style.tabStops) {
                    [tabs addObject:[[NSTextTab alloc] initWithTextAlignment:tab.alignment location:tab.location + shift + kListsAdditionalShift * (index ? 1 : 0) options:tab.options]];
                    index++;
                }
                style.tabStops = tabs;
            }
            style.tailIndent *= 0.75;
            [mastr addAttribute:NSParagraphStyleAttributeName value:style range:range];
        }
    }];
    
    return [mastr copy];
}

相关文章

网友评论

    本文标题:NSAttributedString和HTMLString之间的

    本文链接:https://www.haomeiwen.com/subject/cjrdpxtx.html