'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;

1 answer

  • answered 2018-05-16 06:39 PPL

    Can you please try below, Just try it and let me know it is working or not.

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

    If you are uncertain of the correct encoding you should use NSUTF8StringEncoding

    UPDATE

    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;
    

    It is working perfectly with NSUnicodeStringEncoding.