美文网首页
iOS按钮偏移量操作

iOS按钮偏移量操作

作者: Sunshine_Boys | 来源:发表于2017-09-12 15:15 被阅读66次

一.原理

UIButton内有两个控件titleLabel和imageView,可以用来显示一个文本和图片,这里的图片区别于背景图片。给UIButton设置了title和image后,它们会图片在左边,文本在图片右边显示。它们两个做为一个整体依赖于button的contentHorizontalAlignment居左居右或居中显示。

1.当button.width < image.width时,只显示被压缩后的图片,图片是按fillXY的方式压缩。

2.当button.width > image.width,且 button.width < (image.width + text.width)时,图片正常显示,文本被压缩。

3.当button.width > (image.width + text.width),两者并列默认居中显示,可通过button的属性contentHorizontalAlignment改变对齐方式。

4.想两改变两个子控件的显示位置,可以分别通过setTitleEdgeInsets和setImageEdgeInsets来实现。需要注意的是,对titleLabel和imageView设置偏移,是针对它当前的位置起作用的,并不是针对它距离button边框的距离的。我测试下来,当button的contentHorizontalAlignment为居中时,偏移的距离和实际传的值有些偏差,没有发现规律,看不到源码也没在研究,但把button的contentHorizontalAlignment设为居左时,contentVerticalAlignment设为居上时,可以很方便的通过EdgeInsets改变两个子控件的位置。

二.直接上代码

OC版本

#pragma mark - 
-(void)setbtnimage:(UIImage*)image btntitle:(NSString*)title titlePosition:(UIViewContentMode)titlePosition additionalSpacing:(int)additionalSpacing state:(UIControlState)stateType{
    [self setImage:image forState:stateType];
    [self setTitle:title forState:stateType];
    [self.titleLabel setContentMode:UIViewContentModeCenter];
    [self.imageView setContentMode:UIViewContentModeCenter];
    [self setbuttonEdgeInsetsTitle:title titlePosition:titlePosition additionalSpacing:additionalSpacing];
}
-(void)setbuttonEdgeInsetsTitle:(NSString*)title titlePosition:(UIViewContentMode)Position additionalSpacing:(int)Spacing {
    CGSize imagesize = self.currentImage.size;
    CGSize titlesize = CGSizeMake([self getWithFromStr:title font:self.titleLabel.font.pointSize], imagesize.height);
    if (Position == UIViewContentModeTop) {
        [self setTitleEdgeInsets:UIEdgeInsetsMake(0, -(imagesize.width), self.frame.size.height/2 - Spacing, 0)];
        [self setImageEdgeInsets:UIEdgeInsetsMake(self.frame.size.height/2 + Spacing, 0, 0, -titlesize.width)];
    }else if (Position == UIViewContentModeBottom) {
        [self setTitleEdgeInsets:UIEdgeInsetsMake((self.frame.size.height/2  - Spacing), -(imagesize.width), 0, 0)];
        [self setImageEdgeInsets:UIEdgeInsetsMake(0, 0, self.frame.size.height/2 - 10 + Spacing, -titlesize.width)];
    }else if (Position == UIViewContentModeLeft){
        [self setTitleEdgeInsets:UIEdgeInsetsMake(0, -(imagesize.width)*2, 0, 0)];
        [self setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, -(titlesize.width*2 +Spacing))];
    }else if (Position == UIViewContentModeRight){
        [self setTitleEdgeInsets:UIEdgeInsetsMake(0, 0,0,-Spacing)];
        [self setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
    }
}
-(int)getWithFromStr:(NSString*)str font:(float)font {
    NSDictionary *attribute = @{NSFontAttributeName: self.titleLabel.font};
    CGSize retSize = [str boundingRectWithSize:CGSizeMake(MAXFLOAT, 20)
                                       options:\
                      NSStringDrawingTruncatesLastVisibleLine |
                      NSStringDrawingUsesLineFragmentOrigin |
                      NSStringDrawingUsesFontLeading
                                    attributes:attribute
                                       context:nil].size;
    return retSize.width;
}

swift版本

extension UIButton {
    
    func set(image anImage: UIImage?, title: String,
                   titlePosition: UIViewContentMode, additionalSpacing: CGFloat, state: UIControlState){
        self.imageView?.contentMode = .center
        self.titleLabel?.contentMode = .center
        self.setImage(anImage, for: state)
        self.setTitle(title, for: state)
        positionLabelRespectToImage(title: title, position: titlePosition, spacing: additionalSpacing)
    }
    
    private func positionLabelRespectToImage(title: String, position: UIViewContentMode,
                                             spacing: CGFloat) {
        let titleFont = self.titleLabel?.font!
        let imageSize = self.imageRect(forContentRect: self.frame)
        let titleSize = title.size(attributes: [NSFontAttributeName: titleFont!])
        
        var titleInsets: UIEdgeInsets
        var imageInsets: UIEdgeInsets
        
        switch (position){
        case .top:
            titleInsets = UIEdgeInsets(top: 0,left: -(imageSize.width), bottom: self.frame.size.height/2 - spacing, right: 0)
            imageInsets = UIEdgeInsets(top: self.frame.size.height/2 + spacing, left: 0, bottom: 0, right: -titleSize.width)
        case .bottom:
            titleInsets = UIEdgeInsets(top: (self.frame.size.height/2  - spacing),
                                       left: -(imageSize.width), bottom: 0, right: 0)
            imageInsets = UIEdgeInsets(top: 0, left: 0, bottom: self.frame.size.height/2 - 10 + spacing, 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 + spacing))
        case .right:
            titleInsets = UIEdgeInsets(top: 0, left: -(imageSize.width)*2, bottom: 0, right: 0)
            imageInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: -(titleSize.width*2+spacing))
        default:
            titleInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: -spacing)
            imageInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        }
        
        self.titleEdgeInsets = titleInsets
        self.imageEdgeInsets = imageInsets
    }
}

相关文章

网友评论

      本文标题:iOS按钮偏移量操作

      本文链接:https://www.haomeiwen.com/subject/kwucsxtx.html