0

I'm trying to get the location of glyphs in an NSLayoutManager, but on a few isolated occasions I'm getting what appears to be incorrect values.

For example: two f's together in Helvetica Neue size 16. The x value of the 2nd f should be around 4, but I'm getting 9.232000..? Anyone know why this is?

NSString *lettersString = @"ff";
NSDictionary *attrsDictionary = @{
    NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue" size:16]
};

NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:lettersString attributes:attrsDictionary];
NSTextStorage *textStorage = [[NSTextStorage alloc] initWithAttributedString:attributedString];

NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
[textStorage addLayoutManager:layoutManager];
NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:CGSizeMake(100, 100)];
textContainer.lineFragmentPadding = 0;

[layoutManager addTextContainer:textContainer];

CGPoint location = [layoutManager locationForGlyphAtIndex:1];

// location.x == 9.232000
user2994359
  • 400
  • 4
  • 13
  • 1
    I think 9.232000 is a proper value for 16th HelveticaNeue. I've created a label with those parameters and its intrinsic size is 14x23. – Daniyar May 28 '15 at 06:12
  • @Astoria if I have 'fff', then the position of the 3rd letter also comes back as 9.232000...? How can the 2nd and 3rd letter both have the same x position in the string. – user2994359 May 28 '15 at 12:22

1 Answers1

1

Solved! iOS automatically resolves ligatures (one or more letters joined as a single glyph) unless explicitly disabled in the string attributes.

So the ff characters in this example are converted into a single character at \uniFB00 - which is of course wider.

user2994359
  • 400
  • 4
  • 13
  • OMG thanks. I was beating my head against a wall for a good 30 minutes about why "fi" was giving weird values for locationForGlyphAtIndex. – Corey Zambonie Mar 06 '19 at 19:50