UIButton *first = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, kHeight(80), kHeight(80))]; [first setImage:[UIImage imageNamed:@"icon_index_housekeeping_10"] forState:UIControlStateNormal]; [first setTitle:@"扫一扫" forState:UIControlStateNormal]; [first.titleLabel setFont:kFont(14)]; [first setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [first setTitleColor:kColor(0x444444ff) forState:UIControlStateHighlighted]; [first setBackgroundColor:[UIColor whiteColor]]; [first setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft]; [first setContentVerticalAlignment:UIControlContentVerticalAlignmentTop]; [first setTitleEdgeInsets:UIEdgeInsetsMake((kGetHeigh(first.bounds) - first.titleLabel.intrinsicContentSize.height ) /2 + first.imageView.intrinsicContentSize.height / 2 + (((kGetHeigh(first.bounds) - first.imageView.intrinsicContentSize.height - first.titleLabel.intrinsicContentSize.height)/2)/2), (kGetWidth(first.bounds) - first.titleLabel.intrinsicContentSize.width) / 2 - first.imageView.intrinsicContentSize.width, 0,0)]; [first setImageEdgeInsets:UIEdgeInsetsMake((kGetHeigh(first.bounds) - first.imageView.intrinsicContentSize.height ) /2 - (((kGetHeigh(first.bounds) - first.imageView.intrinsicContentSize.height - first.titleLabel.intrinsicContentSize.height)/2)/2), (kGetWidth(first.bounds) - first.imageView.intrinsicContentSize.width)/2, 0, 0)];
注释:
(((kGetHeigh(first.bounds) - first.imageView.intrinsicContentSize.height - first.titleLabel.intrinsicContentSize.height)/2)/2) 文字向下的偏移量 (总高度-图片高度-文字高度)/2)/2