Geometry 関数
NSBundle
NSDictionary
NSFileManager
NSMutableAttributedString
-文字に属性を設定する
NSNumber
NSScanner
NSString
NSTimer
Graphic 関数
NSApplication
NSBezierPath
NSCell
NSColor
NSCursor
NSCustomImageRep
NSDocument
NSDocumentController
NSDragging
NSEvent
NSFontManager
NSGraphicsContext
NSImage
NSMenu
NSOutlineView
NSPanel
NSPopUpButton
NSResponder
NSScrollView
NSString 追加
NSTableColumn
NSTableView
NSTextStorage
NSTextView
NSToolbar
NSView
NSWindow
NSWorkspace
その他
.nib ウィンドウ
Views パレット
クラス
インスタンス変数
メソッド
CFXML
Carbon Event
Carbon Graphics
Cocoa で日本語
メソッド
その他

- Foundation-
NSMutableAttributedString

Foundation - NSMutableAttributedString
文字に属性を設定する
Keywords: addAtribute

NSMutableAttributedString を使うと、文字に属性を設定できるんだ。こいつの addAtribute:value:range ってメソッドを使う。

Foundation/NSMutableAttributedString.h
- (void)addAttribute:(NSString *)name 
        value:(id)value 
        range:(NSRange)aRange;

ここで設定する属性は、Application Kit の方の NSAttributedString Class ClusterAdditions ってドキュメントに記載がある。こんな感じの属性があるんだ。

NSFontAttributeName
フォント属性
NSForegroundColorAttributeName
文字の色
NSBackgroundColorAttributeName
文字の背景色
NSUnderlineStyleAttributeName
下線
NSSuperscriptAttributeName
上付き
NSBaselineOffsetAttributeName
ベースラインからのオフセット(正負 OK)
NSKernAttributeName
カーニング(正負 OK)
NSLigatureAttributeName
リガチャをするか否か
NSParagraphStyleAttributeName
パラグラフスタイル
NSAttachmentAttributeName
テキストアタッチメント(よくわかんない)

じゃ、使ってみよう。一気にいろいろ使ってみるよ。

AttributedStringDrawingView.m
- (void)drawRect:(NSRect)frameRect
{
    NSMutableAttributedString*	str = 
        [[NSMutableAttributedString alloc]
            initWithString:@"How's about your Mac? It's fine!"];
    NSPoint		point = { 10, 10 };
    
    // Set font attribute
    
    [str addAttribute:NSFontAttributeName
        value:[NSFont fontWithName:@"Chicago" size:36.0] 
        range:NSMakeRange(0,5)];
    [str addAttribute:NSFontAttributeName 
        value:[NSFont fontWithName:@"Helvetica" size:24.0] 
        range:NSMakeRange(6,5)];
    [str addAttribute:NSFontAttributeName 
        value:[NSFont fontWithName:@"Sand" size:12.0] 
        range:NSMakeRange(12,4)];
    [str addAttribute:NSFontAttributeName 
        value:[NSFont fontWithName:@"Monaco" size:48.0] 
        range:NSMakeRange(17,4)];
    [str addAttribute:NSFontAttributeName 
        value:[NSFont fontWithName:@"Apple Chancery" size:36.0] 
        range:NSMakeRange(22,4)];
    [str addAttribute:NSFontAttributeName 
        value:[NSFont fontWithName:@"Times" size:48.0] 
        range:NSMakeRange(27,4)];
    
    // Set underline style attribute
    
    [str addAttribute:NSUnderlineStyleAttributeName 
        value:[NSNumber numberWithFloat:1.0] 
        range:NSMakeRange(0,2)];
    [str addAttribute:NSUnderlineStyleAttributeName 
        value:[NSNumber numberWithFloat:2.0] 
        range:NSMakeRange(2,3)];
    
    // Set foreground color attribute
    
    [str addAttribute:NSForegroundColorAttributeName 
        value:[NSColor redColor] 
        range:NSMakeRange(6,5)];
    [str addAttribute:NSForegroundColorAttributeName 
        value:[NSColor colorWithCalibratedWhite:0.0 alpha:0.3] 
        range:NSMakeRange(17,4)];
    
    // Set background color attribute
    
    [str addAttribute:NSBackgroundColorAttributeName 
        value:[NSColor blueColor] 
        range:NSMakeRange(22,4)];
    
    // Set ligature attribute
    
    [str addAttribute:NSLigatureAttributeName 
        value:[NSNumber numberWithInt:1] 
        range:NSMakeRange(27,4)];
    
    // Set kern attribute
    
    [str addAttribute:NSKernAttributeName 
        value:[NSNumber numberWithFloat:10.0] 
        range:NSMakeRange(22,4)];
    
    // Set superscript attribute
    
    [str addAttribute:NSSuperscriptAttributeName 
        value:[NSNumber numberWithInt:1] 
        range:NSMakeRange(17,4)];
    
    // Set baseline attribute
    
    [str addAttribute:NSBaselineOffsetAttributeName 
        value:[NSNumber numberWithFloat:-10.0] 
        range:NSMakeRange(12,4)];
    
    [str drawAtPoint:point];
}

こうやっていろんな属性を設定できるわけだ。最後の drawAtPoint: で画面に字を描いてる。



[Home] [Download] [Archives] [BBS] [Cocoa Programming Tips 1001] [Core Foundation の秘密] [Safari Developer Center] [はじめてのブラウザのつくり方] [Sketch BP] [スクリーンセイバーを作ろう] [Objective-C 最適化] [Authorization API 完全理解] [Mac OS X Programming Books Review] [オブジェクト指向の言語比較論] [panther-dev]

mailto: mkino@xd5.so-net.ne.jp