2009-07-27 6 views

答えて

7

drawingRectForBounds:をオーバーライドするカスタムNSTextFieldCellを使用します。あなたが好きなだけ矩形を挿入し、新しい矩形よりも[super drawingRectForBounds:]に渡して通常のパディングを取得し、その呼び出しの結果を返します。

+0

私は、どのようにインセットされたいのかを定義するためにメソッドにどのようなものを入れるのでしょうか?これのサンプルコードがありますか? – Joshua

+0

NSRectに精通していない場合はhttp://www.cocoadev.com/index.pl?NSRectを試してください。 – smorgan

+0

このリンクをありがとう "NSRect NSMakeRect(浮動小数点数x、浮動小数点数、浮動小数点w、浮動小数点数h)"メソッド内でこの関数を使用して、x、y、w、hをインセットするように変更しますか? – Joshua

9

スウェーデンの答えは正しい方向に私たちを指していますが、カスタマイズされたテキストフィールドの背景色を表示する方法を理解するのにかなり時間がかかりました。カスタムセルでsetBorder:YESを呼び出す必要があります。

これはジョシュアを助けるには遅すぎですが、ここだ、あなたがカスタマイズしたセル実装する方法:

#import <Foundation/Foundation.h> 
// subclass NSTextFieldCell 
@interface InstructionsTextFieldCell : NSTextFieldCell {   
} 
@end 

#import "InstructionsTextFieldCell.h" 

@implementation InstructionsTextFieldCell 

- (id)init 
{ 
    self = [super init]; 
    if (self) { 
     // Initialization code here. (None needed.) 
    } 
    return self; 
} 

- (void)dealloc 
{ 
    [super dealloc]; 
} 

- (NSRect)drawingRectForBounds:(NSRect)rect { 

    // This gives pretty generous margins, suitable for a large font size. 
    // If you're using the default font size, it would probably be better to cut the inset values in half. 
    // You could also propertize a CGFloat from which to derive the inset values, and set it per the font size used at any given time. 
    NSRect rectInset = NSMakeRect(rect.origin.x + 10.0f, rect.origin.y + 10.0f, rect.size.width - 20.0f, rect.size.height - 20.0f); 
    return [super drawingRectForBounds:rectInset]; 

} 

// Required methods 
- (id)initWithCoder:(NSCoder *)decoder { 
    return [super initWithCoder:decoder]; 
} 
- (id)initImageCell:(NSImage *)image { 
    return [super initImageCell:image]; 
} 
- (id)initTextCell:(NSString *)string { 
    return [super initTextCell:string]; 
} 
@end 

を(ヨシュアのように、あなたが唯一の左のインセットをしたい、場合は、origin.yを残し、高さをそのまま使用し、原点に行うのと同じように幅に同じ量を追加します。

ウィンドウ/ビューコントローラのawakeFromNibメソッドで、このようにカスタマイズしたセルを割り当てます。テキストフィールドを所有しています:

// Assign the textfield a customized cell, inset so that text doesn't run all the way to the edge. 
InstructionsTextFieldCell *newCell = [[InstructionsTextFieldCell alloc] init]; 
[newCell setBordered:YES]; // so background color shows up 
[newCell setBezeled:YES]; 
[self.tfSyncInstructions setCell:newCell]; 
[newCell release]; 
+0

これは、ボタンを使って自分のNSTokenFieldに最適です! setBorder:YESとフィールド自体に設定する他のプロパティを覚えておいてください。 – rocky

+0

これは役に立ちます。ありがとう – adev

関連する問題