美文网首页
UIButton图片和文字的位置

UIButton图片和文字的位置

作者: marlonxlj | 来源:发表于2017-09-21 22:22 被阅读22次

1、经常会遇到图片和文字在同一个按钮不同的位置,有时需求是图片在上,文字在下,有时是图片在下,文字在上等不同的需求,整个项目中到处都是这种情况。

2、还有就是对按钮的背景要求渐变色的处理


直接上效果图

IMG_2226(20170921-221015).jpg

代码

头文件

//
//  UIButton+YAKImagePosition.h
//  Easycare售后端
//
//  Created by m on 2017/9/21.
//  Copyright © 2017年 许留静. All rights reserved.
//

#import <UIKit/UIKit.h>

typedef enum : NSUInteger {
    ///图片在左,文字在右
    XLJImagePostionDefault,
    ///图片在右,文字在左
    XLJImagePostionRight,
    ///图片在上,文字在下
    XLJImagePostionTop,
    ///图片在下,文字在上
    XLJImagePostionBottom,
} XLJImagePositionType;

@interface UIButton (YAKImagePosition)

/**
 设置图片与文字的样式

 @param imagePosition 图片的位置
 @param spacing 图片与文字之间的间距
 */
- (void)XLJ_imagePosition:(XLJImagePositionType)imagePosition withSpacing:(CGFloat)spacing;


/**
 按钮背景颜色渐变
 @param buttonTitle 标题的文字
 */
- (void)XLJ_buttonBackgroundGradation:(NSString *)buttonTitle;

@end

//
//  UIButton+YAKImagePosition.m
//  Easycare售后端
//
//  Created by m on 2017/9/21.
//  Copyright © 2017年 许留静. All rights reserved.
//

#import "UIButton+YAKImagePosition.h"

@implementation UIButton (YAKImagePosition)

- (void)XLJ_imagePosition:(XLJImagePositionType)imagePosition withSpacing:(CGFloat)spacing
{
    CGFloat imageW = self.imageView.image.size.width;
    CGFloat imageH = self.imageView.image.size.height;
    CGFloat titleW = self.titleLabel.frame.size.width;
    
    CGFloat titleIntrinW = self.titleLabel.intrinsicContentSize.width;
    CGFloat titleIntrinH = self.titleLabel.intrinsicContentSize.height;
    
    switch (imagePosition) {
        case XLJImagePostionDefault:{
            self.imageEdgeInsets = UIEdgeInsetsMake(0, - 0.5 * spacing, 0, 0.5 * spacing);
            self.titleEdgeInsets = UIEdgeInsetsMake(0, 0.5 * spacing, 0, - 0.5 * spacing);
            }
            break;
        case XLJImagePostionRight:{
            
            CGFloat imageOffset = titleW + 0.5 * spacing;
            CGFloat titleOffset = imageW + 0.5 * spacing;
            self.imageEdgeInsets = UIEdgeInsetsMake(0, imageOffset, 0, - imageOffset);
            self.titleEdgeInsets = UIEdgeInsetsMake(0, - titleOffset, 0, titleOffset);
        }
            break;
        case XLJImagePostionTop:{
            
            self.imageEdgeInsets = UIEdgeInsetsMake(- titleIntrinH - spacing, 0, 0, - titleIntrinW);
            self.titleEdgeInsets = UIEdgeInsetsMake(0, - imageW, - imageH - spacing, 0);
        }
            break;
        case XLJImagePostionBottom:{
            self.imageEdgeInsets = UIEdgeInsetsMake(titleIntrinH + spacing, 0, 0, - titleIntrinW);
            self.titleEdgeInsets = UIEdgeInsetsMake(0, -imageW, imageH + spacing, 0);
        }
            break;
            
        default:
            break;
    }
}

- (void)XLJ_buttonBackgroundGradation:(NSString *)buttonTitle
{
    //按钮渐变
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.colors = @[(__bridge id)[UIColor colorWithHexString:@"#59a90d"].CGColor, (__bridge id)[UIColor colorWithHexString:@"#7ebf41"].CGColor];
    gradientLayer.locations = @[@0.3, @0.5, @1.0];
    gradientLayer.startPoint = CGPointMake(0, 0);
    gradientLayer.endPoint = CGPointMake(1.0, 0);
    gradientLayer.frame = self.bounds;
    
    [self.layer addSublayer:gradientLayer];
    [self setTitleColor:[UIColor colorWithHexString:@"#ffffff"] forState:UIControlStateNormal];
    self.titleLabel.font = [UIFont boldSystemFontOfSize:15];
    self.layer.cornerRadius = 5;
    self.layer.masksToBounds = YES;
    
    [self setTitle:buttonTitle forState:UIControlStateNormal];
}

@end


测试代码

//
//  YAKTestController.m
//  Easycare售后端
//
//  Created by m on 2017/9/21.
//  Copyright © 2017年 许留静. All rights reserved.
//

#import "YAKTestController.h"
#import "UIButton+YAKImagePosition.h"

@interface YAKTestController ()

@end

@implementation YAKTestController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //测试图片位置
    [self testButtonAndImage];
    
    //测试渐变按钮
    [self testChangeButton];
}

- (void)testChangeButton{
//    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 120, 35) withTitle:@"美女你好"];
    
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 120, 35)];
    
    [btn XLJ_buttonBackgroundGradation:@"美好你好吗?"];
    
    [self.view addSubview:btn];
    
}

- (void)testButtonAndImage{
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(100, 200, 120, 35)];
    btn.backgroundColor = [UIColor darkGrayColor];
    [self.view addSubview:btn];
    btn.titleLabel.font = Font(12);
    [btn setTitle:@"美女你好" forState:UIControlStateNormal];
    [btn setImage:IMAGENamed(@"gerenxinxi") forState:UIControlStateNormal];
    //图片在左,文字在右
    [btn XLJ_imagePosition:XLJImagePostionDefault withSpacing:15];
    
    UIButton *btn1 =[[UIButton alloc] initWithFrame:CGRectMake(100, 250, 120, 65)];
    [btn1 setTitle:@"美女你好1" forState:UIControlStateNormal];
    [btn1 setImage:IMAGENamed(@"gerenxinxi") forState:UIControlStateNormal];
    btn1.titleLabel.font = Font(12);
    [btn1 XLJ_imagePosition:XLJImagePostionRight withSpacing:15];
    [self.view addSubview:btn1];
    btn1.backgroundColor = [UIColor darkGrayColor];
    
    UIButton *btn2 =[[UIButton alloc] initWithFrame:CGRectMake(100, 350, 60, 65)];
    [btn2 setTitle:@"美女你好2" forState:UIControlStateNormal];
    [btn2 setImage:IMAGENamed(@"gerenxinxi") forState:UIControlStateNormal];
    btn2.titleLabel.font = Font(12);
    [btn2 XLJ_imagePosition:XLJImagePostionTop withSpacing:15];
    [self.view addSubview:btn2];
    btn2.backgroundColor = [UIColor darkGrayColor];
    
    UIButton *btn3 =[[UIButton alloc] initWithFrame:CGRectMake(185, 350, 60, 65)];
    [btn3 setTitle:@"美女你好3" forState:UIControlStateNormal];
    [btn3 setImage:IMAGENamed(@"gerenxinxi") forState:UIControlStateNormal];
    btn3.titleLabel.font = Font(12);
    [btn3 XLJ_imagePosition:XLJImagePostionBottom withSpacing:15];
    [self.view addSubview:btn3];
    btn3.backgroundColor = [UIColor darkGrayColor];
    

}

@end

相关文章

网友评论

      本文标题:UIButton图片和文字的位置

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