'NSRangeException', 原因:'*** -[NSBigMutableString characterAtIndex:]: 索引 1258 越界;字符串长度 1176'
'NSRangeException', reason: '*** -[NSBigMutableString characterAtIndex:]: Index 1258 out of bounds; string length 1176'
NSString *htmlString = [NSString stringWithFormat:@"<html> <head><style type=\"text/css\">body { font-family: Mehr Nastaliq Web; font-size: 22pt; white-space: pre-wrap; text-align: right; lang: ar; direction: RTL; -webkit-user-select: none; }</style> </head><body leftmargin=\"20\" topmargin=\"0\" rightmargin=\"20\" > %@ </body></html>",str];
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData: [htmlString dataUsingEncoding:NSUnicodeStringEncoding] options: @{ NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType } documentAttributes: nil error: nil];
self.txtView.attributedText=attributedString;
能否请您在下面尝试一下,请尝试一下,让我知道它是否有效。
NSAttributedString * attributedString = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUTF8StringEncoding]
options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType}
documentAttributes:nil
error:nil];
如果您不确定正确的编码,您应该使用 NSUTF8StringEncoding
更新
NSString *htmlString = @"<html><head><style type=\"text/css\">body { font-family: Mehr Nastaliq Web; font-size: 22pt; white-space: pre-wrap; text-align: right; lang: en; direction: RTL; -webkit-user-select: none; meta charset=\"UTF-8\" }</style> </head><body leftmargin=\"20\" topmargin=\"0\" rightmargin=\"20\"> مُدّعا عَنقا ہے اپنے عالَمِ تقریر کا میری تقریر کا مفہوم چونکہ عنقا یعنی معدوم ہے اور معدوم </body></html>";
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData: [htmlString dataUsingEncoding:NSUnicodeStringEncoding] options: @{ NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType } documentAttributes: nil error: nil];
_txtView.attributedText = attributedString;
它与 NSUnicodeStringEncoding 完美配合。
我也看到了崩溃,即使是在最基本的示例中也是如此。在为 UICollectionView 创建 UICollectionViewCells 时,我能够跟踪它的执行情况。当我把它拉出来并且 运行 在更新列表之前,它工作正常......完全相同的代码。
iOS 中似乎有一个与此相关的错误,至少在 UICollectionViewDataSource:collectionView:cellForItemAtIndexPath:
中调用时是这样的
需要在Option dict中添加字符编码,与字符串编码相同。除此以外。这将是不可读的代码。
NSAttributedString.DocumentReadingOptionKey.characterEncoding:String.Encoding.utf8.rawValue
整个代码是
let htmlString = """
<html><head><style type=\"text/css\">body { font-family: Mehr Nastaliq Web; font-size: 22pt; white-space: pre-wrap; text-align: right; lang: en; direction: RTL; -webkit-user-select: none; meta charset=\"UTF-8\" }</style> </head><body leftmargin=\"20\" topmargin=\"0\" rightmargin=\"20\"> hello world! Arabic.<br/> مُدّعا عَنقا ہے اپنے عالَمِ تقریر کا میری تقریر کا مفہوم چونکہ عنقا یعنی معدوم ہے اور معدوم </body></html>
"""
let attributedString = try? NSAttributedString(data:
htmlString.data(using: String.Encoding.utf8)!, options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html, NSAttributedString.DocumentReadingOptionKey.characterEncoding:String.Encoding.utf8.rawValue], documentAttributes: nil)
textView.attributedText = attributedString
您也可以同时更改字符串编码和属性编码。结果也将是可读的。比如unicode。
let attributedString = try? NSAttributedString(data:
htmlString.data(using: String.Encoding.unicode)!, options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html, NSAttributedString.DocumentReadingOptionKey.characterEncoding:String.Encoding.unicode.rawValue], documentAttributes: nil)
textView.attributedText = attributedString
NSString *htmlString = [NSString stringWithFormat:@"<html> <head><style type=\"text/css\">body { font-family: Mehr Nastaliq Web; font-size: 22pt; white-space: pre-wrap; text-align: right; lang: ar; direction: RTL; -webkit-user-select: none; }</style> </head><body leftmargin=\"20\" topmargin=\"0\" rightmargin=\"20\" > %@ </body></html>",str];
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData: [htmlString dataUsingEncoding:NSUnicodeStringEncoding] options: @{ NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType } documentAttributes: nil error: nil];
self.txtView.attributedText=attributedString;
能否请您在下面尝试一下,请尝试一下,让我知道它是否有效。
NSAttributedString * attributedString = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUTF8StringEncoding]
options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType}
documentAttributes:nil
error:nil];
如果您不确定正确的编码,您应该使用 NSUTF8StringEncoding
更新
NSString *htmlString = @"<html><head><style type=\"text/css\">body { font-family: Mehr Nastaliq Web; font-size: 22pt; white-space: pre-wrap; text-align: right; lang: en; direction: RTL; -webkit-user-select: none; meta charset=\"UTF-8\" }</style> </head><body leftmargin=\"20\" topmargin=\"0\" rightmargin=\"20\"> مُدّعا عَنقا ہے اپنے عالَمِ تقریر کا میری تقریر کا مفہوم چونکہ عنقا یعنی معدوم ہے اور معدوم </body></html>";
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData: [htmlString dataUsingEncoding:NSUnicodeStringEncoding] options: @{ NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType } documentAttributes: nil error: nil];
_txtView.attributedText = attributedString;
它与 NSUnicodeStringEncoding 完美配合。
我也看到了崩溃,即使是在最基本的示例中也是如此。在为 UICollectionView 创建 UICollectionViewCells 时,我能够跟踪它的执行情况。当我把它拉出来并且 运行 在更新列表之前,它工作正常......完全相同的代码。
iOS 中似乎有一个与此相关的错误,至少在 UICollectionViewDataSource:collectionView:cellForItemAtIndexPath:
需要在Option dict中添加字符编码,与字符串编码相同。除此以外。这将是不可读的代码。
NSAttributedString.DocumentReadingOptionKey.characterEncoding:String.Encoding.utf8.rawValue
整个代码是
let htmlString = """
<html><head><style type=\"text/css\">body { font-family: Mehr Nastaliq Web; font-size: 22pt; white-space: pre-wrap; text-align: right; lang: en; direction: RTL; -webkit-user-select: none; meta charset=\"UTF-8\" }</style> </head><body leftmargin=\"20\" topmargin=\"0\" rightmargin=\"20\"> hello world! Arabic.<br/> مُدّعا عَنقا ہے اپنے عالَمِ تقریر کا میری تقریر کا مفہوم چونکہ عنقا یعنی معدوم ہے اور معدوم </body></html>
"""
let attributedString = try? NSAttributedString(data:
htmlString.data(using: String.Encoding.utf8)!, options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html, NSAttributedString.DocumentReadingOptionKey.characterEncoding:String.Encoding.utf8.rawValue], documentAttributes: nil)
textView.attributedText = attributedString
您也可以同时更改字符串编码和属性编码。结果也将是可读的。比如unicode。
let attributedString = try? NSAttributedString(data:
htmlString.data(using: String.Encoding.unicode)!, options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html, NSAttributedString.DocumentReadingOptionKey.characterEncoding:String.Encoding.unicode.rawValue], documentAttributes: nil)
textView.attributedText = attributedString