2012-03-29 173 views
3

我試圖圍繞NSTextField繪製圓角。帶圓角的NSTextField?

我子類NSTextField,嘗試下面的代碼,但沒有任何結果......

任何想法?

- (void)drawRect:(NSRect)dirtyRect 
{ 

    // black outline 
    NSRect blackOutlineFrame = NSMakeRect(0.0, 0.0, [self bounds].size.width, [self bounds].size.height-1.0); 
    NSGradient *gradient = nil; 
    if ([NSApp isActive]) { 
     gradient = [[NSGradient alloc] initWithStartingColor:[NSColor colorWithCalibratedWhite:0.24 alpha:1.0] endingColor:[NSColor colorWithCalibratedWhite:0.374 alpha:1.0]]; 
    } 
    else { 
     gradient = [[NSGradient alloc] initWithStartingColor:[NSColor colorWithCalibratedWhite:0.55 alpha:1.0] endingColor:[NSColor colorWithCalibratedWhite:0.558 alpha:1.0]]; 
    } 
    [gradient drawInBezierPath:[NSBezierPath bezierPathWithRoundedRect:blackOutlineFrame xRadius:5 yRadius:5] angle:90]; 

} 

回答

4

你正在做的幾乎一切正確。你只需要改變textField的單元格和半徑匹配。看看這個:

-(void)awakeFromNib { 

    [[self cell] setBezelStyle: NSTextFieldRoundedBezel]; 
} 


- (void)drawRect:(NSRect)dirtyRect 
{ 

    NSRect blackOutlineFrame = NSMakeRect(0.0, 0.0, [self bounds].size.width, [self bounds].size.height-1.0); 
    NSGradient *gradient = nil; 
    if ([NSApp isActive]) { 
     gradient = [[NSGradient alloc] initWithStartingColor:[NSColor colorWithCalibratedWhite:0.24 alpha:1.0] endingColor:[NSColor colorWithCalibratedWhite:0.374 alpha:1.0]]; 
    } 
    else { 
     gradient = [[NSGradient alloc] initWithStartingColor:[NSColor colorWithCalibratedWhite:0.55 alpha:1.0] endingColor:[NSColor colorWithCalibratedWhite:0.558 alpha:1.0]]; 
    } 

    [gradient drawInBezierPath:[NSBezierPath bezierPathWithRoundedRect:blackOutlineFrame xRadius:10 yRadius:10] angle:90]; 

} 

這對我很好用。

+0

嗯...它不適合我... – 2012-03-29 21:22:24

+0

嗯,我剛剛發現有什麼問題......我在創建文本字段實例時設置了一個'setBackgroundColor:',然後是'setDrawsBackground:YES'。現在,當我刪除它,它的作品!但是:如果在'setBezelStyle:'之後添加背景,背景保持白色......任何建議? – 2012-03-29 21:27:31

+0

@ Dr.Kameleon當用戶點擊textField時,你想要改變背景顏色嗎?而firstResponder?如果不是那種顏色,不要顯示這種顏色? – 2012-03-29 22:27:15

1

在您的項目中包含QuartzCore框架。導入<QuartzCore/QuartzCore.h>和使用類似下面的代碼(只是從我的頭頂):

UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, 20.0f)]; 
textField.layer.cornerRadius = 5.0f; 
textField.layer.masksToBounds = YES; 

瞧,圓角:)

編輯文本字段:剛剛意識到這個問題是相關的到Mac OS X.請查看以下鏈接:http://cocoatricks.com/2010/06/a-better-looking-text-field/

編輯2:創建與圓角文本字段實例項目:http://dl.dropbox.com/u/6487838/RoundedTextField.zip

+0

不,它不工作... – 2012-03-29 17:34:09

+0

嗯..剛剛意識到它是關於Mac OS X的,上面的代碼應該適用於iOS應用程序,但不知道它是否適用於Mac OS X.更新了該帖子。 – 2012-03-29 17:36:52

+0

@WoldgandSchreurs嗯,我調整了你的Mac OS X的示例代碼,用'NSTextField'代替了'UITextField'和一些細微的修改(比如'[textField layer] setCornerRadius:5.0f]等等,但不幸的是我們沒有幸運......謝謝! :-) – 2012-03-29 17:39:43

6

這是更好地繼承NSTextFieldCell繪製圓角保持NSTextField功能,如:

- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView { 
    NSBezierPath *betterBounds = [NSBezierPath bezierPathWithRoundedRect:cellFrame xRadius:CORNER_RADIUS yRadius:CORNER_RADIUS]; 
    [betterBounds addClip]; 
    [super drawWithFrame:cellFrame inView:controlView]; 
    if (self.isBezeled) { 
     [betterBounds setLineWidth:2]; 
     [[NSColor colorWithCalibratedRed:0.510 green:0.643 blue:0.804 alpha:1] setStroke]; 
     [betterBounds stroke]; 
    } 
} 

息率完美的作品(一個漂亮的圓的文本字段,如果您設置它來繪製的矩形邊框首先,至少):

enter image description here

+0

這對我來說更好,因爲Juston Boo提出的解決方案使得佔位符文本在不編輯字段時不可見。 – IndrekV 2015-03-04 12:38:18

1

其中最簡單的是做界面生成器,除非你想通過一些單位四捨五入的角落:

只需選擇邊框,圓潤之一:

enter image description here

+0

這將改變TextField的白色背景 – Borzh 2015-07-08 21:28:35

3

斯威夫特3版@Verious代碼,在界面生成可編輯的屬性:

class RoundedTextFieldCell: NSTextFieldCell { 

    @IBInspectable var borderColor: NSColor = .clear 
    @IBInspectable var cornerRadius: CGFloat = 3 

    override func draw(withFrame cellFrame: NSRect, in controlView: NSView) { 
     let bounds = NSBezierPath(roundedRect: cellFrame, xRadius: cornerRadius, yRadius: cornerRadius) 
     bounds.addClip() 
     super.draw(withFrame: cellFrame, in: controlView) 
     if borderColor != .clear { 
      bounds.lineWidth = 2 
      borderColor.setStroke() 
      bounds.stroke() 
     } 
    } 
}