![](https://img.haomeiwen.com/i5720820/5714d9f30faa612f.png)
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: "标题在上方", 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)
实现:
//已有文字和图片,直接调整位置
@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:
//标题在上
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:
//标题在左
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:
//标题在下
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:
//标题在右
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];
实现:
- (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;
//标题宽高
CGFloat labelWidth = labelSize.width;
CGFloat labelHeight = labelSize.height;
//imageEdgeInsets和labelEdgeInsets
UIEdgeInsets imageEdgeInsets = UIEdgeInsetsZero;
UIEdgeInsets labelEdgeInsets = UIEdgeInsetsZero;
//方便理解的一句话:left以右为正方向,right以左为正方向,top向下为正,bottom向上为正
switch (position) {
case UIViewContentModeTop:{
//标题在上
imageEdgeInsets = UIEdgeInsetsMake(0, 0, -(labelHeight+space), -labelWidth);
labelEdgeInsets = UIEdgeInsetsMake(-(imageHeight+space), -imageWith, 0, 0);
break;
}
case UIViewContentModeLeft:{
//标题在左
imageEdgeInsets = UIEdgeInsetsMake(0, (labelWidth+space/2), 0, -(labelWidth+space/2));
labelEdgeInsets = UIEdgeInsetsMake(0, -(imageWith+space/2), 0, (imageWith+space/2));
break;
}
case UIViewContentModeBottom:{
//标题在下
imageEdgeInsets = UIEdgeInsetsMake(-(labelHeight+space), 0, 0, -labelWidth);
labelEdgeInsets = UIEdgeInsetsMake(0, -imageWith, -(imageHeight+space), 0);
break;
}
case UIViewContentModeRight:{
//标题在右
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;
}
网友评论