何がしたいか
- UIButton をコードで作成し、UIViewController に addSubview で表示する
- 表示したボタン
- にイベントを付与する(addTarget で)
- の titleLabel を意図的に改行させる
前提
登場人物
- ButtonsViewController(.xib | .h | .m)
内部処理
xib
h
#import <UIKit/UIKit.h>
@interface ButtonsViewController : UIViewController
@end
m
#import "ButtonsViewController.h"
@interface ButtonsViewController ()
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@end
@implementation ButtonsViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self createButton];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)createButton
{
CGRect buttonRect = CGRectMake(self.titleLabel.frame.origin.x,
self.titleLabel.frame.origin.y + 30,
135.0f,
50.0f);
UIButton *button = [[UIButton alloc] initWithFrame:buttonRect];
button.backgroundColor = [UIColor whiteColor];
button.layer.borderColor = [[UIColor blackColor] CGColor];
button.layer.borderWidth = 0.5f;
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
button.titleLabel.font = [UIFont fontWithName:@"Helvetica" size:14.0f];
button.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
[button setTitle:@"plz output log\nthanks" forState:UIControlStateNormal];
[button addTarget:self action:@selector(touchCustomButton) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:button];
}
-(void)touchCustomButton
{
NSLog(@"all right. now logged.");
}
@end
「plz output log thanks」Button タップ時のログ
参考
stackoverflow.com