正确的重绘NSTextView的方法

时间:2016-08-13 18:36:11

标签: swift cocoa nstextfield nstextview nstextfieldcell

尝试进行URL验证NSTextField,我会在错误的URL中绘制警告图像。一切正常,但我不确定我是否在与API作斗争,还有其他方法。我调用noteFocusRingMaskChanged重绘键盘焦点环,abortEditing + selectWithFrame重绘textView。

中间图像显示我无法重绘NSTextView Link to image

import Foundation

class URLValidationTextFieldCell: NSTextFieldCell {

    let spaceForImage : CGFloat = 22

    var isValid = false {
      didSet {
          if let textField = controlView as? NSTextField {
              if (textField.isFirstResponder()) {
                  let string = textField.stringValue
                  textField.noteFocusRingMaskChanged()
                  textField.abortEditing()
                  if let textView = (controlView?.window?.fieldEditor(false, forObject: nil) as? NSTextView) {
                      let range = textView.selectedRange()
                      textView.insertText(string)
                      textField.selectWithFrame(frameRectForBounds(), editor: textView, delegate: textField, start: range.location, length: range.length)
                  }
              } else {
                  textField.needsDisplay = true
              }
          }
      }
  }

    func frameRectForBounds() -> NSRect {
        return isValid ? (controlView?.bounds)! : NSIntegralRect(NSOffsetRect(NSInsetRect((controlView?.bounds)!, spaceForImage / 2, 0), -spaceForImage / 2 , 0))
    }


    func rectForImage(cellFrame: NSRect) -> NSRect {
        if (isValid) {
            return NSZeroRect
        } else {

            var rect = frameRectForBounds()
            rect.origin.x = rect.size.width + 4
            rect.size.width = spaceForImage - 4
            return rect
        }
    }

    override func drawInteriorWithFrame(cellFrame: NSRect, inView controlView: NSView) {
        super.drawInteriorWithFrame(frameRectForBounds(), inView: controlView)
    }

    override func editWithFrame(aRect: NSRect, inView controlView: NSView, editor textObj: NSText, delegate anObject: AnyObject?, event theEvent: NSEvent) {
        super.editWithFrame(frameRectForBounds(), inView: controlView, editor: textObj, delegate: anObject, event: theEvent)
    }

    override func selectWithFrame(aRect: NSRect, inView controlView: NSView, editor textObj: NSText, delegate anObject: AnyObject?, start selStart: Int, length selLength: Int) {
        super.selectWithFrame(frameRectForBounds(), inView: controlView, editor: textObj, delegate: anObject, start: selStart, length: selLength)
    }

    override func drawWithFrame(cellFrame: NSRect, inView controlView: NSView) {
        drawImage(rectForImage(cellFrame))
        super.drawWithFrame(frameRectForBounds(), inView: controlView)
    }

    override func drawFocusRingMaskWithFrame(cellFrame: NSRect, inView controlView: NSView) {
        super.drawFocusRingMaskWithFrame(frameRectForBounds(), inView: controlView)
    }

    func drawImage(aRect: NSRect) {
        let image = NSImage(named: NSImageNameCaution)!
        image.size = NSSize(width: 16, height: 16)
        NSGraphicsContext.saveGraphicsState()
        NSGraphicsContext.currentContext()?.imageInterpolation = NSImageInterpolation.High
        image.drawInRect(aRect, fromRect: NSZeroRect, operation:NSCompositingOperation.CompositeSourceOver, fraction: 1.0, respectFlipped: true, hints: [:])
        NSGraphicsContext.restoreGraphicsState()

    }

}


extension NSTextField {

    func isFirstResponder() -> Bool {
        if let _ = window?.fieldEditor(false, forObject: nil)  {
            if let textView = window?.firstResponder as? NSTextView {
                if (isEqualTo(textView.delegate)) {
                    return true
                }
            }
        }
        return false
    }
}

0 个答案:

没有答案