IOS中UILabel排版

#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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值