2009-11-18 9 views
9

中立の垂直整列で表示したいNSTextFieldCellがあります。ここで古い質問とブログエントリのおかげで、私は2つの実用的な解決策を持っています。NSTextFieldCell垂直整列、ソリューションは、水平整列をスカッシュしているようです。

しかし、どちらの解決策でも、セルを右揃えに設定する能力が低下しているようです。誰も私がこれらのソリューションのいずれかを両方の形式のアライメントに対応させるのを助けることができますか?ここ

一の溶液のためのコードである:

@implementation MiddleAlignedTextFieldCell 

- (NSRect)titleRectForBounds:(NSRect)theRect { 
    NSRect titleFrame = [super titleRectForBounds:theRect]; 
    NSSize titleSize = [[self attributedStringValue] size]; 
    titleFrame.origin.y = theRect.origin.y - .5 + (theRect.size.height - titleSize.height)/2.0; 
    return titleFrame; 
} 

- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView { 
    NSRect titleRect = [self titleRectForBounds:cellFrame]; 
    [[self attributedStringValue] drawInRect:titleRect]; 
} 

@end 

代替ソリューションが(this blogから入手される):

@implementation RSVerticallyCenteredTextFieldCell 

- (NSRect)drawingRectForBounds:(NSRect)theRect 
{ 
    NSRect newRect = [super drawingRectForBounds:theRect]; 

    if (mIsEditingOrSelecting == NO) 
    { 
     // Get our ideal size for current text 
     NSSize textSize = [self cellSizeForBounds:theRect]; 

     // Center that in the proposed rect 
     float heightDelta = newRect.size.height - textSize.height; 
     if (heightDelta > 0) 
     { 
      newRect.size.height -= heightDelta; 
      newRect.origin.y += (heightDelta/2); 
     } 
    } 

    return newRect; 
} 

- (void)selectWithFrame:(NSRect)aRect inView:(NSView *)controlView editor:(NSText *)textObj delegate:(id)anObject start:(int)selStart length:(int)selLength 
{ 
    aRect = [self drawingRectForBounds:aRect]; 
    mIsEditingOrSelecting = YES;  
    [super selectWithFrame:aRect inView:controlView editor:textObj delegate:anObject start:selStart length:selLength]; 
    mIsEditingOrSelecting = NO; 
} 

- (void)editWithFrame:(NSRect)aRect inView:(NSView *)controlView editor:(NSText *)textObj delegate:(id)anObject event:(NSEvent *)theEvent 
{ 
    aRect = [self drawingRectForBounds:aRect]; 
    mIsEditingOrSelecting = YES; 
    [super editWithFrame:aRect inView:controlView editor:textObj delegate:anObject event:theEvent]; 
    mIsEditingOrSelecting = NO; 
} 

@end 

答えて

5

私はそれが機能しているので、この回答を投稿していますが、私はIBからのアライメント設定を確認する別の方法が見つからないということがわかりました。 _cFlagsにアクセスするのはちょっと汚いようですが、私はよりクリーンな方法を見つけることが大好きです。

this blog entryから前に投稿したコードに基づいています。

- (NSRect)drawingRectForBounds:(NSRect)theRect 
{ 
    // Get the parent's idea of where we should draw 
    NSRect newRect = [super drawingRectForBounds:theRect]; 

    if (mIsEditingOrSelecting == NO) 
    { 
     // Get our ideal size for current text 
     NSSize textSize = [self cellSizeForBounds:theRect]; 

     // Center that in the proposed rect 
     float heightDelta = newRect.size.height - textSize.height; 
     if (heightDelta > 0) 
     { 
      newRect.size.height -= heightDelta; 
      newRect.origin.y += (heightDelta/2); 
     } 

     // For some reason right aligned text doesn't work. This section makes it work if set in IB. 
     // HACK: using _cFlags isn't a great idea, but I couldn't find another way to find the alignment. 
     // TODO: replace _cFlags usage if a better solution is found. 
     float widthDelta = newRect.size.width - textSize.width; 
     if (_cFlags.alignment == NSRightTextAlignment && widthDelta > 0) { 
      newRect.size.width -= widthDelta; 
      newRect.origin.x += widthDelta; 
     } 

    } 

    return newRect; 
} 
3

あなたがアライメント(及びその他の属性を設定するNSParagraphStyle/NSMutableParagraphStyleを使用することができ)。適切に構成されたNSParagraphStyleオブジェクトを属性付き文字列の全範囲に追加します。

+0

私は、プレゼンテーションのその部分を自分のデータレイヤーと結合する必要があります。正に、私はそれが好きであるかどうかはわかりません。これは、IBに提示された選択肢を尊重すべきであるが、何らかの理由でそうではない(垂直方向の整列もまたIBの選択肢でなければならない)。 –

+0

NSTextFieldCellの文字列をモデルレイヤーに設定する以外に、どのように混合するのかよく分かりません。:-)整列(およびフォント、色、サイズなど)は、このすべての一部です。しかし、私は、垂直方向の調整がオプションであるべきだということに同意する - 拡張レポートを提出する。それが利用できないので、あなたはそれを描画コードで考慮する必要があります。 –

+0

Er、それは「強化要求」です。リンク:http://bugreporter.apple.com –

2

私はしばらく前に尋ねたsimilar questionにいくつかの潜在的な解決策が掲載されています。すべての正直で

、私はまだ仕事を得るために文書化されていない_cFlags.vCenteredブール(TSKTSK、悪いプログラマを!)を使用します。それは簡単で、うまくいきます。私はしなければならないなら、私は後で車輪を再発明するでしょう。

更新:

OK、私はそれを考え出したと思います。両方のソリューションは、superの呼び出しを使用してデフォルトの矩形を取得し、origin.ysize.heightを変更して垂直センタリングを実行します。ただし、superの呼び出しは、テキストが水平方向に収まるように幅が調整されている矩形を返します。

溶液がメソッドに渡される境界RECTからorigin.xsize.widthを使用することである:溶液#1に

溶液#2で
- (NSRect)titleRectForBounds:(NSRect)theRect { 
    NSRect titleFrame = [super titleRectForBounds:theRect]; 
    NSSize titleSize = [[self attributedStringValue] size]; 

    // modified: 
    theRect.origin.y += (theRect.size.height - titleSize.height)/2.0 - 0.5; 
    return theRect; 
} 

- (NSRect)drawingRectForBounds:(NSRect)theRect 
{ 
    NSRect newRect = [super drawingRectForBounds:theRect]; 

    // modified: 
    newRect.origin.x = theRect.origin.x; 
    newRect.size.width = theRect.size.width; 

    if (mIsEditingOrSelecting == NO) 
    { 
     // Get our ideal size for current text 
     NSSize textSize = [self cellSizeForBounds:theRect]; 

     // Center that in the proposed rect 
     float heightDelta = newRect.size.height - textSize.height; 
     if (heightDelta > 0) 
     { 
      newRect.size.height -= heightDelta; 
      newRect.origin.y += (heightDelta/2); 
     } 
    } 

    return newRect; 
} 
+0

実際に私の解決策の1つを見つけたという質問に対する解決策でした。 –

+0

私は、この解決策との水平方向の整列を維持する方法を理解しようとしています。しかし、もし私ができなければ、私は降りて別のルートを試みなければならないかもしれません。 –

+0

私は、水平方向の配置がこれらのソリューションのいずれによっても影響を受けないことに驚いています。どのようにアライメントを設定していますか? –

関連する問題