UIButton的图片和文字相对位置调整


通常。假设直接设置UIButton的图片和文字,默认的两者相对位置可能不是我们想要的,那么须要进行调整。

须要用到的函数例如以下:

UIEdgeInsetsMake(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right)

top,left,bottom,right分别表示向各个方向的移动量


实例说明:


    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(50, 100, 300, 300)];
    //设置文字
    [button setTitle:@"測试" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:25];
    //为了有所区分,设置按钮背景颜色为黑色
    button.backgroundColor = [UIColor blackColor];
    //设置图片
    [button setImage:[UIImage imageNamed:@"QQ"] forState:UIControlStateNormal];
    [self.view addSubview:button];

效果图:
图一


调整:

//    假设想要调整title的位置到image下方,那么能够调整title向下,向左移动
    button.titleEdgeInsets = UIEdgeInsetsMake(125, -150, 0, 0);

效果图:
图二


通常,button的大小不会这么大,所以要在有限的空间内。同一时候调整title和image的位置

    button.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    button.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
posted @ 2017-07-13 10:48  wzjhoutai  阅读(171)  评论(0编辑  收藏  举报