gpt4 book ai didi

ios - Html 字符串转换成属性字符串与\U00002028\n ios

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:30:42 27 4
gpt4 key购买 nike

Html 字符串从服务器获取我尝试转换成属性字符串。使用

NSAttributedString *attributedString = [[NSAttributedString alloc]
initWithData: [htmlString dataUsingEncoding:NSUnicodeStringEncoding]
options: @{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: [NSNumber numberWithInt:NSUTF8StringEncoding] }
documentAttributes: nil
error: nil
];

具有\U00002028\n 的属性字符串导致显示字符串在文本中出现空格和换行。如何能够删除此类内容。

最佳答案

从 NSMutableAttributedString 中去除前导和尾随的空格

NSString * _strAchievementMsg= @"  Hi Test the White space    ";

NSString * htmlString = [NSString stringWithFormat:@"<html><div>%@</div></html>",_strAchievementMsg];

NSMutableAttributedString *attString = [[NSMutableAttributedString alloc]
initWithData: [htmlString dataUsingEncoding:NSUnicodeStringEncoding]
options: @{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: [NSNumber numberWithInt:NSUTF8StringEncoding] }
documentAttributes: nil
error: nil
];
NSCharacterSet *charSet = [NSCharacterSet whitespaceAndNewlineCharacterSet];
NSRange range = [attString.string rangeOfCharacterFromSet:charSet];
while (range.length != 0 && range.location == 0)
{
[attString replaceCharactersInRange:range withString:@""];
range = [attString.string rangeOfCharacterFromSet:charSet];
}

// Trim trailing whitespace and newlines.
range = [attString.string rangeOfCharacterFromSet:charSet
options:NSBackwardsSearch];
while (range.length != 0 && NSMaxRange(range) == attString.length)
{
[attString replaceCharactersInRange:range
withString:@""];
range = [attString.string rangeOfCharacterFromSet:charSet
options:NSBackwardsSearch];
}

关于ios - Html 字符串转换成属性字符串与\U00002028\n ios,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37915304/

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