ある文字列を画面に描くにはどうしたらいいのか?NSString Addisions で定義されている、drawAtPoint:withAttributes: と drawInRect:withAttributes: で書くことができるんだ:
Application Kit/NSStringDrawing.h
- (void)drawAtPoint:(NSPoint)point
withAttributes:(NSDictionary *)attrs;
- (void)drawInRect:(NSRect)rect
withAttributes:(NSDictionary *)attrs;
NSAttributedString を書くときは、withAttributes がなくなるよ。
Application Kit/NSStringDrawing.h
- (void)drawAtPoint:(NSPoint)point;
- (void)drawInRect:(NSRect)rect;
じゃあ、サンプルだ。
StringDrawingView.m (sample)
- (void)drawRect:(NSRect)rect
{
NSMutableAttributedString* str;
str = [[NSMutableAttributedString alloc]
initWithString:@"Hello, NSString!"];
NSPoint point = { 0, 0 };
// 属性を設定する
...
[str drawAtPoint:point];
}
これらの、NSString を描くメソッドは、NSView にフォーカスが当たっている状態で使わなきゃいけない。上のサンプルでは、NSView の drawRect: メソッドで呼び出しているので大丈夫だ。好きな属性を設定して、drawAtPoint: で、描いてくれ。