1

in my app i have to change the font of a part of string which comes from JSON response

> "<span class=\"drop_data_user\">Andrew James</span> liked your comment
> \"hiiiiiiiiiiiiiii\" that you posted."

to convert it in attributed string i am using the following code

NSAttributedString *attr = [[NSAttributedString alloc] initWithData:[NotificationTxt dataUsingEncoding:NSUTF8StringEncoding]
                                                                options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
                                                                          NSCharacterEncodingDocumentAttribute:@(NSUTF8StringEncoding)}
                                                     documentAttributes:nil
                                                                  error:nil];

I want to change the font colour of the sender

Larme
  • 24,190
  • 6
  • 51
  • 81
Abhinandan Pratap
  • 2,142
  • 1
  • 18
  • 39
  • How do you define that "Andrew James" is the sender? Will it always be between that particular span html tag? – Larme Feb 05 '18 at 09:03
  • yes it always be in between span tag – Abhinandan Pratap Feb 05 '18 at 09:05
  • Since that tag won't be "interpreted" but committed (tested on Playground), I'd suggest to find it with `rangeOfString:`, a `NSRegularExpression` (if it's always the same one), `NSScanner`, etc. and add (or replace it) yourself a HTML tag defining a color for it. – Larme Feb 05 '18 at 09:12
  • this might be helpful https://stackoverflow.com/questions/22090597/how-to-add-css-of-an-html-to-nsattributedstring – heximal Feb 05 '18 at 09:41
  • did you try my suggestion? – Milan Nosáľ Feb 05 '18 at 19:20

3 Answers3

1

Also, you can use it as follows.

NSString *dateString = [NSString stringWithFormat:@"%@%@", @"Teslimat: ", selectedReservationModel.DateLabel];
        NSMutableAttributedString *attrS = [[NSMutableAttributedString alloc] initWithString: dateString];
        [attrS addAttribute:NSFontAttributeName value:[GenericUtility getOpenSansBoldFontSize:12] range:NSMakeRange(0, 8)];
        [attrS addAttribute:NSFontAttributeName value:[GenericUtility getOpenSansRegularFontSize:12] range:NSMakeRange(9, selectedReservationModel.DateLabel.length)];
        lblDeliveryDate.attributedText = attrS;
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
sametytmz
  • 9
  • 2
0

I believe the easiest way to fulfil your task is to use CSS inlined in the text, then parsing HTML as you do will change the color too (I'm using Swift syntax, but I believe you can easily convert it to ObjC):

let text = "<span class=\"drop_data_user\">Andrew James</span> liked your comment \"hiiiiiiiiiiiiiii\" that you posted."
let colouredAuthorText = "\(text)<style>.drop_data_user { color: #FEB600; }</style>"

Then just parse colouredAuthorText (which is created by appending style to the original text) instead of the original, and you should be good to go.

Milan Nosáľ
  • 19,169
  • 4
  • 55
  • 90
0
NSMutableAttributedString *attrS = [[NSMutableAttributedString alloc] initWithString:@"My String"];
[attrS addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 2)];
[attrS addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(3, 6)];

self.myLabel.attributedText = attrS;

For fonts use

[attrS addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:20] range:NSMakeRange(0, 3)];
Ryan Heitner
  • 13,119
  • 6
  • 77
  • 119