HTML from NSAttributedString

后端 未结 2 1151
礼貌的吻别
礼貌的吻别 2021-02-04 11:08

Rather than converting HTML to an attributed string, I need to convert it back to HTML. This can easily be done on Mac as can be seen here: http://www.justria.com/2011/01/18/how

2条回答
  •  借酒劲吻你
    2021-02-04 11:50

    Not the 'easy' way, but what about iterating through the attributes of the string using:

    - (void)enumerateAttributesInRange:(NSRange)enumerationRange 
                               options:(NSAttributedStringEnumerationOptions)opts 
                            usingBlock:(void (^)(NSDictionary *attrs, NSRange range, BOOL *stop))block
    

    Have an NSMutableString variable to accumulate the HTML (lets call it 'html'). In the block, you would construct the HTML manually using strings. For instance if the text attributes 'attrs' specify red, bold text:

    [html appendFormat:@"%@", [originalStr substringWithRange:range]]


    EDIT: Stumbled across this yesterday:

    NSAttributedString+HTMLFromRange category from "UliKit" (https://github.com/uliwitness/UliKit/blob/master/NSAttributedString+HTMLFromRange.m)

    Looks like it will do what you want.

提交回复
热议问题