設(shè)置Label的EdgeInsets以及動(dòng)態(tài)改變label的高度

1.設(shè)置label的EdgeInsets(內(nèi)邊距)
代碼如下:
#import <UIKit/UIKit.h>
@interface EdgeLabel : UILabel

@property(nonatomic, assign) UIEdgeInsets edgeInsets;

@end

#import "EdgeLabel.h"

@implementation EdgeLabel

- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines {
    UIEdgeInsets insets = self.edgeInsets;
    CGRect rect = [super textRectForBounds:UIEdgeInsetsInsetRect(bounds, insets)
                    limitedToNumberOfLines:numberOfLines];
    rect.origin.x    -= insets.left;
    rect.origin.y    -= insets.top;
    rect.size.width  += (insets.left + insets.right);
    rect.size.height += (insets.top + insets.bottom);
    return rect;
}

- (void)drawTextInRect:(CGRect)rect {
    [super drawTextInRect:UIEdgeInsetsInsetRect(rect, self.edgeInsets)];
}
@end

2.根據(jù)字符串動(dòng)態(tài)改變label的高度
代碼如下:
#import "ViewController.h"
#import "EdgeLabel.h"

#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define FONT_SIZE 14
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *titleContent  = @"我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試我的測(cè)試";

    EdgeLabel *label        = [EdgeLabel new];
    label.numberOfLines     = 0;
    label.edgeInsets        = UIEdgeInsetsMake(0, 10, 10, 10);
    label.textAlignment     = NSTextAlignmentJustified;
    label.font              = [UIFont systemFontOfSize:FONT_SIZE];
    label.textColor         = [UIColor lightGrayColor];
    label.backgroundColor   = [UIColor cyanColor];
    label.text              = titleContent;
    CGSize labelSize        = [titleContent boundingRectWithSize:CGSizeMake(SCREEN_WIDTH - (label.edgeInsets.left + label.edgeInsets.right) , MAXFLOAT)
                                                         options:NSStringDrawingUsesLineFragmentOrigin
                                                      attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:FONT_SIZE]}
                                                         context:nil].size;
    CGFloat realHeight      = (SCREEN_WIDTH  * (labelSize.height + label.edgeInsets.top + label.edgeInsets.bottom))/SCREEN_WIDTH;
    label.frame             = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, realHeight);
    label.center            = self.view.center;
    [self.view addSubview:label];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end

說(shuō)明

1.因?yàn)槲覀冊(cè)趧?dòng)態(tài)改變label高度的時(shí)候,設(shè)置了EdgeInsets,所以需要在

     CGSize labelSize        = [titleContent boundingRectWithSize:CGSizeMake(SCREEN_WIDTH - (label.edgeInsets.left + label.edgeInsets.right) , MAXFLOAT)
                                                         options:NSStringDrawingUsesLineFragmentOrigin
                                                      attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:FONT_SIZE]}
                                                         context:nil].size;

中讓SCREEN_WIDTH減去左右內(nèi)邊距的值 (label.edgeInsets.left + label.edgeInsets.right)

2.然后我們?cè)僦匦掠?jì)算高度,讓之前的高度值加上label上下內(nèi)邊距的值,即:

    CGFloat realHeight      = (SCREEN_WIDTH  * (labelSize.height + label.edgeInsets.top + label.edgeInsets.bottom))/SCREEN_WIDTH;
最后的效果圖如下:
7A7907FA17D489E47732CF5FCD8A7AAB.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),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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