2017-08-12 8 views
1

NSSearchFieldCellサブクラスでカスタム図面を作成しています。ただし、その2つの描画メソッドのいずれかをオーバーライドすると、プレースホルダテキストが揃えられなくなります。NSSearchFieldCellをオーバーライドすると、プレースホルダが中央に配置されない

たとえば、このカスタムNSSearchFieldCellサブクラスを使用すると、NSCellの描画メソッドをオーバーライドすると、プレースホルダテキストが左揃えになります。

class CustomSearchFieldCell: NSSearchFieldCell { 
    override func draw(withFrame cellFrame: NSRect, in controlView: NSView) { 
     super.draw(withFrame: cellFrame, in: controlView) 
    } 

    override func drawInterior(withFrame cellFrame: NSRect, in controlView: NSView) { 
     super.drawInterior(withFrame: cellFrame, in: controlView) 
    } 
} 

これも、検索フィールドをtrueに検索フィールドのcentersPlaceholderプロパティを設定し、再レイアウトの両方の後であるか、検索フィールドのstringValueをリセットします。

(プレースホルダのテキストになるだろう、これらの2つのメソッドをコメントアウトして再度中心に虫眼鏡。

search field center aligned

しかし、一つだけオーバーライドを(それは何もしませんし、唯一のスーパークラスの実装)を呼び出していても検索フィールドのプレースホルダテキストと左寄せになっ拡大鏡はなるだろう。

search field left aligned

質問は01を取得する方法であり、が動作していて、まだカスタム図面がありますか?

セル内でカスタム描画とマウス操作を行う必要があるため、上書きが必要です。

これはmacOS 10.12.6で観察されました。

+0

私は自分のコードを調べたので、同じ要件がありましたが、私のアプリでこのことを実現するためのメソッドを使用していませんでした。私はdrawメソッドを使用しませんでした。 –

答えて

0
それが呼び出されるので、マウスがテキストフィールドを左にし、それに応じてフレームを更新しているときは、このメソッドになります

: - 垂直NSSearchFieldCellのテキストを中央に、私は要件を持っていた

- (void)stopTracking:(NSPoint)lastPoint at:(NSPoint)stopPoint inView:(NSView *)controlView mouseIsUp:(BOOL)flag; 

、以下のコードではなかったです

// 
// VerticallyCenteredTextFieldCell.m 
// 
// Created by Vikram on 01/03/17. 
// Copyright © 2017 Vikram. All rights reserved. 
// 

#import "VerticallyCenteredTextFieldCell.h" 

@implementation VerticallyCenteredTextFieldCell 

- (NSRect)adjustedFrameToVerticallyCenterText:(NSRect)frame 
{ 
    // super would normally draw text at the top of the cell 
    NSInteger offset = floor((NSHeight(frame) - 
           ([[self font] ascender] - [[self font] descender]))/2); 
    return NSInsetRect(frame, 0.0, offset-3); 
} 
- (void)editWithFrame:(NSRect)aRect inView:(NSView *)controlView 
       editor:(NSText *)editor delegate:(id)delegate event:(NSEvent *)event 
{ 
    [super editWithFrame:[self adjustedFrameToVerticallyCenterText:aRect] 
        inView:controlView editor:editor delegate:delegate event:event]; 
} 
- (void)selectWithFrame:(NSRect)aRect inView:(NSView *)controlView 
       editor:(NSText *)editor delegate:(id)delegate 
        start:(NSInteger)start length:(NSInteger)length 
{ 

    [super selectWithFrame:[self adjustedFrameToVerticallyCenterText:aRect] 
        inView:controlView editor:editor delegate:delegate 
        start:start length:length]; 
} 
- (void)drawInteriorWithFrame:(NSRect)frame inView:(NSView *)view 
{ 
    [super drawInteriorWithFrame: 
    [self adjustedFrameToVerticallyCenterText:frame] inView:view]; 
} 

@end 

私はObjective-Cのそれに応じそれを読んで、迅速で、それが有用なもので、それを作っています - あなたはそれをしたいコンポーネントを中央に試してみることができるようにトリック。

+0

これは[RSVerticallyCenteredTextFieldCell](https://red-sweater.com/blog/148/what-a-difference-a-cell-makes)のものですか? – adib

+0

いいえそれは私によってカスタマイズされました..なぜですか? –

関連する問題