设置UIButton的title和image

yuankai66 发布于 2014/09/30 09:45
阅读 521
收藏 0
请问大家:设置button的title和image的时候,不论title字体的多少,如何始终保持title和image同样的间距?谢谢!!!想要实现的效果:无论title是几个字,始终和image保持同等的间距。 示例代码如下:


- (void)viewDidLoad

{

    [super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

    

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

    button.frame = CGRectMake(10, 100, 300, 40);

    button.backgroundColor = [UIColor lightGrayColor];

    [button setTitle:@"三个字" forState:UIControlStateNormal];

    [button setImage:[UIImage imageNamed:@"2.png"] forState:UIControlStateNormal];

    [button setTitleEdgeInsets:UIEdgeInsetsMake(0, -50, 0, 0)];

    [button setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, -128)];

    [button setContentEdgeInsets:UIEdgeInsetsMake(0, -50, 0, 0)];

    button.titleLabel.textAlignment = NSTextAlignmentRight;

    [self.view addSubview:button];

    

    

    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];

    button2.frame = CGRectMake(10, 160, 300, 40);

    button2.backgroundColor = [UIColor lightGrayColor];

    [button2 setTitle:@"多一个字" forState:UIControlStateNormal];

    [button2 setImage:[UIImage imageNamed:@"2.png"] forState:UIControlStateNormal];

    [button2 setTitleEdgeInsets:UIEdgeInsetsMake(0, -50, 0, 0)];

    [button2 setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, -128)];

    [button2 setContentEdgeInsets:UIEdgeInsetsMake(0, -50, 0, 0)];

     button2.titleLabel.textAlignment = NSTextAlignmentRight;

    [self.view addSubview:button2];

}


加载中
返回顶部
顶部