文字列を画面に出力

http://temping-amagramer.blogspot.com/2009/02/objective-ciphone-sdk.html

やっと、分かってきました。
UIView クラスを継承して、サブクラスを作ります。
drawRect メソッドを、オーバーライドして、その中に、描画処理を書きます。


applicationDidFinishLaunchingの中で、サブクラスを初期化して、実行します。


[window addSubview:output_text]; で、ウインドウに貼り付けます。

次は、文字列を格納、出力位置、文字の色を格納するインターフェースset_text.hを実装

#import <UIKit/UIKit.h>

//UIViewを継承する
@interface set_text : UIView {
 NSString *text;
 CGPoint location;
 UIFont *font;
 UIColor *color;
}

@property (readwrite, retain) NSString *text;
@property (readwrite) CGPoint location;
@property (readwrite, retain) UIFont *font;
@property (readwrite, retain) UIColor *color;

@end


このset_textは、UIViewクラスを継承して、
{}の中にある4つの変数を宣言しています。

と同時に、プロパティとして{}の後ろで宣言しています。
で、次に、肉となる「set_text.m」

#import "set_text.h"

 @implementation set_text
 @synthesize text;
 @synthesize location;
 @synthesize font;
 @synthesize color;

- (id)initWithFrame:(CGRect)frame {

 if (self = [super initWithFrame:frame]) {
  // Initialization code
 }
 return self;
}

 - (void)drawRect:(CGRect)rect {

 //フォントの色を設定
 [self.color set];
 
 //テキストを設定した書式、場所で表示
 [text drawAtPoint:location withFont:font];
}

 - (void)dealloc {
 [super dealloc];
}

@end

「set_text.h」で宣言したプロパティを使う場合、
「@synthesize」が必要なので、忘れずに追加。

initWithFrameメソッドは、UIViewクラスを初期化するのに必要なので、このままにしておく。
(というか、このメソッドは、ファイルを作った時に既にあるので、
その設定のままにしておいただけです。)

で、drawRectメソッドは、viewの所定の位置に、
設定されたフォントの色と大きさで書き込むメソッドです。

deallocはメモリを解放します。

iPhone sdkはJavaのようなガベージコレクションがないので、
手動で解放しなければなりません。

ガベージコレクションについては、schemeにもでてくるので、
後々考察したいポイントです。

次に、今回のプロジェクトの名前がoutput_textなので、
実行部である「output_textAppDelegate.h」を見てみます。

#import <UIKit/UIKit.h>

@interface output_textAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@end

ここもファイルを作った時から変更を加えていません。
で、最後に、「output_textAppDelegate.m」です。

#import "output_textAppDelegate.h"
#import "set_text.h"

@implementation output_textAppDelegate

@synthesize window;


- (void)applicationDidFinishLaunching:(UIApplication *)application {

 // Override point for customization after application launch
 //文字列を作成するオブジェクトをコール
 set_text *output_text = [[set_text alloc] initWithFrame:[window frame]];

 //文字列をセット
 output_text.text= @"ふぁいと〜一発、うふ";
 //文字列を出力するポイントを設定
 output_text.location = CGPointMake(10,200);
 
 //フォントの大きさを設定
 output_text.font = [UIFont systemFontOfSize:24];
 output_text.color = [UIColor whiteColor];
 
 //オブジェクト(window)をウィンドウに追加
 [window addSubview:output_text];

 //メモリの解放
 [output_text release];
 
 //ウィンドウの表示
 [window makeKeyAndVisible];
}

- (void)dealloc {

 [window release];
 [super dealloc];
}
@end

上のapplicationDidFinishLaunchingは、アプリケーションを稼働する
直前にコールされるメソッドで、初期設定を行うところです。

set_text型のオブジェクトを定義し、出力する文字列、位置、
フォント、色を設定します。

文字列は、output_text型のtextメソッドで設定。
(いわゆる、setterです。)

位置は、CGPointMake関数で設定、第一引数は、x軸、第二引数は、y座標。

フォントは、UIFont型のsystemFontOfSizeメソッド、いわゆるクラスメソッドですね。

色もUIColor型のwhiteColorメソッドで、こちらもクラスメソッド。

でも、これだけだと、色の設定が完了しきれていなくて、
最後に、setメソッドを呼ぶ必要があります。
(これは、set_text.mにあるself.color setで実現)

で、これでオールオッケーのように思いますが、実際これでビルドして実行しても何も表示されなくて、最後に、ウィンドウに貼付けるという
|<

*1258123148*CGContextDrawImage
>|
CGContextDrawImage
Draws an image into a graphics context.

void CGContextDrawImage (
   CGContextRef c,
   CGRect rect,
   CGImageRef image
);
Parameters
context
The graphics context in which to draw the image.

rect
The location and dimensions in user space of the bounding box in which to draw the image.

image
The image to draw.

Discussion
Quartz scales the image&#8212;disproportionately, if necessary&#8212;to fit the bounds specified by the rect parameter.

Availability
Available in Mac OS X version 10.0 and later.
Declared In
CGContext.h