2017-10-25 9 views
0

CoreText APIを使用して文字列をレンダリングしようとしています。各文字はCAShapeLayerでレンダリングされており、私はこのような文字ごとに(X座標)オフセット層を取得しています:CoreTextを使用して文字列をレンダリングするときのカリングの問題

let offset = CTLineGetOffsetForStringIndex(line, glyphIndex, nil) 

しかし、結果のオフセットは、文字間のカーニングを尊重していないようです。あなたは私のカスタムラベルの「」の文字を見ることができるように

enter image description here

がさらに配置されています - ここで私が何を意味するかのイメージがあるトップラベルはUILabelで、下の1は私のカスタムレンダリングです「W」の手紙から。

本当に助けていただければ幸いです。 ありがとうございます。

答えて

0

誰かが同じ問題を抱えている場合は、私はこの場所からのアプローチを使用しました。https://github.com/MichMich/XMCircleType/blob/master/XMCircleType/Views/XMCircleTypeView.m、kerningForCharacter関数。関数自体は次のとおりです。

- (float)kerningForCharacter:(NSString *)currentCharacter afterCharacter:(NSString *)previousCharacter 
{ 
    //Create a unique cache key 
    NSString *kerningCacheKey = [NSString stringWithFormat:@"%@%@", previousCharacter, currentCharacter]; 

    //Look for kerning in the cache dictionary 
    NSNumber *cachedKerning = [self.kerningCacheDictionary objectForKey:kerningCacheKey]; 

    //If kerning is found: return. 
    if (cachedKerning) { 
     return [cachedKerning floatValue]; 
    } 

    //Otherwise, calculate. 
    float totalSize = [[NSString stringWithFormat:@"%@%@", previousCharacter, currentCharacter] sizeWithAttributes:self.textAttributes].width; 
    float currentCharacterSize = [currentCharacter sizeWithAttributes:self.textAttributes].width; 
    float previousCharacterSize = [previousCharacter sizeWithAttributes:self.textAttributes].width; 

    float kerning = (currentCharacterSize + previousCharacterSize) - totalSize; 

    //Store kerning in cache. 
    [self.kerningCacheDictionary setValue:@(kerning) forKey:kerningCacheKey]; 

    //Return kerning. 
    return kerning; 
} 
関連する問題