sizeToFit 解决Label的高度自适应碰到的一些问题

版权声明:欢迎学习交流。 https://blog.csdn.net/matiji66/article/details/50256405

说明:

1.此处用到了一些UIView的类扩展

2.sizeWithString 这个方法并不好用,只显示两行文字,其他的文字都显示不了 

3.不要与Mas 自动布局一起使用否则会导致sizeToFit不起作用

4.heightForString 这个方法是能够实现 自适应高度的

 

#import "ViewController.h"

#define KHeight  [[UIScreen mainScreen] bounds]size.height

#define KWidth   [[UIScreen mainScreen] bounds].size.width

#import "UIView+Extension.h"


@interface ViewController ()


@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    

    NSString *testString =@"Do any additional setup after loading the view, typically from a nib.Do any additional setup after loading the view, typically from a nib.";


    UILabel *label = [[UILabelalloc] initWithFrame:CGRectMake(30,20, KWidth -30 *2, 12)];

    label.text = testString;

    label.numberOfLines = 0;

    [label sizeToFit];

    label.backgroundColor = [UIColorlightGrayColor];

    [self.viewaddSubview:label ];

    

    UILabel *label2 = [[UILabelalloc] initWithFrame:CGRectMake(10,CGRectGetMaxY(label.frame),KWidth - 10,30)];

    label2.text = testString;

    label2.numberOfLines = 0;

    label2.size = [selfsizeWithString:label.textfont:[UIFontsystemFontOfSize:15]];

//    [label2 sizeToFit];

    label2.backgroundColor = [UIColororangeColor];

    [self.viewaddSubview:label2 ];

    

    UILabel *label3 = [[UILabelalloc] initWithFrame:CGRectMake(10,CGRectGetMaxY(label2.frame),KWidth - 20,30)];

    label3.text = testString;

    label3.numberOfLines = 0;

    //label2.size = [self sizeWithString:label.text font:[UIFont systemFontOfSize:15]];

//    [label3 sizeToFit];

    label3.backgroundColor = [UIColorblueColor];

    label3.height = [ViewControllerheightForString:testString fontSize:14 andWidth:KWidth-20 ];

    [self.viewaddSubview:label3];

 

}

//此处可以作为NSString 的类扩展方法使用

+(float)heightForString:(NSString *)value fontSize:(float)fontSize andWidth:(float)width

{

    

    float height = [[NSStringstringWithFormat:@"%@\n",value]boundingRectWithSize:CGSizeMake(width,CGFLOAT_MAX)options:NSStringDrawingUsesLineFragmentOrigin |NSStringDrawingUsesFontLeading attributes:[NSDictionarydictionaryWithObjectsAndKeys:[UIFontsystemFontOfSize:fontSize],NSFontAttributeName,nil] context:nil].size.height;

    

    return height;

}


-(CGSize)sizeWithString:(NSString *)string font:(UIFont *)font

{

    //限制最大的宽度和高度//采用换行模式//传人的字体字典


    return  [[NSStringstringWithFormat:@"%@\n",string]boundingRectWithSize:CGSizeMake(KWidth -10, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin |NSStringDrawingUsesFontLeading attributes:[NSDictionarydictionaryWithObjectsAndKeys:font,NSFontAttributeName,nil] context:nil].size;

    

//    CGRect rect = [string boundingRectWithSize:CGSizeMake(KWidth, CGFLOAT_MAX)                                       options: NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName: font}

//                                       context:nil];

//    

//    return rect.size;

}



附图说明:

   

阅读更多
换一批

没有更多推荐了,返回首页