2011-01-09 3 views
2

xやy、あるいはその両方を繰り返すことができるビュー内にパターン(グリッドなど)を表示することはできますか?のようなCSSの種類:Objective-c:背景パターンを表示

background-image:url(pattern.png); background-repeat:repeat; //またはrepeat-x、repeat-y、繰り返しなし;

+0

のNSViewまたはUIViewの? –

答えて

4

はい。あなたは色([NSColor withPatternImage:[NSImage imageNamed:@"pattern"]])などの画像パターンをロードしますが、通常の色と同じようにそれを描くことができます:

- (void)drawRect:(NSRect)dirtyRect { 
    ... 
    // Load the image through NSImage and set it as the current color. 
    [[NSColor colorWithPatternImage:[NSImage imageNamed:@"pattern"]] set]; 

    // Fill the entire view with the image. 
    [NSBezierPath fillRect:[self bounds]]; 
    ... 
} 

このコードは、ビュー全体にパターンを繰り返しますが、あなたは、単にそれを持つことができX-リピートまたはyリピートを少し変更してください。


の詳細を学ぶためにNSColor Class ReferenceNSView Class Referenceを見てみましょう。

+0

スニペットありがとう! – PruitIgoe

+0

問題ありません。それがうれしかった。 –

7

次のコード意志タイルの背景画像:

- (void) viewDidLoad { 
    [[self view] setBackgroundColor: [[UIColor alloc] initWithPatternImage: [UIImage imageNamed: @"background.png"]]]; 
    [super viewDidLoad]; 
} 
関連する問題