Question

I found a strange behaviour of NSAttributedString in iOS 7 with system font when using "ff" string. It's counted as single letter. Here is result and code to test.

aaaaaa

ffffff

Is it bug or feature? How can I get rid of this without changing font?

If I change font to anything else letters are colored correctly.

@property (nonatomic) IBOutlet UILabel *testText

- (void)testALetter
{
    NSDictionary *blackText = @{NSForegroundColorAttributeName : [UIColor blackColor],
                                         NSFontAttributeName : [UIFont systemFontOfSize:48]};

    NSDictionary *orangeText = @{NSForegroundColorAttributeName : [UIColor orangeColor],
                                           NSFontAttributeName : [UIFont systemFontOfSize:48]};

    NSRange range = NSMakeRange(2, 1);

    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"aaaaaa" attributes:blackText];
    [attributedString setAttributes:orangeText range:range];

    [self.testText setAttributedText:attributedString];
}

- (void)testFLetter
{
    NSDictionary *blackText = @{NSForegroundColorAttributeName : [UIColor blackColor],
                                NSFontAttributeName : [UIFont systemFontOfSize:48]};

    NSDictionary *orangeText = @{NSForegroundColorAttributeName : [UIColor orangeColor],
                                 NSFontAttributeName : [UIFont systemFontOfSize:48]};

    NSRange range = NSMakeRange(2, 1);

    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"ffffff" attributes:blackText];
    [attributedString setAttributes:orangeText range:range];

    [self.testText setAttributedText:attributedString];
}
Was it helpful?

Solution

It is to be expected. This is a feature of the font called a ligature.

Check out NSLigatureAttributeName for options related to ligatures.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top