UIAlertView文字居左,UILabel內(nèi)邊距設(shè)置

最近在項(xiàng)目中,需要有個(gè)強(qiáng)制更新功能,更新內(nèi)容,動(dòng)態(tài)顯示,系統(tǒng)的content設(shè)置,內(nèi)容是居中,丑爆了??!所以自定義個(gè)UILabel,因?yàn)樾枰袃?nèi)邊距!

UILabel不像UIButton那樣,有個(gè)contentEdgeInsets、titleEdgeInsets、imageEdgeInsets供我們?cè)O(shè)置文字或圖片與按鈕邊界的界限,所以我們只能另外想其他辦法來(lái)實(shí)現(xiàn)。其實(shí),辦法也很簡(jiǎn)單,只需要我們自定義UILabel,然后重寫drawTextInRect:方法即可實(shí)現(xiàn)我們的目標(biāo)。

如下圖

圖片發(fā)自簡(jiǎn)書App
 UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"發(fā)現(xiàn)新版本"
                                                                   message:nil
                                                                  delegate:self
                                                         cancelButtonTitle:nil
                                                         otherButtonTitles:@"立即更新", nil];
                    alert.tag = 3;
                    DGLabel *textLabel = [[DGLabel alloc] init];
                    textLabel.font = [UIFont systemFontOfSize:13];
                    textLabel.numberOfLines =0;
                    textLabel.textAlignment =NSTextAlignmentLeft;
                    textLabel.textInsets = UIEdgeInsetsMake(0.f, 10.f, 0.f, 10.f); // 設(shè)置左內(nèi)邊距
                    textLabel.text = responseObject[@"content"];
                    [alert setValue:textLabel forKey:@"accessoryView"];
                    [alert show];

DGLabel.h

#import <UIKit/UIKit.h>

@interface DGLabel : UILabel

@property (nonatomic, assign) UIEdgeInsets textInsets; // 控制字體與控件邊界的間隙

@end

DGLabel.m

#import "DGLabel.h"

@implementation DGLabel

- (instancetype)init {
    if (self = [super init]) {
        _textInsets = UIEdgeInsetsZero;
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        _textInsets = UIEdgeInsetsZero;
    }
    return self;
}

- (void)drawTextInRect:(CGRect)rect {
    [super drawTextInRect:UIEdgeInsetsInsetRect(rect, _textInsets)];
}

@end

最后編輯于
?著作權(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),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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