UIButton中的**EdgeInsets是做什么用的?

本文详细介绍了UIButton中的EdgeInsets属性的作用,包括如何使用EdgeInsets调整按钮内图片和文本的位置与大小,并通过实例展示了EdgeInsets对按钮内容的影响。

UIButton中的**EdgeInsets是做什么用的?

 

UIEdgeInsetsMake

Creates an edge inset for a button or view.
An inset is a margin around the drawing rectangle where each side (left, right, top, and bottom) can have a different value.

给一个button或者view创建一个嵌入的边缘.

inset是一个矩形view页边空白,每一边都可以设定一个不同的值.

 

图片素材: 1@2x.png


- (void)viewDidLoad
{
    [super viewDidLoad];

    UIButton *normalButton = nil;
    UIButton *edgeButton   = nil;
    
    // normalButton
    {
        // 初始化按钮
        normalButton = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
        normalButton.backgroundColor = [UIColor blackColor];
        
        // 按钮图片
        [normalButton setImage:[UIImage imageNamed:@"1"] forState:UIControlStateNormal];
    }
    
    // EdgeButton
    {
        // 初始化按钮
        edgeButton = [[UIButton alloc] initWithFrame:CGRectMake(100, 210, 100, 100)];
        edgeButton.backgroundColor = [UIColor blackColor];
        
        // 按钮图片
        [edgeButton setImage:[UIImage imageNamed:@"1"] forState:UIControlStateNormal];
        
        // 设置图片间距
        edgeButton.imageEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10);
    }
    
    [self.view addSubview:normalButton];
    [self.view addSubview:edgeButton];
}

viewDidLoad

以下是模拟器显示结果:

从结果中可以看到,设置了图片的imageEdgeInsets后,图片显示被缩放了哦.

 

再来测试文本的titleEdgeInsets

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIButton *normalButton = nil;
    UIButton *edgeButton   = nil;
    
    // normalButton
    {
        // 初始化按钮
        normalButton = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
        normalButton.backgroundColor = [UIColor blackColor];
        
        [normalButton setTitle:@"Y.X." forState:UIControlStateNormal];
        [normalButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    }
    
    // EdgeButton
    {
        // 初始化按钮
        edgeButton = [[UIButton alloc] initWithFrame:CGRectMake(100, 210, 100, 100)];
        edgeButton.backgroundColor = [UIColor blackColor];
        
        [edgeButton setTitle:@"Y.X." forState:UIControlStateNormal];
        [edgeButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        
        edgeButton.titleEdgeInsets = UIEdgeInsetsMake(60, 60, 0, 0);
    }
    
    [self.view addSubview:normalButton];
    [self.view addSubview:edgeButton];
}

viewDidLoad

以下是运行结果:

文本并没有缩放的说,再试试这么设置:

UIButton还有一个属性叫contentEdgeInsets.

Use this property to resize and reposition the effective drawing rectangle for the button content. The content comprises the button image and button title.

实际上,这个contentEdgeInsets是用来同时设置imageEdgeInsets与titleEdgeInsets的,没什么特别的,就不举例子了:)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值