ios控件-UILabel

UILabel控件


- (void)viewDidLoad {
    [super viewDidLoad];
    
    //初始化label
    UILabel *label = [[UILabel alloc] init];
    label.frame = CGRectMake(50, 50, 150, 100);
    
    label.text = @"what the fack";
    
    //设置阴影
//    label.shadowColor = [UIColor redColor];
//    label.shadowOffset = CGSizeMake(1.0,1.0);
    
    // 设置高亮
//    label.highlighted = YES;
//    label.highlightedTextColor = [UIColor orangeColor];
    
    // 设置文本字体和大小
    label.font = [UIFont fontWithName:@"Arial" size:15];
    label.font = [UIFont systemFontOfSize:15];
    label.font = [UIFont boldSystemFontOfSize:15];  // 粗体
    
    // 设置字体颜色
    label.textColor = [UIColor orangeColor];
    
    //设置背景颜色
    label.backgroundColor = [UIColor blueColor];
    
    //设置字体居中
    /**
     NSTextAlignmentLeft      = 0,    // Visually left aligned
     NSTextAlignmentCenter    = 1,    // Visually centered
     NSTextAlignmentRight     = 2,    // Visually right aligned
     */
    label.textAlignment = NSTextAlignmentCenter;
    
    //设置单词折行方式
    /**
     NSLineBreakByWordWrapping = 0,default 以单词为单位换行,以单位为单位截断。
     NSLineBreakByCharWrapping,//以字符为单位换行,以字符为单位截断。
     NSLineBreakByClipping,	//以单词为单位换行。以字符为单位截断。
     NSLineBreakByTruncatingHead,//Truncate at head of line: "...wxyz"
     NSLineBreakByTruncatingTail,//Truncate at tail of line: "abcd..."
     NSLineBreakByTruncatingMiddle//Truncate middle of line:  "ab...yz"
     */
//    label.lineBreakMode = NSLineBreakByWordWrapping;
    
    //设置label是否可以显示多行,0则显示多行
    label.numberOfLines = 0;
    
    //设置字体大小适宜label宽度
    label.adjustsFontSizeToFitWidth = YES;
    
    //设置是否能与用户交互
    label.userInteractionEnabled = YES;
    
    [self.view addSubview:label];
    
    
    /**
     uilabel的Attributed用法
     */
    UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, 300, 300)];
    
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"This is my test code to test this label style is working or not on the text to show other user"];
    
    [str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,31)];
    [str addAttribute:NSBackgroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(61,10)];
    
    [str addAttribute:NSFontAttributeName value: [UIFont fontWithName:@"Helvetica-Bold" size:13.0] range:NSMakeRange(32, 28)];
    [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Helvetica-Bold" size:13.0] range:NSMakeRange(65, 20)];
    
    label1.attributedText = str;
    //设置label是否可以显示多行,0则显示多行
    label1.numberOfLines = 0;
    
    [self.view addSubview:label1];
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值