在iOS 7上将NSAttributedString解析为HTML
我有一个在iOS 7上将NSAttributedString解析为HTML,ios,objective-c,uitextview,nsattributedstring,textkit,Ios,Objective C,Uitextview,Nsattributedstring,Textkit,我有一个NSAttributedString(来自HTML),我为UITextView设置了它 - (void)setHtml:(NSString *)html { NSData *htmlData = [html dataUsingEncoding:NSUTF8StringEncoding]; // Create the HTML string NSDictionary *importParams = @{NSDocumentTypeDocumentAttribut
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,因此我使用的是已编辑版本的。以下是与此相关的方法:
-(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;
}
-(NSString*)HTMLFromRange:(NSRange)范围
{
无符号整数位置=0;
NSRange范围;
NSMutableString*str=[NSMutableString];
NSMutableString*endStr=[NSMutableString];
NSDictionary*attrs=nil;
NSDictionary*oldAttrs=nil;
无符号int finalLen=range.location+range.length;
//TODO:使用oldAttrs,添加NSForegroundColorAttributeName和
属性=[self attributesAtIndex:location effectiveRange:&effRange];
位置=effRange.location+effRange.length;
NSLog(@“属性字符串:%@”,self);
//字体/颜色改变了吗?
UIFont*fnt=[attrs objectForKey:NSFontAttributeName];
UIColor*tcol=[attrs objectForKey:NSForegroundColorAttributeName];
如果(fnt | | tcol)
{
[str appendString:@”“;
[endStr insertString:@“atIndex:0];
UIFontDescriptor*fontDescriptor=[fnt fontDescriptor];
if((fontDescriptor.symbolicTraits&UIFontDescriptorRaiTitalic)==UIFontDescriptorRaiTitalic)
{
[str appendString:@”“;
[endStr insertString:@“atIndex:0];
}
if((fontDescriptor.symbolicTraits&UIFontDescriptorRaitbold)==UIFontDescriptorRaitbold)
{
[str appendString:@”“;
[endStr insertString:@“atIndex:0];
}
}
//下划线
NSNumber*underline=[attrs objectForKey:NSUnderlineStyleAttributeName];
如果(下划线和[underline intValue]>0){
[str appendString:@”“;
[endStr insertString:@“atIndex:0];
}
//上标变了?
NSNumber*supers=[attrs objectForKey:(NSString*)kCTSuperscriptAttributeName];
if(supers&&supers!=[oldAttrs objectForKey:(NSString*)kCTSuperscriptAttributeName])
{
[str appendString:[supersintvalue]>0?@“”:@“”;
[endStr insertString:[supersintvalue]>0?@“:@”索引:0];
}
//实际文本和结束标记:
[str appendString:[[[self string]substringWithRange:effRange]STRINGBYINSERTINGHTMLENTITIES和换行符:是]];
[str appendString:endStr];
while(位置<最终)
{
[endStr setString:@”“;
属性=[self attributesAtIndex:location effectiveRange:&effRange];
位置=effRange.location+effRange.length;
//字体/颜色改变了吗?
UIFont*fnt=[attrs objectForKey:NSFontAttributeName];
UIColor*tcol=[attrs objectForKey:NSForegroundColorAttributeName];
如果(fnt | | tcol)
{
[str appendString:@”“;
[endStr insertString:@“atIndex:0];
UIFontDescriptor*fontDescriptor=[fnt fontDescriptor];
if((fontDescriptor.symbolicTraits&UIFontDescriptorRaiTitalic)==UIFontDescriptorRaiTitalic)
{
[str appendString:@”“;
[endStr insertString:@“atIndex:0];
}
if((fontDescriptor.symbolicTraits&UIFontDescriptorRaitbold)==UIFontDescriptorRaitbold)
{
[str appendString:@”“;
[endStr insertString:@“atIndex:0];
}
}
//下划线
NSNumber*underline=[attrs objectForKey:NSUnderlineStyleAttributeName];
如果(下划线和[underline intValue]>0){
[str appendString:@”“;
[endStr insertString:@“atIndex:0];
}
//上标变了?
NSNumber*supers=[attrs objectForKey:(NSString*)kCTSuperscriptAttributeName];
if(supers&&supers!=[oldAttrs objectForKey:(NSString*)kCTSuperscriptAttributeName])
{
[str appendString:[supersintvalue]>0?@“”:@“”;
[endStr insertString:[supersintvalue]>0?@“:@”索引:0];
}
///名单。
//实际文本和结束标记:
[str appendString:[[[self string]substringWithRange:effRange]STRINGBYINSERTINGHTMLENTITIES和换行符:是]];
[str appendString:endStr];
}
返回str;
}
问题来自列表。具体地说,从HTML解析的NSAttributedString
方法从我在日志中看到的内容将列表(和标记)解析为NSTextList<代码>NSTextList在UIKit中是私有的。有没有办法不用苹果的默认解析器(通过添加到我上面的htmlFromRange方法)将其解析回列表?(见问题:)
我知道如果用户在我的textview中创建一个列表,使用自定义属性来伪造NSTextList,那么我可以轻松地将其解析为html,但无法找到一种方法来使用预先制作的NSAttributedString
谢谢。(我会注意到,如果你想在这两个地方都回答的话,我对Nic哈伯德的问题给予了悬赏,这是一个非常相关的问题。)