我是IOS的新手。 所以我的问题是,我需要在两个字形之间进行字距调整。 首先我尝试这样:
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;
我认为,对于单个符号宽度将没有提前。但是我错了。它对我没有帮助。比我尝试过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
给出了与rect.size.width
相同的价值,即使是第一次尝试也是如此size.width
答案 0 :(得分:0)
我发现kerning不能使用ios 7中的某些字体。请参阅我对这个问题的回答: Why Does Kerning fail for NSAttributedString in IOS7
也许它适用于不同的字体......