Welcomeボタンの描画
drawRectが呼び出されるまでに、描画環境が設定され、準備完了となる。ここで行うことは自作コンテンツを描画するための描画コマンドを指定することである。PlacardViewクラスではコンテンツはバックグラウンド画像と文字列から成り立っており、そのテキストは動的に変化する。この内容を描画するためにはクラスは下記のステップを踏む。
・バックグランド画像を原点に配置。
・welcom文字列の場所の計算をし、中央に置く。
・ドローカラーを黒にする。
・文字列を黒で書く。
・ドローカラーを白にする。
・白で指定位置に文字列を書く。
実際のコードは下記のようになる。
- (void)drawRect:(CGRect)rect { // Draw the placard at 0, 0 [placardImage drawAtPoint:(CGPointMake(0.0, 0.0))]; /* Draw the current display string. This could be done using a UILabel, but this serves to illustrate the UIKit extensions to NSString. The text is drawn center of the view twice - first slightly offset in black, then in white -- to give an embossed appearance. The size of the font and text are calculated in setupNextDisplayString. */ // Find point at which to draw the string so it will be in the center of the view CGFloat x = self.bounds.size.width/2 - textSize.width/2; CGFloat y = self.bounds.size.height/2 - textSize.height/2; CGPoint point; // Get the font of the appropriate size UIFont *font = [UIFont systemFontOfSize:fontSize]; [[UIColor blackColor] set]; point = CGPointMake(x, y + 0.5); [currentDisplayString drawAtPoint:point forWidth:(self.bounds.size.width-STRING_INDENT) withFont:font fontSize:fontSize lineBreakMode:UILineBreakModeMiddleTruncation baselineAdjustment:UIBaselineAdjustmentAlignBaselines]; [[UIColor whiteColor] set]; point = CGPointMake(x, y); [currentDisplayString drawAtPoint:point forWidth:(self.bounds.size.width-STRING_INDENT) withFont:font fontSize:fontSize lineBreakMode:UILineBreakModeMiddleTruncation baselineAdjustment:UIBaselineAdjustmentAlignBaselines]; }