设为首页 收藏本站
查看: 968|回复: 0

[经验分享] Mac OS 获取和修改InspectorBar

[复制链接]

尚未签到

发表于 2015-12-30 11:29:11 | 显示全部楼层 |阅读模式
  NSTextView 自带的 Inspector Bar 集成了样式(Style)、字体(FontFamily)、字体样式(FontStyle)、字体大小(FontSize)、字体颜色(TextColor)、字体背景颜色(TextBackgroudColor)、粗体-斜体-下划线(FontTrait)、文本对齐方式(TextAlignment)、行间距(LineHeight)和列表符号(TextList)。
  Step1: 让 NSTextView 使用 Inspector Bar。可以在 xib 文件中选中 NSTextView,勾选Inspector Bar:
DSC0000.png
  也可以在代码中实现:



[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效果如下:
DSC0001.png
  经测试,移动位置之后的 Inspector Bar 的控件是可以正常使用的,添加到 Inspector Bar 上的控件也可以正常使用

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-158281-1-1.html 上篇帖子: WikidPad 下篇帖子: 解决Mac OS 山猫10.8下Xcode无法更新以及速度慢的问题
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表