UILabel继承与UIView,是iOS设计非常常用的一个控件
//label初始化
UILabel *lable = [[UILabel alloc] init];
//label的位置
lable.frame = CGRectMake(20, 100, 300, 50);
//label的内容
lable.text = @"这是一个比较长的label,这是一个比较长的label,这是一个比较长的label,这是一个比较长的label,这是最后几个结尾字";
//label的背景颜色
lable.backgroundColor = [UIColor yellowColor];
//label内容的颜色
lable.textColor = [UIColor redColor];
//label字体的大小
lable.font = [UIFont systemFontOfSize:30];
//设置阴影颜色
lable.shadowColor = [UIColor blackColor];
//label阴影的偏移量
lable.shadowOffset = CGSizeMake(3, 3);
//label的对齐方式
lable.textAlignment = NSTextAlignmentCenter;
//label的边缘属性
lable.layer.masksToBounds = YES;
//label的圆角
lable.layer.cornerRadius = 20;
//label边线颜色
lable.layer.borderColor = [[UIColor purpleColor] CGColor];
//label边线宽度
lable.layer.borderWidth = 3;
//文字超长处理
label.lineBreakMode = NSLineBreakByTruncatingMiddle;
[self.view addSubview:lable];
UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(20, 250, 200, 50)];
label2.backgroundColor = [UIColor yellowColor];
[self.view addSubview:label2];
//==================
//多属性字符串的使用:$700 ,$是红色的,700是黑色的
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc]init];
//NSFontAttributeName字典的key,代表字体
//NSForegroundColorAttributeName 文字颜色
//NSBackgroundColorAttributeName 文字背景色
NSAttributedString *str1 =[[NSAttributedString alloc]initWithString:@"$" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:35],NSForegroundColorAttributeName:[UIColor redColor]}];
NSAttributedString *str2 = [[NSAttributedString alloc]initWithString:@"700" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:40],NSForegroundColorAttributeName:[UIColor blackColor]}];
[attrString appendAttributedString:str1];
[attrString appendAttributedString:str2];
label2.attributedText = attrString;
UILabel *label3 = [[UILabel alloc] initWithFrame:CGRectMake(20, 320, 200, 250)];
[self.view addSubview:label3];
label3.backgroundColor = [UIColor yellowColor];
//用户可交互
label3.userInteractionEnabled = YES;
//label高亮
label3.highlighted = YES;
//label高亮颜色
label3.highlightedTextColor = [UIColor purpleColor];
//label字体大小适应label宽度(与numberOfLines冲突)
label3.adjustsFontSizeToFitWidth = YES;
label3.text = @"这是一个比较长的label,这是一个比较长的label,这是一个比较长的label,这是一个比较长的label,这是最后几个结尾字";
//多行控制为0时,表示多行
label3.numberOfLines = 0;
对齐方式textAlignment:
NSTextAlignmentLeft = 0, // Visually left aligned 左对齐
NSTextAlignmentCenter = 1, // Visually centered 居中
NSTextAlignmentRight = 2, // Visually right aligned 右对齐
NSTextAlignmentJustified = 3, // Fully-justified. The last line in a paragraph is natural-aligned.
NSTextAlignmentNatural = 4, // Indicates the default alignment for script
文字超长处理lineBreakMode:
NSLineBreakByWordWrapping = 0, // Wrap at word boundaries, default 包在用词边界(默认)应该是能够识别空格
NSLineBreakByCharWrapping, // Wrap at character boundaries 包在字符边界
NSLineBreakByClipping, // Simply clip 直接截取
NSLineBreakByTruncatingHead, // Truncate at head of line: "...wxyz" 头部缩略
NSLineBreakByTruncatingTail, // Truncate at tail of line: "abcd..." 尾部缩略
NSLineBreakByTruncatingMiddle // Truncate middle of line: "ab...yz" 中间缩略
文本基线控制baselineAdjustment:
UIBaselineAdjustmentAlignBaselines = 0, // default. 默认文本最上端与label中间线
UIBaselineAdjustmentAlignCenters, //text中间与label中间线对齐
UIBaselineAdjustmentNone, //text最底端与label中间线对齐