1
votes

Recently, I come into a problem in my project. I need convert NSAttributedString to HTML file. The solution Click here doesn't work for me because want to output the HTML file only with TagName and idName and ClassName, at the same time, output the CSS style file to control how the HTML file will display. Here is my sample code, I wish you can get my intend:

    - (NSDictionary *)html
    {
        NSTextStorage *textStorage = [self contents];
        NSArray *arr = [textStorage paragraphs];
        // Initialize the CSS dictionay
        NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                     nil];

        NSEnumerator *paragraphEnumerator;
        paragraphEnumerator = [arr objectEnumerator];
        NSAttributedString *paragraph;
        NSMutableArray *paragrapHTMLStrings = [[NSMutableArray alloc] initWithCapacity:[arr count]];

        NSMutableString *cssString = [[NSMutableString alloc] initWithCapacity:0];
        [cssString appendString:@"div{"];
        [cssString appendString:[NSString stringWithFormat:@"-webkit-column-count:%ld;", self.columnCount]];
        [cssString appendString:[NSString stringWithFormat:@"width:%fpx;", self.bounds.size.width]];
        [cssString appendString:[NSString stringWithFormat:@"height:%fpx;", self.bounds.size.height]];
        [cssString appendString:@"}"];


        [dict setObject:cssString forKey:@"css"];

        while (paragraph = [paragraphEnumerator nextObject]) {
            // initialize
            NSUInteger length;
            NSRange effectiveRange = NSMakeRange(0, 0);
            id attributeValue;

            length = [paragraph length];
            // get the font attributes
            attributeValue = [paragraph attribute:NSFontAttributeName atIndex:NSMaxRange(effectiveRange) effectiveRange:&effectiveRange];
            NSLog(@"font is %@", [attributeValue fontName]);
            NSLog(@"font-size is %f", [[[attributeValue fontDescriptor] objectForKey:NSFontSizeAttribute] floatValue]);
            NSMutableString *htmlString = [NSMutableString stringWithFormat:@"", [attributeValue fontName],
                                           [[[attributeValue fontDescriptor] objectForKey:NSFontSizeAttribute] floatValue]];

            [htmlString appendString:[paragraph string]];
            [htmlString appendString:@""];
            NSLog(@"htmlString is %@", htmlString);
            [paragrapHTMLStrings addObject:htmlString];
            htmlString = nil;


        }

        NSMutableString *htmlStringOfGraphToReturn = [NSMutableString stringWithString:@""];
        NSString *stringToAdd;
        NSEnumerator *stringEnumerator;
        stringEnumerator = [paragrapHTMLStrings objectEnumerator];

        while (stringToAdd = [stringEnumerator nextObject])
        {
            [htmlStringOfGraphToReturn appendString:stringToAdd];
        }
        [htmlStringOfGraphToReturn appendString:@""];

        [dict setObject:htmlStringOfGraphToReturn forKey:@"html"];

        // test part
        CSSSetGenerator *generater = [[CSSSetGenerator alloc] init];

        NSMutableString *string = [generater outputCSSStyleContent:self];
        NSLog(@"%@", string);
        return dict;
    }
1

1 Answers

0
votes

I got the solution from Github, There's an open project named DTCoreText. I hope this might be useful for someone.