センタリングする方法

iphoneとかは、あんまりないけど...ipadならやらないと駄目かなぁと思ったので、
センタリングの方法を書こうと思います。
前に書いたUILabelの自動調整のソースをベースに書こうと思います。
ViewControllerのインターフェイス部分の定義です。

@interface ViewController : UIViewController
@property (weak, nonatomic) NSString *str;
@property (weak, nonatomic) IBOutlet UILabel *label;
@end

それで実装ですが、分割して書こうと思います。
まず初期表示部分ですが、viewDidLoadメソッド内に記述します。
センタリングするには、X軸とY軸と表示する文字の高さと幅を調整します。
文字の高さと幅は、CGSizeを使います。
X軸とY軸は、UIScreenで取ってきます。

str = @"こんにちは";
// 最大サイズ
CGSize maxSize = CGSizeMake(100, 500);
// 改行モード
UILineBreakMode breakMode = UILineBreakModeWordWrap;
// フォント
UIFont *font = [UIFont fontWithName:@"Helvetica-Bold" size:14];
// 文字列のサイズ
CGSize size = [str sizeWithFont:font constrainedToSize:maxSize lineBreakMode:breakMode];
// 画面の高さ
int width = [[UIScreen mainScreen]applicationFrame].size.width;
// 画面の幅
int height = [[UIScreen mainScreen]applicationFrame].size.height;
// センタリング
label.frame = CGRectMake((width/2)-(size.width/2), height/2, size.width, size.height);
// 文字列設定
label.text = str;
// カラー
label.textColor = [UIColor purpleColor];
// バックグラウンドカラー
label.backgroundColor = [UIColor whiteColor];

次に、回転させた時の処理を書きます。
回転を検知すると呼び出されるメソッドのdidRotateFromInterfaceOrientationに実装します。

// 最大サイズ
CGSize maxSize = CGSizeMake(100, 500);
// 改行モード
UILineBreakMode breakMode = UILineBreakModeWordWrap;
// フォント
UIFont *font = [UIFont fontWithName:@"Helvetica-Bold" size:14];
// 文字列のサイズ
CGSize size = [str sizeWithFont:font constrainedToSize:maxSize lineBreakMode:breakMode];
// 画面の高さ
int width = [[UIScreen mainScreen]applicationFrame].size.width;
// 画面の幅
int height = [[UIScreen mainScreen]applicationFrame].size.height;
// 向き
UIDevice *device = [UIDevice currentDevice];
UIDeviceOrientation orientation = [device orientation];
switch (orientation) {
   // 横方向の回転
   case UIDeviceOrientationLandscapeLeft:
   case UIDeviceOrientationLandscapeRight:
           label.frame = 
              CGRectMake((height/2)-(size.width/2), width/2, size.width, size.height);
   // 縦方向の回転
   case UIDeviceOrientationPortrait:
   case UIDeviceOrientationPortraitUpsideDown:
           label.frame = 
              CGRectMake((width/2)-(size.width/2), height/2, size.width, size.height);
   // 画面が上の場合と画面が下の場合
   case UIDeviceOrientationFaceUp:
   case UIDeviceOrientationFaceDown:
   default:
             label.frame = 
                CGRectMake((width/2)-(size.width/2), height/2, size.width, size.height);
    }

ちなみに回転の検知方法は、別のやり方もあります...今度書きます。