问题描述
我有一个NSTextField圆角通过子类化NSTextFieldCell和覆盖生成
I have a NSTextField with rounded corners generated by subclassing NSTextFieldCell and overriding
使用以下
- (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?
推荐答案
)
我相信你有两个选项:
- 您可以将
focusRingType
设置为NSFocusRingTypeNone
super drawInteriorWithFrame:] 你可以使用[NSBezierPath bezierPathWithRoundedRect:]
这样的东西绘制自己的对焦环。 - 您可以覆写一个名为(I think)的私人方法
_drawFocusRingWithFrame
- You can set
focusRingType
toNSFocusRingTypeNone
then after[super drawInteriorWithFrame:]
you can draw your own focus ring using something like[NSBezierPath bezierPathWithRoundedRect:]
. - 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对焦环的形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!