2014-07-15 46 views
1

我是IOS新手。 所以我的問題是,我需要得到兩個字形之間的字距。 首先我想是這樣的:在IOS 7中獲取內核號碼

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

你有任何客串?或者我可以找到答案。 對不起。

回答