2012-04-14 6 views
0

メッセージ/ iMessageのリッチテキストスタイル、または明るい灰色の背景のテキスト「ホワイトシャドウ」スタイルを複製したいと思います。アップルのMac用メッセージのように凹んだテキストスタイルを実現するには?

example

あなたが見ることができるように、テキストでもライトグレーの背景に「白い影」です。太字のテキストはサブピクセルレンダリングを持ち、グレーのテキストは(意図的に)?

私は-setBackgroundStyle:NSBackgroundStyleRaisedを試しました。しかし、それは背景より暗い影を生成していました。 -setBackgroundStyle:NSBackgroundStyleLoweredは、私のフォントの色の設定を覆すことさえも悪化しました。

だから、これを行う正しい方法は何ですか?任意のトリックまたはサブクラスNSTextFieldsする必要がありますか?

答えて

5

解決策1:

私が考えることができる最も簡単な解決策は、お互いに2つのテキストを書くことである(例えば、頂部及び底部における差分1ピクセルと白に灰色)。


解決方法2:

そしてもちろん、それはNSTextFieldCellをサブクラス化し、影を追加することによって行うことができます。ただ、このような

- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView 
{ 
    NSShadow *shadow = [[NSShadow alloc] init]; 
    [shadow setShadowOffset:NSMakeSize(0,-1)]; 
    [shadow setShadowColor:[NSColor whiteColor]]; 
    [shadow setShadowBlurRadius:0]; 

    NSMutableParagraphStyle *paragStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy]; 
    [paragStyle setAlignment:[self alignment]]; 

    NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys: 
         [self font],NSFontAttributeName, 
         shadow,NSShadowAttributeName, 
         [self textColor],NSForegroundColorAttributeName, 
         paragStyle,NSParagraphStyleAttributeName,nil]; 
    [shadow release]; 
    [paragStyle release]; 

    NSAttributedString *string = [[NSAttributedString alloc] initWithString:[self stringValue] attributes:attributes]; 
    [self setAttributedStringValue:string]; 
    [string release]; 
    [[self attributedStringValue] drawInRect:cellFrame]; 
} 

結果:

Text example

関連する問題