
swift:
使用
//上文下圖
let btnTop = UIButton(frame: CGRect(x: 0, y: 100, width: 170, height: 70))
btnTop.titleLabel?.font = UIFont.systemFont(ofSize: 20)
btnTop.setTitleColor(UIColor.black, for: .normal)
btnTop.backgroundColor = UIColor.cyan
btnTop.set(image: UIImage(named: "btnAlphaControl"), title: "標(biāo)題在上方", titlePosition: .top, space: 10, state: .normal)
view.addSubview(btnTop)
// 上圖下文
btnBottom.lableToImage(titlePosition: .bottom, space: 10)
//左圖右文
btnBottom.lableToImage(titlePosition: .right, space: 10)
//左文右圖
btnBottom.lableToImage(titlePosition: .left, space: 10)
實(shí)現(xiàn):
//已有文字和圖片,直接調(diào)整位置
@objc func lableToImage(titlePosition: UIView.ContentMode, space: CGFloat){
let imageSize = self.imageRect(forContentRect: self.frame)
let titleFont = self.titleLabel?.font!
let titleSize = self.titleLabel?.text?.size(withAttributes: [kCTFontAttributeName as NSAttributedString.Key: titleFont!]) ?? CGSize.zero
var titleInsets: UIEdgeInsets
var imageInsets: UIEdgeInsets
switch titlePosition {
case .top:
//標(biāo)題在上
titleInsets = UIEdgeInsets(top: -(imageSize.height+titleSize.height+space)/2, left: -imageSize.width, bottom: 0, right: 0)
imageInsets = UIEdgeInsets(top: (imageSize.height + titleSize.height + space)/2, left: 0, bottom: 0, right: -titleSize.width)
case .left:
//標(biāo)題在左
titleInsets = UIEdgeInsets(top: 0, left: -(imageSize.width*2), bottom: 0, right: 0)
imageInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: -(titleSize.width*2+space))
case .bottom:
//標(biāo)題在下
titleInsets = UIEdgeInsets(top: (imageSize.height+titleSize.height+space)/2, left: -imageSize.width, bottom: 0, right: 0)
imageInsets = UIEdgeInsets(top: -(imageSize.height + titleSize.height + space)/2, left: 0, bottom: 0, right: -titleSize.width)
case .right:
//標(biāo)題在右
titleInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: -space)
imageInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
default:
titleInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
imageInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
break
}
self.titleEdgeInsets = titleInsets
self.imageEdgeInsets = imageInsets
}
OC:
使用:
//上文下圖
UIButton *btnTop = [[UIButton alloc] initWithFrame:CGRectMake(50, 100, 70, 70)];
[btnTop setTitle:@"上邊" forState:UIControlStateNormal];
[btnTop setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btnTop setImage:[UIImage imageNamed:@"btnAlphaControl"] forState:UIControlStateNormal];
btnTop.titleLabel.font = [UIFont systemFontOfSize:20];
[btnTop titlePosition:UIViewContentModeTop space:10];
[btnTop setBackgroundColor:[UIColor cyanColor]];
[self.view addSubview:btnTop];
// 上圖下文
[btnTop titlePosition:UIViewContentModeBottom space:10];
//左圖右文
[btnTop titlePosition:UIViewContentModeRight space:10];
//左文右圖
[btnTop titlePosition:UIViewContentModeLeft space:10];
實(shí)現(xiàn):
- (void)titlePosition:(UIViewContentMode)position space:(CGFloat)space{
CGRect imageSize = [self imageRectForContentRect:self.frame];
CGSize labelSize = [self.titleLabel.text sizeWithAttributes:@{NSFontAttributeName:self.titleLabel.font}];
//圖片寬高
CGFloat imageWith =imageSize.size.width;
CGFloat imageHeight = imageSize.size.height;
//標(biāo)題寬高
CGFloat labelWidth = labelSize.width;
CGFloat labelHeight = labelSize.height;
//imageEdgeInsets和labelEdgeInsets
UIEdgeInsets imageEdgeInsets = UIEdgeInsetsZero;
UIEdgeInsets labelEdgeInsets = UIEdgeInsetsZero;
//方便理解的一句話:left以右為正方向,right以左為正方向,top向下為正,bottom向上為正
switch (position) {
case UIViewContentModeTop:{
//標(biāo)題在上
imageEdgeInsets = UIEdgeInsetsMake(0, 0, -(labelHeight+space), -labelWidth);
labelEdgeInsets = UIEdgeInsetsMake(-(imageHeight+space), -imageWith, 0, 0);
break;
}
case UIViewContentModeLeft:{
//標(biāo)題在左
imageEdgeInsets = UIEdgeInsetsMake(0, (labelWidth+space/2), 0, -(labelWidth+space/2));
labelEdgeInsets = UIEdgeInsetsMake(0, -(imageWith+space/2), 0, (imageWith+space/2));
break;
}
case UIViewContentModeBottom:{
//標(biāo)題在下
imageEdgeInsets = UIEdgeInsetsMake(-(labelHeight+space), 0, 0, -labelWidth);
labelEdgeInsets = UIEdgeInsetsMake(0, -imageWith, -(imageHeight+space), 0);
break;
}
case UIViewContentModeRight:{
//標(biāo)題在右
imageEdgeInsets = UIEdgeInsetsMake(0, -space/2, 0, space/2);
labelEdgeInsets = UIEdgeInsetsMake(0, space/2, 0, -space/2);
break;
}
default:
break;
}
self.titleEdgeInsets = labelEdgeInsets;
self.imageEdgeInsets = imageEdgeInsets;
}