【问题标题】:Change border color of NSTextField更改 NSTextField 的边框颜色
【发布时间】:2013-09-05 06:11:23
【问题描述】:

我想改变NSTextField对象的边框颜色,但是无法实现。

我已经尝试了很多解决方案 EX:成为子类,绘制背景......

有没有人可以解决这个问题或分享任何想法?

请告诉我。非常感谢。

【问题讨论】:

标签: objective-c cocoa nstextfield border-color


【解决方案1】:

使用NSBezierPath

- (void)drawRect:(NSRect)dirtyRect
{
    NSPoint origin = { 0.0,0.0 };
    NSRect rect;
    rect.origin = origin;
    rect.size.width  = [self bounds].size.width;
    rect.size.height = [self bounds].size.height;

    NSBezierPath * path;
    path = [NSBezierPath bezierPathWithRect:rect];
    [path setLineWidth:2];
    [[NSColor colorWithCalibratedWhite:1.0 alpha:0.394] set];
    [path fill];
    [[NSColor redColor] set]; 
    [path stroke];

    if (([[self window] firstResponder] == [self currentEditor]) && [NSApp isActive])
    {   
        [NSGraphicsContext saveGraphicsState];
        NSSetFocusRingStyle(NSFocusRingOnly);
        [path fill]; 
        [NSGraphicsContext restoreGraphicsState];
    }
    else
    {
        [[self attributedStringValue] drawInRect:rect];
    }
}

输出:

【讨论】:

  • @AviramNetanel 边框和背景不一样。
【解决方案2】:

对我来说,帕拉格的回答导致了一些奇怪的文本字段绘图,所以我最终得到了这个简单的代码(基于他的回答):

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

    if (!self.borderColor) {
        return;
    }

    NSPoint origin = { 0.0,0.0 };
    NSRect rect;
    rect.origin = origin;
    rect.size.width  = [self bounds].size.width;
    rect.size.height = [self bounds].size.height;

    NSBezierPath * path;
    path = [NSBezierPath bezierPathWithRect:rect];
    [path setLineWidth:2];
    [self.borderColor set];
    [path stroke];
}

【讨论】:

    【解决方案3】:

    您可以尝试 CALayer 而无需子类化

    self.wantsLayer = true
    self.layer?.borderColor = NSColor.red.cgColor
    self.layer?.borderWidth = 1
    

    【讨论】:

      【解决方案4】:

      SWift 5 版本的Parag 的答案,但增加了用户定义标题和边框颜色的能力。

      fileprivate class URLField: NSTextField {
          var title : String?
          var borderColor: NSColor?
      
          override func mouseDown(with event: NSEvent) {
              super.mouseDown(with: event)
              if let textEditor = currentEditor() {
                  textEditor.selectAll(self)
              }
          }
      
          convenience init(withValue: String?, modalTitle: String?) {
              self.init()
      
              if let string = withValue {
                  self.stringValue = string
              }
              if let title = modalTitle {
                  self.title = title
              }
              self.cell?.controlView?.wantsLayer = true
              self.cell?.controlView?.layer?.borderWidth = 1
              self.lineBreakMode = .byTruncatingHead
              self.usesSingleLineMode = true
          }
      
          override func draw(_ dirtyRect: NSRect) {
              super.draw(dirtyRect)
      
              if let color = borderColor {
                  ///self.layer?.borderColor = color.cgColor
                  let path = NSBezierPath.init(rect: frame)
                  path.lineWidth = 1
                  color.setStroke()
                  path.stroke()
      
                  if self.window?.firstResponder == self.currentEditor() && NSApp.isActive {
                      NSGraphicsContext.saveGraphicsState()
                      NSFocusRingPlacement.only.set()
                      NSGraphicsContext.restoreGraphicsState()
                  }
              }
          }
      
          override func viewDidMoveToWindow() {
              super.viewDidMoveToWindow()
      
              if let title = self.title {
                  self.window?.title = title
              }
      
              // MARK: this gets us focus even when modal
              self.becomeFirstResponder()
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-03-05
        • 1970-01-01
        • 1970-01-01
        • 2017-05-24
        • 1970-01-01
        相关资源
        最近更新 更多