gpt4 book ai didi

objective-c - NSTextField 自定义对焦环

转载 作者:行者123 更新时间:2023-12-03 16:25:48 25 4
gpt4 key购买 nike

是否可以在可编辑的 NSTextField 中绘制自定义对焦环?我搜索了整个网络,但找不到有效的解决方案。我对 NSTextField 进行了子类化并覆盖了“drawFocusRingMask”,但没有任何结果。

我的目标是实现一个像 Mac OS Adressbook 中的聚焦环(在编辑人物时)

最佳答案

NSTextField 子类中的这段代码有效:

- (void)awakeFromNib {
self.focusRingType = NSFocusRingTypeNone;
}

- (void)drawRect:(NSRect)dirtyRect {
[super drawRect:dirtyRect];

BOOL focus = NO;

//check, if the NSTextField is focused
id firstResponder = self.window.firstResponder;
if ([firstResponder isKindOfClass:[NSTextView class]]) {
NSTextView *textView = (NSTextView*)firstResponder;
NSClipView *clipView = (NSClipView*)textView.superview;
NSTextField *textField = (NSTextField*)clipView.superview;
if (textField == self)
focus = YES;
}

if (focus) {
NSRect bounds = self.bounds;
NSRect outerRect = NSMakeRect(bounds.origin.x - 2,
bounds.origin.y - 2,
bounds.size.width + 4,
bounds.size.height + 4);

NSRect innerRect = NSInsetRect(outerRect, 1, 1);

NSBezierPath *clipPath = [NSBezierPath bezierPathWithRect:outerRect];
[clipPath appendBezierPath:[NSBezierPath bezierPathWithRect:innerRect]];

[clipPath setWindingRule:NSEvenOddWindingRule];
[clipPath setClip];

[[NSColor colorWithCalibratedWhite:0.6 alpha:1.0] setFill];
[[NSBezierPath bezierPathWithRect:outerRect] fill];
}
}

关于objective-c - NSTextField 自定义对焦环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22510997/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com