1

I'm new in IOS. So my problem is, that I need to get kerning between two glyphs. First I tried like this:

NSString *text = @"AB";
NSDictionary* stringAttrs = @{ NSFontAttributeName : self.level.font,         
NSKernAttributeName:@0.0f };
CGSize size = [text sizeWithAttributes:stringAttrs];

//For better understanding
NSString *textA = @"A";
NSString *textB = @"B";
CGSize sizeA = [text sizeWithAttributes:stringAttrs];
CGSize sizeB = [text sizeWithAttributes:stringAttrs];
double kerning = size.width - sizeA.width - size.widthB;

I thought that, for single symbol width would be without advance. But I was wrong. It's did't help me. Than I tried CoreText.

CFStringRef str = (__bridge CFStringRef)@"A";
CFIndex count = CFStringGetLength(str);

CTFontRef font = CTFontCreateWithName((CFStringRef)@"Lora", 100, NULL);

UniChar *characters = (UniChar *)malloc(sizeof(UniChar) * count);
CGGlyph *glyphs = (CGGlyph *)malloc(sizeof(CGGlyph) * count);

CFStringGetCharacters(str, CFRangeMake(0, count), characters);
CTFontGetGlyphsForCharacters(font, characters, glyphs, count);

CGSize *sizeCore = malloc(count * sizeof(CGSize));
CGRect *rectCore = malloc(count * sizeof(CGRect));

double adv = CTFontGetAdvancesForGlyphs (font,
                                         kCTFontHorizontalOrientation,
                                         glyphs,
                                         sizeCore,
                                         count);

CGRect rect = CTFontGetBoundingRectsForGlyphs (font,
                                               kCTFontHorizontalOrientation,
                                               glyphs,
                                               rectCore,
                                               count);

double kerning = adv - rect.size.width; (== 0)

adv gave the same value as rect.size.width and even the same like in first try size.width

Do you have any gues? Or link were I can find an answer. Sorry for mistakes.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Ihor Bykov
  • 1,843
  • 3
  • 15
  • 22

1 Answers1

0

I found kerning not to work with certain fonts in ios 7. See my answer to this question: Why Does Kerning fail for NSAttributedString in IOS7

Maybe it works with different fonts...

Community
  • 1
  • 1
naeger
  • 410
  • 3
  • 17