代码实现UILabel,并为UILabel添加样式,在应用开发时,UILabel做为显示控件是会被经常用到的。
所以总结了一下
- - (void)viewDidLoad
- {
- CGRect rect = CGRectMake(100, 200, 50, 50);
- UILabel *label = [[UILabel alloc] initWithFrame:rect];
- // 设置UILabel文字
- label.text = @"hello UILabel";
- // 设置Text为粗体
- label.font = [UIFont boldSystemFontOfSize:20];
- // 设置字体颜色
- label.textColor = [UIColor redColor];
- // 设置背景色
- label.backgroundColor = [UIColor clearColor];
- // 文字换行
- label.numberOfLines = 2;
- // 高亮显示
- label.highlighted = YES;
- label.highlightedTextColor = [UIColor orangeColor];
- //设置阴影
- label.shadowColor = [UIColor redColor];
- label.shadowOffset = CGSizeMake(1.0,1.0);
- //设置文字过长时的显示格式
- label.lineBreakMode = UILineBreakModeMiddleTruncation;//截去中间
- //设置label中的文字是否可变,默认值是YES
- label.enabled = YES;
- [self.view addSubview:label];
- [super viewDidLoad];
- }