#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//1.普通
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[self.view addSubview:label];
label.backgroundColor = [UIColor redColor];
label.text = @"xxxx";
UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, 100, 100)];
[self.view addSubview:label1];
label1.backgroundColor = [UIColor greenColor];
label1.text = @"xxxx";
//2.水平居中
label1.textAlignment = NSTextAlignmentCenter;
UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 200, 100, 100)];
[self.view addSubview:label2];
label2.backgroundColor = [UIColor yellowColor];
label2.text = @"xxxx";
//3.水平居右
label2.textAlignment = NSTextAlignmentRight;
UILabel *label3 = [[UILabel alloc] initWithFrame:CGRectMake(0, 300, 100, 100)];
[self.view addSubview:label3];
label3.backgroundColor = [UIColor cyanColor];
label3.text = @"xxxx\n\n\n ";
//4.垂直居上
[label3 sizeToFit];
label3.lineBreakMode = NSLineBreakByTruncatingTail;
label3.numberOfLines = 0;
label3.frame = CGRectMake(0, 300, 100, 100);
UILabel *label4 = [[UILabel alloc] initWithFrame:CGRectMake(0, 400, 100, 100)];
[self.view addSubview:label4];
label4.backgroundColor = [UIColor purpleColor];
label4.textAlignment = NSTextAlignmentCenter;
label4.text = @"xxxx\n\n\n ";
//4.垂直居上,水平居中
[label4 sizeToFit];
label4.lineBreakMode = NSLineBreakByTruncatingTail;
label4.numberOfLines = 0;
label4.frame = CGRectMake(0, 400, 100, 100);
UILabel *label5 = [[UILabel alloc] initWithFrame:CGRectMake(([UIScreen mainScreen].bounds.size.width-100)/2, ([UIScreen mainScreen].bounds.size.height-100)/2-200, 100, 100)];
[self.view addSubview:label5];
label5.backgroundColor = [UIColor blueColor];
label5.textAlignment = NSTextAlignmentCenter;
//加下划线
NSMutableAttributedString *content = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"]];
NSRange contentRange = {0,[content length]};
[content addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:contentRange];
label5.attributedText = content;
label5.lineBreakMode = NSLineBreakByTruncatingTail;
//多行
label5.numberOfLines = 0;
//自适应
[label5 sizeToFit];
UILabel *label6 = [[UILabel alloc] initWithFrame:CGRectMake(([UIScreen mainScreen].bounds.size.width-100)/2, ([UIScreen mainScreen].bounds.size.height-100)/2, 100, 100)];
[self.view addSubview:label6];
label6.backgroundColor = [UIColor orangeColor];
label5.textAlignment = NSTextAlignmentCenter;
//加下划线
NSMutableAttributedString *content1 = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"]];
NSRange contentRange1 = {0,[content1 length]};
[content1 addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:contentRange1];
label6.attributedText = content1;
label6.lineBreakMode = NSLineBreakByTruncatingTail;
//多行
label6.numberOfLines = 0;
//自适应
[label6 sizeToFit];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
IOS中UILabel排版
最新推荐文章于 2020-09-05 01:14:04 发布