0

I have added attributed string on label.I am displaying html text which works fine but by default it is always showing Times new roman family. Please tell how can i change text family.

NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData:[inst.desc dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
Larme
  • 24,190
  • 6
  • 51
  • 81
Dheeraj Kumar
  • 327
  • 2
  • 6
  • 19
  • The fastest way would be to edit the `inst.desc` and add it there in HTML format style. Else, you would have to enumerate the whole attributed string for NSFontAttributeName. – Larme Mar 17 '16 at 08:54
  • not able to set html content in ur code – Dheeraj Kumar Mar 17 '16 at 09:04

2 Answers2

10

Try with this:

NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData:[inst.desc dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
NSMutableAttributedString *newString = [[NSMutableAttributedString alloc] initWithAttributedString:attributedString];
NSRange range = (NSRange){0,[newString length]};
[newString enumerateAttribute:NSFontAttributeName inRange:range options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired usingBlock:^(id value, NSRange range, BOOL *stop) {
    UIFont *replacementFont =  [UIFont fontWithName:@"Palatino-Roman" size:14.0];
    [newString addAttribute:NSFontAttributeName value:replacementFont range:range];
}];
self.label.attributedText = newString;
LorenzOliveto
  • 7,796
  • 1
  • 20
  • 47
5

you can do like that:

NSDictionary *attrDict = @{
    NSFontAttributeName : [UIFont fontWithName:Arial size:16.0],
    NSForegroundColorAttributeName : [UIColor redColor]
};
 NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:@"string" attributes:attrDict];
Piyush
  • 1,534
  • 12
  • 32