如何仅更改NSTextView中整个样式文本的字体大小

Ind*_*oor 11 macos cocoa objective-c nstextview

我需要设置使用多种字体的所选富文本的文本大小(例如,42).

我想我可以检查每组字符的属性,修改字体大小和设置属性,但是看看浮动字体面板,似乎应该有一种非常简单直接的方法来实现它.我想念一些明显的东西吗?

Jon*_*ell 11

在10.6上,有一种方便的方法来迭代属性并增加字体大小.可以将此方法添加到NSTextView类别.

    - (IBAction)increaseFontSize:(id)sender
{
    NSTextStorage *textStorage = [self textStorage];
    [textStorage beginEditing];
    [textStorage enumerateAttributesInRange: NSMakeRange(0, [textStorage length])
                                     options: 0
                                  usingBlock: ^(NSDictionary *attributesDictionary,
                                                NSRange range,
                                                BOOL *stop)
     {
#pragma unused(stop)
         NSFont *font = [attributesDictionary objectForKey:NSFontAttributeName];
         if (font) {
             [textStorage removeAttribute:NSFontAttributeName range:range];
             font = [[NSFontManager sharedFontManager] convertFont:font toSize:[font pointSize] + 1];
             [textStorage addAttribute:NSFontAttributeName value:font range:range];
         }
     }];
    [textStorage endEditing];
    [self didChangeText];

}
Run Code Online (Sandbox Code Playgroud)


Kae*_*ure 8

概括了Jonathan的答案,这里是一个类别界面,您可以简单地粘贴到Xcode项目中的相应文件中:

@interface NSTextView (FrameworkAdditions)

-  (IBAction)decrementFontSize:(id)sender;
-  (IBAction)incrementFontSize:(id)sender;

@end
Run Code Online (Sandbox Code Playgroud)

并相应的实施:

@implementation NSTextView (FrameworkAdditions)

- (void)changeFontSize:(CGFloat)delta;
{
    NSFontManager * fontManager = [NSFontManager sharedFontManager];
    NSTextStorage * textStorage = [self textStorage];
    [textStorage beginEditing];
    [textStorage enumerateAttribute:NSFontAttributeName
                            inRange:NSMakeRange(0, [textStorage length])
                            options:0
                         usingBlock:^(id value,
                                      NSRange range,
                                      BOOL * stop)
     {
         NSFont * font = value;
         font = [fontManager convertFont:font
                                  toSize:[font pointSize] + delta];
         if (font != nil) {
             [textStorage removeAttribute:NSFontAttributeName
                                    range:range];
             [textStorage addAttribute:NSFontAttributeName
                                 value:font
                                 range:range];
         }
     }];
    [textStorage endEditing];
    [self didChangeText];
}

-  (IBAction)decrementFontSize:(id)sender;
{
    [self changeFontSize:-1.0];
}

-  (IBAction)incrementFontSize:(id)sender;
{
    [self changeFontSize:1.0];
}

@end
Run Code Online (Sandbox Code Playgroud)


Pet*_*isu 4

这将使字体大小加倍,但您可以将缩放属性更改为任何值,或提供固定大小

    NSFont * font = ...;
    CGFloat fontSize =  [[font fontDescriptor].fontAttributes[NSFontSizeAttribute] floatValue];
    font = [NSFont fontWithDescriptor:[font fontDescriptor] size:fontSize * 2.];

    self.textField.font = font;
Run Code Online (Sandbox Code Playgroud)