2012-04-03 3 views
2

OSX에서 Windows CreateFont(..) 기능을 대체 할 수 있습니까?OSX에서 지정된 특성으로 글꼴 찾기

및 해당 matchingFontDescriptorsWithMandatoryKeys: 메서드를 사용해 보았지만이 메서드는 "가장 가까운"글꼴을 찾지 못했고 일치하는 특성을 찾았으며 nil을 반환 할 수 있습니다.

OSX에서 지정된 특성 (예 : CreateFont)으로 가장 가까운 글꼴을 찾는 방법이 있습니까? 코코아에

:

NSDictionary* fontTraits = [NSDictionary dictionaryWithObjectsAndKeys: 
          [NSNumber numberWithInt:NSFontSansSerifClass], NSFontSymbolicTrait, 
          [NSNumber numberWithFloat:0.4], NSFontWidthTrait, 
          nil]; 

NSDictionary* fontAttributes = [NSDictionary dictionaryWithObjectsAndKeys: 
        fontTraits, NSFontTraitsAttribute, 
        nil]; 

NSFontDescriptor* fontDescriptor = [NSFontDescriptor fontDescriptorWithFontAttributes:fontAttributes]; 

NSArray* matchedDescriptors = [fontDescriptor matchingFontDescriptorsWithMandatoryKeys:nil]; 

그리고 CoreText API 사용 :

CFMutableDictionaryRef fontTraits = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); 

float weight = 0.4; 

CFNumberRef fontWeight = CFNumberCreate(kCFAllocatorDefault, kCFNumberFloat32Type, &weight); 

CFDictionaryAddValue(fontTraits, kCTFontWeightTrait, fontWeight); 

int symbolicTraits = kCTFontSansSerifClass; 

CFNumberRef fontSymbolicTraits = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &symbolicTraits); 

CFDictionaryAddValue(fontTraits, kCTFontSymbolicTrait, fontSymbolicTraits); 

CFMutableDictionaryRef fontAttributes = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); 

CFDictionaryAddValue(fontAttributes, kCTFontTraitsAttribute, fontTraits); 

CTFontDescriptorRef fontDescriptor = CTFontDescriptorCreateWithAttributes(fontAttributes); 

CFArrayRef matchedDescriptors = CTFontDescriptorCreateMatchingFontDescriptors(fontDescriptor, 0); 

내가 코드의 두 가지가

.. NSFontDescriptor와

뭔가 이상한 일 업데이트

두 경우 모두 동일한 글꼴 설명자를 만들었지 만, 첫 번째 경우 matchedDescriptorsnil이고 CoreText API의 경우 matchedDescriptors에 글꼴이 있습니다. 그게 버그 야?

그러나 일반적으로 필수 속성으로 nil에서 XXXMatchingFontDescriptorsXXX을 전달하면 적어도 하나의 설명자를 반환해야합니까?

답변

0

내가 갖고 있지 않다면 레이더 파일을 보내주십시오. 나는 행동 자체에서 이와 똑같은 차이점을 조금씩 가지고있다.

놀랍게도 디버깅하는 동안 실제로 문서화되지는 않았지만 [fontDescriptor matchingFontDescriptorWithMandatoryKeys:]이 정의되어 있고 단일 설명자에 대해 예상대로 작동한다는 것을 알았습니다. 의미는 마치 CTFontDescriptorCreateMatchingFontDescriptor()처럼 작동합니다.

[fontDescriptor matchingFontDescriptorsWithMandatoryKeys:]은 분명히 나에게 단절된 것처럼 보입니다.

[UIFont fontWithDescriptor:size:]을 사용했을 때이 문제가 발견되어 예상대로 잘 맞았습니다. 그런 다음 [fontDescriptor matchingFontDescriptorsWithMandatoryKeys:nil]으로 전환했으며 동일한 설명자에 대해 nil을 반환했습니다.

뭔가 느낌이 들었으므로 CoreText API와 비교해 보았습니다. 결론을 내 렸습니다.

- (nullable instancetype)my_matchingFontDescriptorWithMandatoryKeys:(nullable NSSet<UIFontDescriptorAttributeName> *)mandatoryKeys { 
    return (__bridge_transfer UIFontDescriptor *)CTFontDescriptorCreateMatchingFontDescriptor((__bridge CTFontDescriptorRef)self, (__bridge CFSetRef)mandatoryKeys); 
} 
: 나는 이러한 방법의 목록 버전을보고 알고

, 나는 내가 UIFontDescriptor 범주에이 방법으로 갔다 내가 공유 할 생각