索引1258超出范围

索引1258超出范围

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 完美配合。

关于ios - 'NSRangeException',原因:'***-[NSBigMutableString characterAtIndex:]:索引1258超出范围;弦长1176',我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50363326/

10-13 09:03