2014-03-19 3 views
1

편집 가능한 NSTextField에 사용자 지정 포커스 링을 그릴 가능성이 있습니까? 전체 네트를 검색했지만 작동하는 솔루션을 찾을 수 없습니다. 나는 NSTextField를 서브 클래스 화하고 "drawFocusRingMask"를 오버라이드했지만 어떤 결과도 가져 오지 않았습니다.NSTextField 사용자 지정 포커스 링

내 목표는

+0

는 사용자 정의 필드 편집기를 사용하여 시도 해 봤나 다음과 같다 스위프트입니까? – Merlevede

답변

3

NSTextField있는의 서브 클래스에서이 코드는 작동 (사람을 편집하는 동안) 맥 OS Adressbook에있는 것과 같은 초점 링을 구현하는 것입니다 :

대답 위
- (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]; 
    } 
} 
1

일이다 잘! 다음은 내가 사용 방법

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)) 
    } 

} 

}

및 변환 3.0 버전은

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