2017-10-25 17 views
0

CoreText API를 사용하여 문자열을 렌더링하려고합니다. 각 문자는 CAShapeLayer 렌더링하고이 같은 각 문자에 대해 (X 좌표) 오프셋 레이어를 얻고있다 :CoreText를 사용하여 문자열을 렌더링 할 때 커닝 문제가 발생했습니다.

let offset = CTLineGetOffsetForStringIndex(line, glyphIndex, nil) 

을하지만이 문자 사이에 커닝을 존중하지 않는 것 오프셋 결과적. 내 사용자 지정 레이블에서 "A"편지를 볼 수 있듯이

enter image description here

추가로 배치됩니다 : - 여기가 무슨 뜻인지의 이미지가 상단 라벨은 UILabel의이며, 하단 하나는 내 사용자 지정 렌더링 "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; 
}