ios自适应

一,    label适应文字多少


UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(40, 100, WIDTH-80, 50)];

lab.textColor = [UIColor blackColor];

lab.layer.borderWidth = 2;

lab.layer.borderColor = [UIColor redColor].CGColor;

// label 适应文字大小多少

[lab szieAdaptiveWithText:@"323.45" andTextFont:[UIFont systemFontOfSize:30] andTextMaxSzie:CGSizeMake(5, 5)];

[self.view addSubview:lab];



UILabel *lab1 = [[UILabel alloc] initWithFrame :CGRectMake(40, 200, WIDTH-80, 50)];

lab1.textColor = [UIColor blackColor];

lab1.layer.borderWidth = 2;

lab1.layer.borderColor = [UIColor redColor].CGColor;

[lab1 szieAdaptiveWithText:@"323.45affafda" andTextFont:[UIFont systemFontOfSize:30] andTextMaxSzie:CGSizeMake(5, 5)];

[self.view addSubview:lab1];



UILabel *lab2 = [[UILabel alloc] initWithFrame:CGRectMake(40, 300, WIDTH-80, 50)];

lab2.textColor = [UIColor blackColor];

lab2.layer.borderWidth = 2;

lab2.layer.borderColor = [UIColor redColor].CGColor;

[lab2 szieAdaptiveWithText:@"这是测试的文字这是测试的文字" andTextFont:[UIFont systemFontOfSize:30] andTextMaxSzie:CGSizeMake(5, 5)];

lab2.textAlignment = NSTextAlignmentLeft;

[self.view addSubview:lab2];


二,  文字适应固定大小的label


效果图片


UILabel *progress_text = [[UILabel alloc] initWithFrame: CGRectMake(20, 100, 300, 50)];

progress_text.backgroundColor = [UIColor blackColor];

progress_text.text = @"人生难免经历苦痛挣扎🐂🐂🐂°°°°°°°°°°°°挣扎🐂🐂";

progress_text.textColor = [UIColor whiteColor];

progress_text.adjustsFontSizeToFitWidth = YES;

progress_text.textAlignment = NSTextAlignmentCenter;

progress_text.font = [UIFont systemFontOfSize:20];

[self.view addSubview: progress_text];



UILabel *progress_text2 = [[UILabel alloc] initWithFrame: CGRectMake(20, 200, 300, 50)];

progress_text2.backgroundColor = [UIColor blackColor];

progress_text2.text = @"人生难免经历苦痛挣扎人生难免经历苦痛挣扎";

progress_text2.textColor = [UIColor whiteColor];

progress_text2.adjustsFontSizeToFitWidth = YES;

progress_text2.textAlignment = NSTextAlignmentCenter;

progress_text2.font = [UIFont systemFontOfSize:20];

[self.view addSubview: progress_text2];



UILabel *progress_text3 = [[UILabel alloc] initWithFrame: CGRectMake(20, 300, 300, 50)];

progress_text3.backgroundColor = [UIColor blackColor];

progress_text3.text = @"人生难免经历苦痛挣扎人生难免经历苦痛挣扎人生难免经历苦痛挣扎";

progress_text3.textColor = [UIColor whiteColor];

progress_text3.adjustsFontSizeToFitWidth = YES;

progress_text3.textAlignment = NSTextAlignmentCenter;

progress_text3.font = [UIFont systemFontOfSize:20];

[self.view addSubview: progress_text3];



UILabel *progress_text4 = [[UILabel alloc] initWithFrame: CGRectMake(20, 400, 300, 50)];

progress_text4.backgroundColor = [UIColor blackColor];

progress_text4.text = @"人生难免经历苦痛挣扎人生难免经历苦痛挣扎人生难免经历苦痛挣扎扎";

progress_text4.numberOfLines = 0;

progress_text4.textColor = [UIColor whiteColor];

progress_text4.adjustsFontSizeToFitWidth = YES;

progress_text4.textAlignment = NSTextAlignmentCenter;

progress_text4.font = [UIFont systemFontOfSize:20];

[self.view addSubview: progress_text4];

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容