1.取消点击时高亮状态
- (void)setHighlighted:(BOOL)highlighted {
// 自定义Button重写该方法可以取消高亮
}
2. button上文字 图片 位置
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame=CGRectMake(40,300,200,80);
//[btn setBackgroundImage:[UIImage imageNamed:@"background.jpg"] forState:UIControlStateNormal];
[btnsetBackgroundImage:[UIImage imageNamed:@"Button-Normal"] forState:UIControlStateNormal];
[btnsetImage:[UIImage imageNamed:@"home_on"] forState:UIControlStateNormal];
[btnsetTitle:@"Home" forState:UIControlStateNormal];
//按钮的内容在水平方向的对齐方式
btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
//按钮的内容在垂直方向的对齐方式
btn.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
CGFloatbtnWidth =CGRectGetWidth(btn.frame);
CGFloatimageWidth =CGRectGetWidth(btn.imageView.frame);
CGFloatlableWidth =CGRectGetWidth(btn.titleLabel.frame);
CGFloatbtnHeight =CGRectGetHeight(btn.frame);
CGFloatimageHeight =CGRectGetHeight(btn.imageView.frame);
CGFloatlabelHeight =CGRectGetHeight(btn.titleLabel.frame);
//如果按钮上既有图像又有文字,设置边距时,图像的上、下、左边距相对于按钮,右边距相对于文字;文字的上、下、右边距相对于按钮,左边距相对于图像
btn.titleLabel.backgroundColor = [UIColor yellowColor];
btn.imageView.backgroundColor = [UIColor redColor];
//设置图像的边距
btn.imageEdgeInsets=UIEdgeInsetsMake((btnHeight - imageHeight - labelHeight) /2, (btnWidth - imageWidth) /2,0,0);
NSLog(@"btn.imageEdgeInsets:%@", NSStringFromUIEdgeInsets(btn.imageEdgeInsets));
NSLog(@"btn.frame:%@", NSStringFromCGRect(btn.frame));
//设置文字的边距
btn.titleEdgeInsets=UIEdgeInsetsMake((btnHeight - imageHeight - labelHeight) /2+ imageHeight, (btnWidth - lableWidth) /2- imageWidth,0,0);
[self.viewaddSubview:btn];
网友评论