'NSRangeException',原因:'*** - [NSBigMutableString characterAtIndex:]:索引1258超出范围;弦长1176'

时间:2018-05-16 05:53:44

标签: ios objective-c nsattributedstring

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;

3 个答案:

答案 0 :(得分:1)

请你试试下面的内容,试一试,让我知道它是否有效。

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

答案 1 :(得分:0)

即使在最基本的示例中,我也看到了崩溃。当为UICollectionView创建UICollectionViewCells时,我能够跟踪它所做的事情。当我拔出它并运行它是在更新列表之前,它工作得很好...完全一样的代码。

iOS内部似乎存在与此相关的错误,至少在UICollectionViewDataSource:collectionView:cellForItemAtIndexPath:

内部被调用的情况

答案 2 :(得分:0)

它需要在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

enter image description here

您还可以同时更改字符串编码和属性编码。结果也将可读。如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