簡體   English   中英

NSTextField自定義聚焦環

[英]NSTextField Custom focus ring

有沒有可能在可編輯的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];
    }
}

以上答案很好! 以下是我轉換的Swift 3.0版本

class MyTextField: NSTextField {
    override func awakeFromNib() {
        self.focusRingType = NSFocusRingType.none
    }

    override func draw(_ dirtyRect: NSRect) {
        super.draw(dirtyRect)

        var focus = false

        if let firstResponder = self.window?.firstResponder {
            if firstResponder.isKind(of: NSTextView.self) {
                let textView = firstResponder as! NSTextView
                if let clipView = textView.superview {
                    if let textField = clipView.superview {
                        if textField == self {
                            focus = true
                        }
                    }
                }
            }
        }

        if focus {
            let bounds = self.bounds
            let outerRect = NSMakeRect(bounds.origin.x - 2, bounds.origin.y - 2, bounds.size.width + 4, bounds.size.height + 4)
            let innerRect = NSInsetRect(outerRect, 1, 1)
            let clipPath = NSBezierPath(rect: outerRect)
            clipPath.append(NSBezierPath(rect: innerRect))
            clipPath.windingRule = NSWindingRule.evenOddWindingRule
            clipPath.setClip()

            NSColor(calibratedWhite: 0.6, alpha: 1.0).setFill()
            NSBezierPath(rect: outerRect).fill()
            self.backgroundColor = NSColor(cgColor: CGColor(red: 1, green: 0.4, blue: 0.5, alpha: 0.4))
        }

    }
}

和如何使用如下

textView.select(withFrame: textView.frame, editor: NSText(), delegate: self, start, 0, length: 3)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM