1.取消點(diǎn)擊時高亮狀態(tài)
- (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];
? ? //按鈕的內(nèi)容在水平方向的對齊方式
? ? btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
? ? //按鈕的內(nèi)容在垂直方向的對齊方式
? ? 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);
? ? //如果按鈕上既有圖像又有文字,設(shè)置邊距時,圖像的上、下、左邊距相對于按鈕,右邊距相對于文字;文字的上、下、右邊距相對于按鈕,左邊距相對于圖像
? ? btn.titleLabel.backgroundColor = [UIColor yellowColor];
? ? btn.imageView.backgroundColor = [UIColor redColor];
? ? //設(shè)置圖像的邊距
? ? btn.imageEdgeInsets=UIEdgeInsetsMake((btnHeight - imageHeight - labelHeight) /2, (btnWidth - imageWidth) /2,0,0);
? ? NSLog(@"btn.imageEdgeInsets:%@", NSStringFromUIEdgeInsets(btn.imageEdgeInsets));
? ? NSLog(@"btn.frame:%@", NSStringFromCGRect(btn.frame));
? ? //設(shè)置文字的邊距
? ? btn.titleEdgeInsets=UIEdgeInsetsMake((btnHeight - imageHeight - labelHeight) /2+ imageHeight, (btnWidth - lableWidth) /2- imageWidth,0,0);
? ? [self.viewaddSubview:btn];