Search code examples
iosobjective-cstringnsmutableattributedstring

How can I scan a string for numbers to adjust the font size in an iOS app?


I'm off to a good start. But I would like to make this code more dynamic.

NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:string];
UIFont *font = [UIFont systemFontOfSize:10.0f];
UIFont *smallFont = [UIFont systemFontOfSize:9.0f];

[attString beginEditing];
[attString addAttribute:NSFontAttributeName value:(font) range:NSMakeRange(0, string.length - 2)];

[attString addAttribute:NSFontAttributeName value:(smallFont) range:NSMakeRange(string.length - 1, 1)];

Say I have the following String:

@"C3OC2OH4"

I would like to adjust the font size of the numbers only to fulfill a chemistry application. How can I scan the above string, and create a series of custom ranges to plug into my function above, which adjusts the font size?


Solution

  • You can enumerate the characters and apply the attributes like this.

    NSString * string = @"C3OC2OH4";
    
    NSMutableAttributedString * attributedString = [NSMutableAttributedString new];
    
    NSDictionary * subscriptAttributes = @{NSFontAttributeName : [UIFont systemFontOfSize:10.0],
                                           NSBaselineOffsetAttributeName : @(-5.0)};
    
    [string enumerateSubstringsInRange:NSMakeRange(0, [string length])
                               options:NSStringEnumerationByComposedCharacterSequences
                            usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
    
                                NSCharacterSet * letterCharacterSet = [NSCharacterSet letterCharacterSet];
    
                                if ([substring rangeOfCharacterFromSet:letterCharacterSet].location != NSNotFound)
                                    [attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:substring]];
                                else
                                    [attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:substring attributes:subscriptAttributes]];
    
                            }];
    

    In this case substring is each character and is appended without any change if it is a letter. If it is a number we change the font size and shift the baseline. Change them according to your need.

    Good luck.