设置按钮UIButton中文字在图片的下面

一、想实现文字在按钮的下面

 

- (void)setupUI {

    UIButton *inviteButton = [UIButton buttonWithType:UIButtonTypeCustom];
    inviteButton.frame = CGRectMake(51, 19.5+64, 45, 43.5);
    [inviteButton setImage:[UIImage imageNamed:@"邀歌求词"] forState:UIControlStateNormal];
    [inviteButton setTitle:@"邀歌求词" forState:UIControlStateNormal];
    inviteButton.titleLabel.font = [UIFont systemFontOfSize:10];
    [inviteButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [inviteButton addTarget:self action:@selector(pushToPublish) forControlEvents:UIControlEventTouchUpInside];
   
    // 按钮图片和标题总高度
    CGFloat totalHeight = (inviteButton.imageView.frame.size.height + inviteButton.titleLabel.frame.size.height);
   
    // 设置按钮图片偏移
    [inviteButton setImageEdgeInsets:UIEdgeInsetsMake(-(totalHeight - inviteButton.imageView.frame.size.height), 0.0, 5, -inviteButton.titleLabel.frame.size.width)];
   
    // 设置按钮标题偏移
    [inviteButton setTitleEdgeInsets:UIEdgeInsetsMake(0.0, -inviteButton.imageView.frame.size.width - 10, -(totalHeight - inviteButton.titleLabel.frame.size.height)- 10,0.0)];
 
    [self.view addSubview:inviteButton];

}

 

还有一个实现该功能的方法就是让我们的UI设计师,直接切一张带文字的图片给我们,既省时又省事。

转载于:https://my.oschina.net/u/2613740/blog/693946

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值