MAC开发--NSTextField汇总

作者: 背靠背的微笑 | 来源:发表于2017-11-17 16:39 被阅读146次
图片来自网络

以下记录关于按钮NSTextField在项目中涉及到的需求:

1、取消焦点的高亮状态:

//点击的时候不显示蓝色外框
self.focusRingType = NSFocusRingTypeNone; 

2、文字垂直居中:

- (NSRect)adjustedFrameToVerticallyCenterText:(NSRect)frame {
// super would normally draw text at the top of the cell
CGFloat fontSize = self.font.boundingRectForFont.size.height;
NSInteger offset = floor((NSHeight(frame) - ceilf(fontSize))/2)-5;
NSRect centeredRect = NSInsetRect(frame, 0, offset);
return centeredRect;
}

 - (void)editWithFrame:(NSRect)aRect inView:(NSView *)controlView
editor:(NSText *)editor delegate:(id)delegate event:(NSEvent *)event {
[super editWithFrame:[self adjustedFrameToVerticallyCenterText:aRect]
              inView:controlView editor:editor delegate:delegate event:event];
}

- (void)selectWithFrame:(NSRect)aRect inView:(NSView *)controlView
editor:(NSText *)editor delegate:(id)delegate
 start:(NSInteger)start length:(NSInteger)length {

[super selectWithFrame:[self adjustedFrameToVerticallyCenterText:aRect]
                inView:controlView editor:editor delegate:delegate
                 start:start length:length];
}

 - (void)drawInteriorWithFrame:(NSRect)frame inView:(NSView *)view {
[super drawInteriorWithFrame:
 [self adjustedFrameToVerticallyCenterText:frame] inView:view];
}

3、文字内容富文本显示:

NSString *string = @"这是蓝色文字,这是红色文字。";
NSMutableAttributedString *colorTitle = [[NSMutableAttributedString alloc] initWithString: string];
[colorTitle addAttribute:NSForegroundColorAttributeName value:[NSColor blueColor] range:NSMakeRange(0, 7)];
[colorTitle addAttribute:NSForegroundColorAttributeName value:HEX_RGB_COLOR(0x38b162) range:NSMakeRange(7, 7)];
self.attributedStringValue = colorTitle;

4、改变边框颜色:

self.bordered = YES;
self.wantsLayer = YES;
self.layer.borderColor = [NSColor redColor].CGColor;
self.layer.borderWidth = 1.0f;

// 重要:一定要设置如下属性,否则无法显示效果
 [[self cell] setBezeled:NO];
 [[self cell] setBordered:NO];

5、多行文字换行:

如何换行

题外话:整个窗体失去焦点,不闪烁光标:[self.window makeFirstResponder:nil];

相关文章

  • MAC开发--NSTextField汇总

    以下记录关于按钮NSTextField在项目中涉及到的需求: 1、取消焦点的高亮状态: 2、文字垂直居中: 3、文...

  • Mac开发之NSTextField添加文字监控

    新做Mac开发的时候 发现 里面控件和iOS开发是有所区别的,NSTextField 相较于iOS UIText...

  • Mac开发基础_1-NSTextField

    搬运csdn上我曾经写的一些东西。由于网上关于Mac开发的资料很少,所以我的经验也都是摸索着,啃官方文档以及官方d...

  • macOS 开发-NSTextField

    NSTextField 是一个可以展示或编辑的文本框,是最常用的UI控件之一。需要注意它与iOS的UITextFe...

  • macOS开发-NSTextField

    NSTextField 简介 输入框,主要用于用户输入文本,同 iOS里的UITextField 在macOS开发...

  • Mac 开发 NSTextField背景色如何去掉

    如图中所示,当我们设置 的 的背景色的时候,其本身也会生成一个背景色。 我尝试过以下方法: 无效 正确的处理方式:...

  • Mac app 开发之-NSTextField 输入框

    在上一篇文章中讲述了如何使用 NSTextField展示文本,这一篇章就要讲如何NSTextField输入文字。 ...

  • MAC开发--NSButton汇总

    以下记录关于按钮NSButton在项目中涉及到的需求: 1、添加点击事件:有别于iOS的[self addTarg...

  • Flutter常见问题汇总

    Flutter常见问题汇总 目录 Mac os 10.15 运行‘idevice_id’无法验证开发者 json数...

  • macOS 开发-NSTextField 实践

    这节主要通过实践来学习NSTextField的使用,初步了解NSTextField的代理方法、常用属性、常用样式等...

网友评论

    本文标题:MAC开发--NSTextField汇总

    本文链接:https://www.haomeiwen.com/subject/rdwdmxtx.html