gpt4 book ai didi

html - 将 HTML 文本转换为普通文本在 Objective-C 中添加了额外的回车符

转载 作者:行者123 更新时间:2023-12-03 17:18:52 28 4
gpt4 key购买 nike

我有一个方法可以用来将 HTML 文本转换为普通文本。
问题是,当我传递 HTMl 文本时,它会在普通文本中添加额外的回车符

下面是导致问题的方法和示例文本。

有人可以让我知道我缺少什么或需要为此做什么吗?
感谢您的帮助。

- (NSString *)flattenHTML {
NSString *finalString = self;

if ([self isEqualToString:kSNREmptyString] == NO) {
NSAttributedString *attributeString = nil;
NSData *data = [self dataUsingEncoding:NSUnicodeStringEncoding];

if (nil != data) {
NSDictionary *encodingDict = [NSDictionary dictionaryWithObject:@(NSUnicodeStringEncoding)
forKey:@"CharacterEncoding"];
attributeString = [[NSAttributedString alloc] initWithHTML:data
documentAttributes:&encodingDict];
finalString = [attributeString string];
}
}

return finalString;
}

示例文本:

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

最佳答案

也许,这段代码对您有帮助,它在这里工作正常。检查下面的代码。

 - (void)viewDidLoad 
{
[super viewDidLoad];
[self HtmltoText]
}

-(void)HtmltoText
{
NSString *Html_Str =[NSString stringWithFormat:@"<html><head>
<title>My First HTML</title></head><body>
<p>The HTML head element contains meta data.</p>
<p>Meta data is data about the HTML document. <br/>
Break Next line.</p></body> </html>"];

NSAttributedString *attributedString = [[NSAttributedString alloc]
initWithData: [Html_Str
dataUsingEncoding:NSUnicodeStringEncoding]
options: @{
NSDocumentTypeDocumentAttribute:
NSHTMLTextDocumentType }
documentAttributes: nil
error: nil
];


NSLog(@"Html To Normal %@ ",attributedString );


}

关于html - 将 HTML 文本转换为普通文本在 Objective-C 中添加了额外的回车符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44628727/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com