0

[UIFont familyNames]でフォントファミリ名のリストを取得し、でファミリのフォント名を反復処理できることを知ってい[UIFont fontNamesForFamilyName:]ます。どのフォント名が「通常の」フォントを表しているかを知る方法はありますか?命名には一貫性がありません(「Roman」、「Regular」、または修飾形容詞がない場合もあります)。私は最終的に、NSAttributedString既存の特性と装飾を維持しながら、しばらくの間サブストリング上でフォントを変更しようとしています。前もって感謝します。

4

2 に答える 2

1

さて、CoreText のドキュメントをもっと詳しく読む必要があります。適切な CoreText ルーチンを使用すれば、フォント ファミリ名を渡すだけで十分です...

NSString *fontFamilyName = ...(font family name)...;
// Make a mutable copy of the attributed text
NSMutableAttributedString *workingAttributedText = [self.attributedText mutableCopy];

// Over every attribute run in the selected range...
[workingAttributedText enumerateAttributesInRange:self.selectedRange
    options:(NSAttributedStringEnumerationOptions) 0
    usingBlock:^(NSDictionary *attrs, NSRange range, BOOL *stop) {
        // get the old font
        CTFontRef oldFont = (__bridge CTFontRef)[attrs objectForKey:NSFontAttributeName];
        // make a new one, with our new desired font family name
        CTFontRef newFontRef = CTFontCreateCopyWithFamily(oldFont, 0.0f, NULL, (__bridge CFStringRef)fontFamilyName);
        // Convert it to a UIFont
        UIFont *newFont = [UIFont fontWithCTFont:newFontRef];
        // Add it to the attributed text
        [workingAttributedText addAttribute:NSFontAttributeName value:newFont range:range];
    }];

// Replace the attributed text with the new version
[self setAttributedText:workingAttributedText];

これを行う簡単な方法がある場合は、それについて聞いてみたいです。

于 2013-01-29T21:02:07.120 に答える
0

リストに通常のフォントが含まれている必要はありません。太字のみ、または斜体のみを提供するものもあります。これが、ユーザーが決定できるようにリストが提供されることが多い理由です。

于 2013-01-29T18:14:52.610 に答える