2

I have an NSAttributedString (coming from HTML) that I set for a 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;

}

I then let the user edit what they want, and I would like to then convert it out to HTML again, so I use an edited version of ULIKit htmlFromRange:. Here is the relevant method from that:

-(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;
}

The problem comes from lists. Specifically the NSAttributedString method to parse from HTML parses lists ( and tags) into NSTextLists from what I can see in logs. NSTextList is private in UIKit. Is there a way to parse that back into a list without using apple's default parser (by adding to my htmlFromRange method above)? (See this question: from Nic Hubbard).

I understand how if the user CREATES a list in my textview to use a custom attribute to fake a NSTextList and then I can easily parse that into html, but can't figure out a way to get that going with a pre-made NSAttributedString.

Thanks. (I will note I put a bounty on the Nic Hubbard question that is very related if you want to answer in both places. )

Community
  • 1
  • 1
Jesse Naugher
  • 9,780
  • 1
  • 41
  • 56

0 Answers0