テキストの表示にはUILabelやUITextView等がありますが、今回はUILabelを使いたいと思います。
まずはUILabelオブジェクトの生成です。
myLabel = [[UILabel alloc] init];
次にラベルの領域を設定します。
以下例は、座標(x, y) = (0, 0)にサイズ(width, height) = (100, 50)の領域を設定しています。
[myLabel setFrame: CGRectMake(0, 0, 100, 50)];
次に表示するテキストの指定。指定領域が分かるように背景をグレーにしています。
[myLabel setText: @"test"];
[myLabel setBackgroundColor: [UIColor grayColor]];
[myLabel setBackgroundColor: [UIColor grayColor]];
そして、生成したラベルをViewにアタッチします。
[myView addSubview:myLabel];
全体的には以下のコードとなります。これをdidFinishLaunchingWithOptionsメソッドに書きます。
//create rects.
CGRect rect = [[UIScreen mainScreen] bounds];
//create window.
_window = [[UIWindow alloc] initWithFrame:rect];
//create view.
myView = [[UIView alloc] initWithFrame:rect];
myView.backgroundColor = [UIColor whiteColor];
//text label
myLabel = [[UILabel alloc] init];
[myLabel setFrame: CGRectMake(0, 0, 100, 50)];
[myLabel setText: @"test"];
[myLabel setBackgroundColor: [UIColor grayColor]];
//add label to view.
[myView addSubview:myLabel];
//add view to window.
[_window addSubview:myView];
[self.window makeKeyAndVisible];
return YES;
実行するとこんな感じになります。
ラベルより大きなテキストを指定するとどうなるか?
テキストの指定部分を以下のように変更して、長いテキストを指定してみます。
[myLabel setText: @"test1234567890"];
以下、実行結果。
おぉ!勝手に「...」ってなってくれます。賢いっすね。
フォントを変えてみる
フォントをイタリックにしてみます。また、フォントサイズを12.0に変更しています。
//font
UIFont *font = [UIFont italicSystemFontOfSize:12.0];
[myLabel setFont:font];
UIFont *font = [UIFont italicSystemFontOfSize:12.0];
[myLabel setFont:font];
文字揃えしてみる
右寄せしてみる。
[myLabel setTextAlignment:UITextAlignmentRight];
右寄せ以外にも、左寄せ、センタリングがあります。




0 件のコメント :
コメントを投稿