0

I have a NSString stored in cell.lblTitle.text. I am converting this NSString to NSMutableAttributedString with following code.

text = [[NSMutableAttributedString alloc] initWithData:[cell.lblTitle.text dataUsingEncoding:NSUTF8StringEncoding]
                                               options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)}
                                    documentAttributes:nil error:nil];

[text setAttributes:@{NSFontAttributeName:labelFont} range:NSMakeRange(0, [text length])];
[text addAttribute:NSForegroundColorAttributeName
             value:[UIColor darkGrayColor]
             range:NSMakeRange(0, [text length])];

When I print cell.lblTitle.text and its length in console then the following is my output:

po cell.lblTitle.text

Address: HM<MM-
UU@FF-
Mobile,
Alabama,
123456-
United States

po [cell.lblTitle.text length]
61

And when I print text which is my NSMutableAttributedString. The output in console is:

po text
Address: HM{
    NSColor = "UIDeviceWhiteColorSpace 0.333333 1";
    NSFont = "<UICTFont: 0x7aebc0f0> font-family: \"Futura-Medium\"; font-weight: normal; font-style: normal; font-size: 15.00pt";
}

po [text length]
11

Following is my crash log:

Terminating app due to uncaught exception 'NSRangeException', reason: 'NSMutableRLEArray objectAtIndex:effectiveRange:: Out of bounds'

So, the string after special character "<" is not identified and my app crashes. How can I manage this special character "<" so I get the output [text length] = 61.

halfer
  • 19,824
  • 17
  • 99
  • 186
iPhone
  • 4,092
  • 3
  • 34
  • 58
  • is this ( ) ... ? or not – Arun Jun 05 '15 at 05:51
  • no that is not actually tag. it is treated as a special character in this case. – iPhone Jun 05 '15 at 05:53
  • add the crash log please ? – Arun Jun 05 '15 at 05:54
  • please refer updated answer for crash log – iPhone Jun 05 '15 at 05:57
  • [text length] - 1 put and check and which line did you face crash ? – Arun Jun 05 '15 at 06:01
  • Actually as a output I want entire text of cell.lblTitle.text whose lenght is 61 but when I convert it to NSMutableAttributedString its length is 11. So, when I convert it to NSMutableAttributedString I am not able to get length 61. That is my main issue. If I got length of NSMutableAttributedString as 61 then perhaps my problem will be resolved. – iPhone Jun 05 '15 at 06:04
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/79734/discussion-between-spynet-and-iphone). – Arun Jun 05 '15 at 06:05
  • Why using `NSHTMLTextDocumentType`? Is there really HTML in your text? – Larme Jun 05 '15 at 16:09

1 Answers1

0

Your text isn't html so don't try to create it as such. Just use initWithString: to create the attributed string directly from the text.

Wain
  • 118,658
  • 15
  • 128
  • 151