1
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;
Nitish
  • 13,845
  • 28
  • 135
  • 263
jaydev
  • 1,629
  • 5
  • 17
  • 31
  • what is str in your htmlString? – PPL May 16 '18 at 06:05
  • str is string which is contain urdu text from file in it . – jaydev May 16 '18 at 06:16
  • 1
    @jaydev Please, add some example of `str` that would crash the code. – Sulthan May 16 '18 at 07:00
  • It is not being crashed for every urdu text. I tried with `تم کیسی ہو؟` so please add an example text which will crash the code. – TheTiger May 16 '18 at 07:19
  • Adding `` in ` <\head>` tag showing correct result. I read it [here](https://github.com/webpack/webpack-dev-server/issues/1) and this is my [example file](https://pastebin.com/8KGxPSJp). – TheTiger May 16 '18 at 07:50

3 Answers3

2

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.

PPL
  • 6,357
  • 1
  • 11
  • 30
  • Please, explain the difference between Unicode and UTF8 encoding if possible. – Sulthan May 16 '18 at 06:43
  • You can visit [this](https://stackoverflow.com/questions/643694/what-is-the-difference-between-utf-8-and-unicode) – PPL May 16 '18 at 06:44
  • I know the difference but it should be part of the answer. Also it's not clear from your answer why a specific encoding (UTF8) must be used. – Sulthan May 16 '18 at 06:49
  • Hi @PPL i have try this code but now text is not displaying in Urdu format . and text is not readable . – jaydev May 16 '18 at 06:56
  • 1
    @jaydev Does your font support it? Did you load the text properly? – Sulthan May 16 '18 at 07:00
  • @jaydev Also, you should have `` in your html. – Sulthan May 16 '18 at 07:03
  • Hi @PPL i have include [NSString stringWithFormat:@" %@ ",str]; but still its showing wrong string format . – jaydev May 16 '18 at 07:09
  • @jaydev as Sulthan said, Please, add some example of str that would crash the code – PPL May 16 '18 at 07:10
  • hi @PPL مُدّعا عَنقا ہے اپنے عالَمِ تقریر کا میری تقریر کا مفہوم چونکہ عنقا یعنی معدوم ہے اور معدوم – jaydev May 16 '18 at 07:14
  • @jaydev give me some time please – PPL May 16 '18 at 07:15
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/171129/discussion-between-ppl-and-jaydev). – PPL May 16 '18 at 07:27
  • Adding `` in ` <\head>` tag showing correct result. I read it [here](https://github.com/webpack/webpack-dev-server/issues/1) and this is my [example file](https://pastebin.com/8KGxPSJp). – TheTiger May 16 '18 at 07:48
  • OK Thanks @PPL for support – jaydev May 16 '18 at 09:02
0

I'm also seeing a crash, even in the most basic of examples. I was able to track it do doing it when creating UICollectionViewCells for a UICollectionView. When I pulled it out and ran is prior to updating the list, it worked fine...exact same code.

There seems to be a bug inside iOS related to this, at least is cases when it's called inside a UICollectionViewDataSource:collectionView:cellForItemAtIndexPath:

Darren Ehlers
  • 633
  • 6
  • 16
0

It needs to add character encoding in Option dict the same as the string encode. Otherwise. It will be unreadable code.

NSAttributedString.DocumentReadingOptionKey.characterEncoding:String.Encoding.utf8.rawValue

the whole code is

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

You also can change the string encoding and the attribute encoding at the same time. The result will be readable, too. Such as 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
Zgpeace
  • 3,927
  • 33
  • 31