Mac OS 获取和修改InspectorBar

  NSTextView 自带的 Inspector Bar 集成了样式(Style)、字体(FontFamily)、字体样式(FontStyle)、字体大小(FontSize)、字体颜色(TextColor)、字体背景颜色(TextBackgroudColor)、粗体-斜体-下划线(FontTrait)、文本对齐方式(TextAlignment)、行间距(LineHeight)和列表符号(TextList)。

  Step1: 让 NSTextView 使用 Inspector Bar。可以在 xib 文件中选中 NSTextView,勾选Inspector Bar:

  

  也可以在代码中实现:

[firstTextView setUsesInspectorBar:YES];

  Step2: 获取 Inspector Bar。通过遍历 [firstTextView.window.contentView superview].subviews ,打印 [subview class] ,可以看到有一个类名为 __NSInspectorBarView 的subview。这就是我们想要获取的 Inspector Bar。代码如下

    NSArray *subviews = [firstTextView.window.contentView superview].subviews;
    id inspectorBar = nil;
    for (NSView *subview in subviews) {
        NSLog(@"%@",[subview class]);
        if ([subview isKindOfClass:NSClassFromString(@"__NSInspectorBarView")]) {
            inspectorBar = subview;
        }
    }

  Step3: 获取 Inspector Bar 上的控件。代码如下

NSArray *inspectorBarItems = [_inspectorBar subviews];

  再通过数组的下标获取对应的控件。

  完整的代码如下:

   InspectorBarDemo.h //原意是想做一个通用的类,因此头文件并没有定义其他方法 

1 #import <Cocoa/Cocoa.h>
2 
3 @interface InspectorBarDemo : NSWindowController
4 
5 @end

   InspectorBarDemo.m 

  1 @interface InspectorBarFactory : NSObject{
  2     NSTextView *firstTextView;
  3 }
  4 @property NSPopUpButton *jStyle;
  5 @property NSPopUpButton *jFontFamily;
  6 @property NSPopUpButton *jFontStyle;
  7 @property id jFontSize;         // NSTexturedComboBox
  8 @property id jTextColor;        // NSPopoverColorWell
  9 @property id jTextBackgroudColor;// NSPopoverColorWell
 10 @property NSSegmentedControl *jFontTrait;
 11 @property NSSegmentedControl *jTextAlignment;
 12 @property NSPopUpButton *jLineHeight;
 13 @property NSPopUpButton *jTextList;
 14 
 15 @property id inspectorBar;
 16 
 17 - (id)initWithTextView:(NSTextView *)textView;
 18 @end
 19 
 20 @implementation InspectorBarFactory
 21 - (id)initWithTextView:(NSTextView *)textView{
 22     self = [super init];
 23     if (self) {
 24         firstTextView = textView;
 25         _inspectorBar = [self getInspectorBar];
 26         [self factoryForInspectorBarItems];
 27     }
 28     return self;
 29 }
 30 - (id)getInspectorBar{
 31     [firstTextView setUsesInspectorBar:YES];
 32     NSArray *subviews = [firstTextView.window.contentView superview].subviews;
 33     id inspectorBar = nil;
 34     for (NSView *subview in subviews) {
 35         NSLog(@"%@",[subview class]);
 36         if ([subview isKindOfClass:NSClassFromString(@"__NSInspectorBarView")]) {
 37             inspectorBar = subview;
 38         }
 39     }
 40     return inspectorBar;
 41 }
 42 - (NSArray *)inspectorBarItems{
 43     return [_inspectorBar subviews];
 44 }
 45 - (void)factoryForInspectorBarItems{
 46     NSArray *inspectorBarItems = [self inspectorBarItems];
 47     NSLog(@"%zi",inspectorBarItems.count);
 48     _jStyle = inspectorBarItems[0];
 49     _jFontFamily = inspectorBarItems[1];
 50     _jFontStyle = inspectorBarItems[2];
 51     _jFontSize = inspectorBarItems[3];
 52     _jTextColor = inspectorBarItems[4];
 53     _jTextBackgroudColor = inspectorBarItems[5];
 54     _jFontTrait = inspectorBarItems[6];
 55     _jTextAlignment = inspectorBarItems[7];
 56     _jLineHeight = inspectorBarItems[8];
 57     _jTextList = inspectorBarItems[9];
 58 }
 59 @end
 60 #import "InspectorBarDemo.h"
 61 
 62 @interface InspectorBarDemo ()
 63 @property (strong) IBOutlet NSTextView *firstTextView;
 64 @property (strong) IBOutlet NSBox *myBox;
 65 
 66 @property InspectorBarFactory *inspectorBarFactory;
 67 @end
 68 
 69 @implementation InspectorBarDemo
 70 
 71 - (id)initWithWindow:(NSWindow *)window
 72 {
 73     self = [super initWithWindow:window];
 74     if (self) {
 75 
 76     }
 77     return self;
 78 }
 79 
 80 - (void)windowDidLoad
 81 {
 82     [super windowDidLoad];
 83     
 84     NSButton *button = [[NSButton alloc] initWithFrame:NSZeroRect];
 85     [button setBordered:NO];
 86     [button setTitle:@"Button"];
 87     [button setAction:@selector(buttonAction:)];
 88     _inspectorBarFactory = [[InspectorBarFactory alloc] initWithTextView:_firstTextView];
 89     NSRect jFontSizeFrame = [_inspectorBarFactory.jFontSize frame];
 90     [button setFrame:jFontSizeFrame];
 91     [_inspectorBarFactory.inspectorBar addSubview:button];
 92     jFontSizeFrame.origin.x = 20;
 93     jFontSizeFrame.origin.y = 100;
 94     [_inspectorBarFactory.jFontSize setFrame:jFontSizeFrame];
 95     [_myBox addSubview:_inspectorBarFactory.jFontSize];
 96 }
 97 - (void)buttonAction:(id)sender{
 98     NSFont *font = [NSFont userFontOfSize:32];
 99     NSMutableParagraphStyle *paragraphStyle = [[[NSParagraphStyle alloc] init] mutableCopy];
100     [paragraphStyle setAlignment:NSCenterTextAlignment];
101     [self.firstTextView.textStorage setAttributedString:[[NSAttributedString alloc] initWithString:@"onecodego" attributes:@{NSFontAttributeName:font,NSParagraphStyleAttributeName:paragraphStyle}]];
102 }
103 @end

  点击button效果如下:

        

  经测试,移动位置之后的 Inspector Bar 的控件是可以正常使用的,添加到 Inspector Bar 上的控件也可以正常使用

posted on 2014-04-24 16:56  onecodego  阅读(551)  评论(0)    收藏  举报

导航