iOS button学习

// 创建
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(x,y,w,h);
button.center = CGPointMake(x,y,w,h);
// 设置aligment 属性
button.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
//button.titleLabel.textAlignment = UITextAlignmentCenter;
//设置title自适应对齐
button.titleLabel.lineBreakMode = UILineBreakModeWordWrap;

// 按钮大小自适应
[button sizeToFit];

// 设置颜色和字体
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
button.titleLabel.font = [UIFont boldSystemFontOfSize:24.0f];
// 添加 action
[button addTarget:self action:@selector(toggleButton:) forControlEvents: UIControlEventTouchUpInside];
    //设置title
    [button setTitle:@"我是按钮" forState:UIControlStateNormal];
    [button setTitle:@"按钮亮啦" forState:UIControlStateHighlighted];
    //设置背景
    [button setBackgroundImage:baseGreen forState:UIControlStateNormal];
    [button setBackgroundImage:altGreen forState:UIControlStateHighlighted];





你可能感兴趣的:(ios,Objective-C,button)