2

我有一个NSAttributedString (来自 HTML),我为UITextView.

- (void)setHtml:(NSString *)html {

    NSData *htmlData = [html dataUsingEncoding:NSUTF8StringEncoding];

    // Create the HTML string
    NSDictionary *importParams = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType};
    NSError *error = nil;
    self.htmlString = [[NSAttributedString alloc] initWithData:htmlData options:importParams documentAttributes:NULL error:&error];

    self.editorView.attributedText = self.htmlString;

}

然后我让用户编辑他们想要的内容,然后我想再次将其转换为 HTML,所以我使用ULIKit htmlFromRange:的编辑版本。这是相关的方法:

-(NSString*)    HTMLFromRange: (NSRange)range
{
    unsigned int                location = 0;
    NSRange                     effRange;
    NSMutableString*            str = [NSMutableString string];
    NSMutableString*            endStr = [NSMutableString string];
    NSDictionary*               attrs = nil;
    NSDictionary*               oldAttrs = nil;

    unsigned int    finalLen = range.location +range.length;

    // TODO: Use oldAttrs, add NSForegroundColorAttributeName and

    attrs = [self attributesAtIndex: location effectiveRange: &effRange];
    location = effRange.location +effRange.length;

    NSLog(@"attributed string: %@", self);

    // Font/color changed?
    UIFont* fnt = [attrs objectForKey: NSFontAttributeName];
    UIColor* tcol = [attrs objectForKey: NSForegroundColorAttributeName];
    if( fnt || tcol )
    {
        [str appendString: @"<font"];
        if( tcol )
        {
            [str appendFormat: @" color=\"%@\"", ColorToHTMLColor(tcol)];
        }
        [str appendString: @">"];
        [endStr insertString: @"</font>" atIndex: 0];

        UIFontDescriptor *fontDescriptor = [fnt fontDescriptor];
        if( (fontDescriptor.symbolicTraits & UIFontDescriptorTraitItalic) == UIFontDescriptorTraitItalic )
        {
                [str appendString: @"<i>"];
                [endStr insertString: @"</i>" atIndex: 0];
        }
        if( (fontDescriptor.symbolicTraits & UIFontDescriptorTraitBold) == UIFontDescriptorTraitBold)
        {
            [str appendString: @"<b>"];
            [endStr insertString: @"</b>" atIndex: 0];
        }
    }

    //Underline
    NSNumber *underline = [attrs objectForKey:NSUnderlineStyleAttributeName];
    if(underline && [underline intValue] > 0) {
        [str appendString:@"<u>"];
        [endStr insertString:@"</u>" atIndex:0];
    }

    // Superscript changed?
    NSNumber* supers = [attrs objectForKey: (NSString*)kCTSuperscriptAttributeName];
    if( supers && supers != [oldAttrs objectForKey: (NSString*)kCTSuperscriptAttributeName] )
    {

        [str appendString: [supers intValue] > 0 ? @"<sup>" : @"<sub>"];
        [endStr insertString: [supers intValue] > 0 ? @"</sup>" : @"</sub>" atIndex: 0];
    }

    // Actual text and closing tags:
    [str appendString: [[[self string] substringWithRange:effRange] stringByInsertingHTMLEntitiesAndLineBreaks: YES]];
    [str appendString: endStr];

    while( location < finalLen )
    {
        [endStr setString: @""];
        attrs = [self attributesAtIndex: location effectiveRange: &effRange];
        location = effRange.location +effRange.length;

        // Font/color changed?
        UIFont* fnt = [attrs objectForKey: NSFontAttributeName];
        UIColor* tcol = [attrs objectForKey: NSForegroundColorAttributeName];
        if( fnt || tcol )
        {
            [str appendString: @"<font"];
            if( tcol )
            {
                [str appendFormat: @" color=\"%@\"", ColorToHTMLColor(tcol)];
            }
            [str appendString: @">"];
            [endStr insertString: @"</font>" atIndex: 0];

            UIFontDescriptor *fontDescriptor = [fnt fontDescriptor];
            if( (fontDescriptor.symbolicTraits & UIFontDescriptorTraitItalic) == UIFontDescriptorTraitItalic )
            {
                    [str appendString: @"<i>"];
                    [endStr insertString: @"</i>" atIndex: 0];
            }
            if( (fontDescriptor.symbolicTraits & UIFontDescriptorTraitBold) == UIFontDescriptorTraitBold )
            {
                [str appendString: @"<b>"];
                [endStr insertString: @"</b>" atIndex: 0];
            }
        }

        //Underline
        NSNumber *underline = [attrs objectForKey:NSUnderlineStyleAttributeName];
        if(underline && [underline intValue] > 0) {
            [str appendString:@"<u>"];
            [endStr insertString:@"</u>" atIndex:0];
        }

        // Superscript changed?
        NSNumber* supers = [attrs objectForKey: (NSString*)kCTSuperscriptAttributeName];
        if( supers && supers != [oldAttrs objectForKey: (NSString*)kCTSuperscriptAttributeName] )
        {

            [str appendString: [supers intValue] > 0 ? @"<sup>" : @"<sub>"];
            [endStr insertString: [supers intValue] > 0 ? @"</sup>" : @"</sub>" atIndex: 0];
        }


        ///Lists.

        // Actual text and closing tags:
        [str appendString: [[[self string] substringWithRange:effRange] stringByInsertingHTMLEntitiesAndLineBreaks: YES]];
        [str appendString: endStr];
    }

    return str;
}

问题来自列表。具体来说,NSAttributedString从 HTML 解析的方法将列表(和标签)从我在日志中看到的内容解析为 NSTextLists。 NSTextList在 UIKit 中是私有的。有没有办法在不使用苹果默认解析器的情况下将其解析回列表(通过添加到我上面的 htmlFromRange 方法)?(见这个问题: 来自 Nic Hubbard)。

我了解如果用户在我的 textview 中创建一个列表以使用自定义属性来伪造 NSTextList,然后我可以轻松地将其解析为 html,但无法找到一种方法来使用预制的NSAttributedString.

谢谢。(我会注意到,如果您想在两个地方都回答,我会悬赏非常相关的 Nic Hubbard 问题。)

4

0 回答 0