iOS button 文字圖片上下/左右排布

網(wǎng)上之前的方法:
http://www.cnblogs.com/LynnAIQ/p/5908664.html

上下:
    //使圖片和文字水平居中顯示
    self.rechargeButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    //文字距離上邊框的距離增加imageView的高度,距離左邊框減少imageView的寬度,距離下邊框和右邊框距離不變
    [self.rechargeButton setTitleEdgeInsets:UIEdgeInsetsMake(self.rechargeButton.imageView.frame.size.height+10 ,-self.rechargeButton.imageView.frame.size.width, 0.0,0.0)];
    //圖片距離右邊框距離減少圖片的寬度,其它不邊
    [self.rechargeButton setImageEdgeInsets:UIEdgeInsetsMake(-10, 0.0,0.0, -self.rechargeButton.titleLabel.bounds.size.width)];

 左右:(因?yàn)槟J(rèn)button  圖片在左,文字在右, 下面代碼是反過來的:   文字  圖片)
    [self.DetailButton setTitleEdgeInsets:UIEdgeInsetsMake(0, -self.DetailButton.imageView.bounds.size.width, 0, self.DetailButton.imageView.bounds.size.width)];
    [self.DetailButton setImageEdgeInsets:UIEdgeInsetsMake(0, self.DetailButton.titleLabel.bounds.size.width, 0, -self.DetailButton.titleLabel.bounds.size.width)];

我不確定是不是因?yàn)樗麄兊腷tn.titleLabel的文字寬度一樣,所以沒發(fā)生問題
當(dāng)我用的時(shí)候,圖片左右偏移量是錯(cuò)亂的,像這樣:

false1.png

所以,setImageEdgeInsets這一步操作前,需要計(jì)算titleLabel的實(shí)際寬度:

    CGSize maxSize = CGSizeMake(1000, shareBtn.titleLabel.height);
    CGSize fitSize = [shareBtn.titleLabel sizeThatFits:maxSize];
    CGFloat w_titleLabel = fitSize.width;

完整代碼:

    NSArray *array1 = @[@"微信",@"朋友圈",@"QQ",@"QQ空間"];
    NSArray *array2 = @[@"fenxiangweixin",@"fenxiangpyq",@"fenxiangqq",@"fenxiangkongj"];
    CGFloat wid_icon = (AL_DEVICE_WIDTH-20)/4;
    for (int i=0; i<array1.count; i++) {
        UIButton *shareBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.view addSubview:shareBtn];
        [shareBtn setImage:[UIImage imageNamed:array2[i]] forState:UIControlStateNormal];
        shareBtn.frame = CGRectMake(10+i*wid_icon, iconA.bottom, wid_icon, wid_icon);
        [shareBtn setTitle:array1[i] forState:UIControlStateNormal];
        shareBtn.titleLabel.textAlignment = 1;
        shareBtn.titleLabel.font = FontSystem(17);
        [shareBtn setTitleColor:UICOLOR_HEX(0x000000) forState:UIControlStateNormal];
        [shareBtn addTarget:self action:@selector(inviteMyFriend:) forControlEvents:UIControlEventTouchUpInside];
        [shareBtn setAdjustsImageWhenDisabled:NO];       
 
        //關(guān)鍵步驟
        shareBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        //調(diào)用方法
        [self initButton:shareBtn];
    }
//方法直接調(diào)用
-(void)initButton:(UIButton*)shareBtn{
    CGSize maxSize = CGSizeMake(1000, shareBtn.titleLabel.height);
    CGSize fitSize = [shareBtn.titleLabel sizeThatFits:maxSize];
    CGFloat w_titleLabel = fitSize.width;
    CGFloat w_imageV = shareBtn.imageView.width;
    CGFloat h_imageV = shareBtn.imageView.height;
    
    //文字距離上邊框的距離增加imageView的高度,距離左邊框減少imageView的寬度,距離下邊框和右邊框距離不變
    [shareBtn setTitleEdgeInsets:UIEdgeInsetsMake(h_imageV+20 ,-w_imageV, 0.0,0.0)];
    //圖片距離右邊框距離減少圖片的寬度,其它不變
    [shareBtn setImageEdgeInsets:UIEdgeInsetsMake(-10, 0.0,0.0, -w_titleLabel)];
}

結(jié)果:

false2.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容