UIButton详解

时间:2023-11-22 14:31:44

// ----------------------------------UIButton------------------------------

// UIButtonTypeSystem 点击文字会有效果,变色  UIButtonTypeCustom 无点击效果

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

button.frame = CGRectMake(200, 100, 200, 80);

[button setTitle:@"click me" forState:UIControlStateNormal];

button.backgroundColor = [UIColor blueColor];

[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

[button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];

// 字体大小

button.titleLabel.font = [UIFont systemFontOfSize: 20.0];

//    button.font = [UIFont systemFontOfSize:20];  废弃

//边框

button.layer.borderWidth = 3;

button.layer.borderColor = [UIColor grayColor].CGColor;

// 设置圆角

[button.layer setMasksToBounds:YES];

[button.layer setCornerRadius:20.0];

// 按下会有发光的效果

button.showsTouchWhenHighlighted = YES;

// 文字居左

button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;

// 文字居顶

button.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;

// 文字内边距

button.contentEdgeInsets = UIEdgeInsetsMake(10, 10, 0, 0);

[button addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];