NSTextField自定义聚焦环 [英] NSTextField Custom focus ring

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

问题描述

有可能在可编辑的NSTextField中绘制自定义聚焦环吗?我搜索整个网络,但找不到一个工作的解决方案。我的子类化NSTextField和覆盖drawFocusRingMask,但没有任何结果。

is there a possibility to draw a custom focus ring in an editable NSTextField? I searched the whole net, but couldn't find a working solution. I subclassed the NSTextField and overrided "drawFocusRingMask", but without any result.

我的目标是实现一个聚焦环像Mac OS Adressbook人)

My target is to implement a focus ring like the one in the Mac OS Adressbook (while editing a person)

推荐答案

NSTextField子类中的代码工作原理:

This code in the subclass of NSTextField works:

- (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];
    }
}

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

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