修改NSTextField对焦环的形状 [英] Modify shape of NSTextField focus ring

查看:356
本文介绍了修改NSTextField对焦环的形状的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个NSTextField圆角通过子类化NSTextFieldCell和覆盖生成

I have a NSTextField with rounded corners generated by subclassing NSTextFieldCell and overriding


(void)drawWithFrame:(NSRect)cellFrame inView :( NSView *)controlView

(void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView

使用以下

- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {

    controlView.layer.cornerRadius = cellFrame.size.height / 2;
    [super drawInteriorWithFrame:cellFrame inView:controlView];
} 

这似乎工作正常,但是当文本字段是第一响应者聚焦环仍然显示为矩形形状。是否可以修改聚焦环以符合NSTextField的形状?

This seems to work fine, however when the textfield is first responder the focus ring still appears as a rectangle shape. Is it possible modify the focus ring to correspond to the shape of the NSTextField?

推荐答案

我相信你有两个选项:


  1. 您可以将 focusRingType 设置为 NSFocusRingTypeNone super drawInteriorWithFrame:] 你可以使用 [NSBezierPath bezierPathWithRoundedRect:] 这样的东西绘制自己的对焦环。

  2. 您可以覆写一个名为(I think)的私人方法 _drawFocusRingWithFrame

  1. You can set focusRingType to NSFocusRingTypeNone then after [super drawInteriorWithFrame:] you can draw your own focus ring using something like [NSBezierPath bezierPathWithRoundedRect:].
  2. You can override a private method called (I think) _drawFocusRingWithFrame

方式你会自己画环,但那部分很容易。如果您希望系统对焦环颜色使用 [NSColor keyboardFocusIndicatorColor] 作为路径的笔触颜色。

either way you will be drawing the ring yourself, but that part is easy. If you desire the system focus ring color use [NSColor keyboardFocusIndicatorColor] for the stroke color of your path.

10.7或更高版本在 NSCell 上有一个名为 drawFocusRingMaskWithFrame 的方法。

On 10.7 or later there is a method called drawFocusRingMaskWithFrame on NSCell.

这篇关于修改NSTextField对焦环的形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆