4

As per apple's documentation, if you dont provide any font for given range of attributed string, by default it will take Helvetica font of size 12.

Now rightnow i want to change that default font. Can anyone suggest me how can i do that.

Actually i want to achieve following thing:

i have string with html(dynamic html coming from server), let say it as htmlStr. In this string, it is possible that font have been set for different parts of html, and it is also possible that there might not be any font set for that htmlStr.

Now, as i want to show+edit this htmlStr, So first i am converting this htmlStr into attributedString, then showing it in textview by setting attributedText for textview.

Now my problem is, in case when there is no any font is provided for this htmlStr, it takes default font which of too much small size and not looking good. So, How can i change this default font for attributed string.

Mehul Thakkar
  • 12,178
  • 9
  • 52
  • 74
  • Either edit the HTML string to force a font size, or enumerate the attributedString and add the correct font. – Larme Jun 13 '17 at 11:14
  • How to enumerate attributedstring to check font? – Mehul Thakkar Jun 13 '17 at 11:15
  • actually html is coming from any random places, so we cant enforce them from server, either we can update html by adding some default font(i dont know much about html, so asking) or i need to update default font, but i am unable to do it. – Mehul Thakkar Jun 13 '17 at 11:16
  • 1
    I wasn't suggesting from server, but edit `htmlStr` before transforming it into NSAttributedString. – Larme Jun 13 '17 at 11:34
  • 1
    Else https://stackoverflow.com/questions/31546844/attributed-text-replace-a-specific-font-by-another-using-swift https://stackoverflow.com/questions/19386849/looping-through-nsattributedstring-attributes-to-increase-font-size https://stackoverflow.com/questions/19921972/parsing-html-into-nsattributedtext-how-to-set-font https://stackoverflow.com/questions/25401742/apply-custom-font-to-attributed-string-which-converts-from-html-string – Larme Jun 13 '17 at 11:36

2 Answers2

4

Try

Objective - C

__block BOOL found = NO;
    NSMutableAttributedString *attrString = ...
[attrString beginEditing];
    [attrString enumerateAttribute:NSFontAttributeName inRange:NSMakeRange(0, attrString.length) options:0 usingBlock:^(id value, NSRange range, BOOL *stop) {
        if (value) {
            UIFont *oldFont = (UIFont *)value;
            UIFont *newFont = [oldFont fontWithSize:oldFont.pointSize * 2];
            [attrString addAttribute:NSFontAttributeName value:newFont range:range];
            found = YES;
        }
    }];
    if (!found) {
        // No font was found - do something else?
    }
    [attrString endEditing];

Swift 4

var found = false
var attrString = NSMutableAttributedString()

attributedText.enumerateAttribute(NSAttributedStringKey.font, in: NSMakeRange(0, attributedText.length), options: NSAttributedString.EnumerationOptions(rawValue: 0)) { (value, range, stop) in
        if let oldFont = value as? UIFont {
            let newFont = oldFont.withSize(oldFont.pointSize * 2)
            attributedText.addAttribute(NSAttributedStringKey.font, value: newFont, range: range)
            found = true
        }
    }

    if !found {
        // No font was found - do something else?
    }

attributedText.endEditing()
Laszlo
  • 2,783
  • 2
  • 26
  • 33
Piyush
  • 2,530
  • 13
  • 36
0
    Try this one also:-

- (NSAttributedString *) convertToAttributedString: (NSString *) string withStringToConvert: (NSString *) str2 {
    NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:string];
    [attributedStr addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, string.length)];
    [attributedStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:FONT_APPLE_SD_GOTHIC_NEO_BOLD size:17.0] range:NSMakeRange(0, string.length)];

    [attributedStr addAttribute:NSForegroundColorAttributeName value:[UIColor darkGrayColor] range:NSMakeRange(0,str2.length)];
    [attributedStr addAttribute:NSFontAttributeName value: [UIFont fontWithName:FONT_APPLE_SD_GOTHIC_NEO size:17.0] range:NSMakeRange(0, str2.length)];

    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    [style setLineSpacing:3];
    [attributedStr addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, string.length)];

    return attributedStr;
}
Kamal Thakur
  • 380
  • 2
  • 8