0

I have a NSString with this format:

<p>When: Every day except Monday at 11:45am

</p><p>Where: Main Pool</p><p><br></p><p>Get refreshed in the main pool during the warm sunny days whilst participating in our animation's fit activities. Enjoy our aqua fitness 6 times per week.<br></p>

I use this code to display the html attributes and also set a default font size and color:

NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithData:[self.Description dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];

        __block BOOL found = NO;
        [attrStr beginEditing];
        [attrStr enumerateAttribute:NSFontAttributeName inRange:NSMakeRange(0, attrStr.length) options:0 usingBlock:^(id value, NSRange range, BOOL *stop) {
            if (value) {
                UIFont *oldFont = (UIFont *)value;
                UIFont *newFont = [oldFont fontWithSize:16];
                [attrStr addAttribute:NSFontAttributeName value:newFont range:range];
                [attrStr addAttribute:NSForegroundColorAttributeName
                               value:[UIColor darkGrayColor]
                               range:range];

                found = YES;
            }
        }]; 
        [attrStr endEditing];


cell.descLbl.attributedText = attrStr;

Those words doesn't show up:

fitness 6 times per week.<br></p>

it only shows some weird dots-lines at the top of where the letters should be.

I believe the problem is when I change the String to unicode, the length of the String changes and so the range. How can I fix this?

Student
  • 418
  • 1
  • 4
  • 17
  • can you show me the output? because your code works perfectly on my side – Priya Jun 28 '18 at 11:20
  • @Priya https://scontent.fath2-1.fna.fbcdn.net/v/t1.0-9/36367588_1305880409544861_6333541035913773056_n.jpg?_nc_cat=0&oh=cdd1337141a89e2bbcc92a131a7177b5&oe=5BAFDC34 – Student Jun 28 '18 at 11:30
  • Maybe for some reason it adds one more empty line, but I don't know why. – Student Jun 28 '18 at 11:31
  • try `[cell.contentView layoutIfNeeded];` after you set attributed string – Priya Jun 28 '18 at 11:33
  • Didn't work.... – Student Jun 28 '18 at 11:36
  • Try to add `[cell.descLbl.attributedText sizeToFit];` after `cell.descLbl.attributedText = attrStr;` – AnthoPak Jun 28 '18 at 11:40
  • No visible @interface for 'NSAttributedString' declares the selector 'sizeToFit' – Student Jun 28 '18 at 11:47
  • 1
    What are the constraints for the label? Have you add this lines in viewDidLoad?`self.tableView.estimatedRowHeight = 100; self.tableView.rowHeight = UITableViewAutomaticDimension;` – Priya Jun 28 '18 at 11:52
  • Sorry @Student, it was `[cell.descLbl sizeToFit];` – AnthoPak Jun 28 '18 at 11:58
  • 1
    The label is small, nothing to do with the encoding or the parsing. Also, if you use Debug Hierarchy View in XCode, is the label frame correct and goes out of bounds for the lower part, or is the cell auto dimension causing issue? – Larme Jun 28 '18 at 12:07
  • Yea you were right Larme. It was a height problem. – Student Jun 29 '18 at 08:26

0 Answers0