从NSAttribute字符串生成HTML文件

从NSAttribute字符串生成HTML文件,html,objective-c,Html,Objective C,最近,我在我的项目中遇到了一个问题。我需要将NSAttribute字符串转换为HTML文件。解决方案单击对我不起作用,因为我只想输出带有标记名、idName和ClassName的HTML文件,同时输出CSS样式文件以控制HTML文件的显示方式。 这是我的示例代码,我希望您能得到我的建议: - (NSDictionary *)html { NSTextStorage *textStorage = [self contents]; NSArray *arr =

最近,我在我的项目中遇到了一个问题。我需要将NSAttribute字符串转换为HTML文件。解决方案单击对我不起作用,因为我只想输出带有标记名、idName和ClassName的HTML文件,同时输出CSS样式文件以控制HTML文件的显示方式。 这是我的示例代码,我希望您能得到我的建议:

- (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; } -(NSDictionary*)html { NSTextStorage*textStorage=[self contents]; NSArray*arr=[文本存储段落]; //初始化CSS词汇表 NSMutableDictionary*dict=[NSMutableDictionary Dictionary WithObjectsAndKeys: 零]; N分子*段落枚举器; 段落枚举器=[arr objectEnumerator]; NSAttribute字符串*段落; NSMUTABLEARRY*段落tmlstrings=[[NSMUTABLEARRY alloc]initWithCapacity:[arr count]]; NSMutableString*cssString=[[NSMutableString alloc]initWithCapacity:0]; [cssString appendString:@“div{”]; [CSSSString appendString:[NSString stringWithFormat:@”-webkit列计数:%ld;“,self.columnCount]]; [CSSSString appendString:[NSString stringWithFormat:@“宽度:%fpx;”,self.bounds.size.width]; [CSSSString appendString:[NSString stringWithFormat:@“高度:%fpx;”,self.bounds.size.height]; [cssString appendString:@“}”]; [dict setObject:cssString forKey:@“css”]; 而(段落=[段落枚举器下一个对象]){ //初始化 整数长度; NSRange effectiveRange=NSMakeRange(0,0); id属性值; 长度=[段落长度]; //获取字体属性 attributeValue=[段落属性:NSFontAttributeName atIndex:NSMaxRange(effectiveRange)effectiveRange:&effectiveRange]; NSLog(@“字体为%@,[attributeValue fontName]); NSLog(@“字体大小为%f”,[[[attributeValue fontDescriptor]objectForKey:NSFontSizeAttribute]floatValue]); NSMutableString*htmlString=[NSMutableString stringWithFormat:@],[attributeValue fontName], [[[attributeValue fontDescriptor]objectForKey:NSFontSizeAttribute]floatValue]]; [htmlString appendString:[段落字符串]]; [htmlString appendString:@“

”; NSLog(@“htmlString是%@”,htmlString); [段落tmlstrings addObject:htmlString]; htmlString=nil; } NSMutableString*htmlStringOfGraphToReturn=[NSMutableString stringWithString:@''; NSString*stringToAdd; N分子*字符串枚举器; stringEnumerator=[paragrapHTMLStrings对象枚举器]; while(stringToAdd=[stringEnumerator nextObject]) { [htmlStringOfGraphToReturn appendString:stringToAdd]; } [htmlStringOfGraphToReturn appendString:@”“; [dict setObject:htmlStringOfGraphToReturn-forKey:@“html”]; //试验件 CSSSetGenerator*生成器=[[CSSSetGenerator alloc]init]; NSMutableString*string=[Generator OutputCSSSStyleContent:self]; NSLog(@“%@”,字符串); 返回命令; }
我从Github获得了解决方案,有一个名为DTCoreText的开放项目。我希望这可能对某些人有用