Label是oc的最常用的控件
Label主要用于显示文本信息,下面是常用的属性
//1.创建label 并且添加到view上
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 300, 200)];
[self.view addSubview:label];
//2.给label赋值,默认情况下是nil
label.text = @"我是一个文本内容";
//3.设置label的字体大小和样式,默认情况下是17
label.font = [UIFont systemFontOfSize:22]; //字体默认样式下改变字号
label.font = [UIFont boldSystemFontOfSize:22]; //字体加粗样式下改变字号
// label.font = [UIFont fontWithName:@"" size:22];//指定的字体样式下改变字号 引号中间写字体名称
//4.label字体颜色,默认情况下是黑色
label.textColor = [UIColor blueColor];
//5.label的shadowColor阴影颜色 shadowOffset阴影偏移量
label.shadowColor = [UIColor blackColor];
label.shadowOffset = CGSizeMake(-2, -2);
//6.label的textAlignment对齐方式
label.backgroundColor = [UIColor yellowColor];//给label一个背景色 方便看对齐方式
label.textAlignment = NSTextAlignmentLeft;
/*
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
*/
//7.label的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" 中间多余文字用......方式省略 显示前面后面文字
*/
label.lineBreakMode = NSLineBreakByTruncatingTail;
//8.label的attributedText标签属性文本
NSString *text = @"标签属性文本";
NSMutableAttributedString *textAttributedStr = [[NSMutableAttributedString alloc] initWithString:text];
[textAttributedStr setAttributes:@{
NSForegroundColorAttributeName:[UIColor greenColor],
NSFontAttributeName:[UIFont systemFontOfSize:15]
} range:NSMakeRange(2, 3)];
label.attributedText = textAttributedStr;
//9.label的highlightedTextColor 高亮状态
label.highlighted = YES;
label.highlightedTextColor = [UIColor redColor];
//10.label的userInteractionEnabled 是否开启交互 默认为NO
label.userInteractionEnabled = YES;
//11.label的enabled 默认为YES 决定了label的绘制方式 为NO时文本变暗表示没有激活,此时设置的颜色值都无效
label.enabled = NO;
//12.label的numberOfLines 行数 为0显示多行
label.numberOfLines = 0;
//13.label的adjustsFontSizeToFitWidth 设置字体大小适应label的宽度
label.text = @"我是一个文本内容我是一个文本内容我是一个文本内容我是一个文本内容我是一个文本内容我是一个文本内容我是一个文本内容我是一个文本内容我是一个文本内容我是一个文本内容我是一个文本内容我是一个文本内容我是一个文本内容我是一个文本内容我是一个文本内容我是一个文本内容我是一个文本内容我是一个文本内容我是一个文本内容我是一个文本内容我是一个文本内容我是一个文本内容我是一个文本内容我是一个文本内容我是一个文本内容我是一个文本内容我是一个文本内容我是一个文本内容";
// label.adjustsFontSizeToFitWidth = YES;
//14.label的baselineAdjustment 控制文本基线的行为
// label.baselineAdjustment = UIBaselineAdjustmentNone;
// label.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
/*
UIBaselineAdjustmentAlignBaselines = 0, // 默认,文本最上端与中线对齐
UIBaselineAdjustmentAlignCenters, // 文本中线与label中线对齐
UIBaselineAdjustmentNone, // 文本最低端与label中线对齐
*/
//15.label的minimumScaleFactor 设置最小的收缩比,如果label的宽度小于文字长度,文字要收缩,收缩超过比例后,停止收缩 默认值是0
label.minimumScaleFactor = 0.5;
//16.label的allowsDefaultTighteningForTruncation
//17.label的adjustsLetterSpacingToFitWidth 改变字母之间的间距来适应Label的大小
// label.adjustsFontSizeToFitWidth = YES;
//18.label随字体多行后的高度
CGRect bounds = CGRectMake(50, 100, 300, 200);
CGRect heightLabel = [label textRectForBounds:bounds limitedToNumberOfLines:5];
NSLog(@"%f",heightLabel.size.height);
label.frame = CGRectMake(50, 100, heightLabel.size.width, heightLabel.size.height);
//19.label根据字数多少来实现自动适应label的高度
CGSize size = [label.text boundingRectWithSize:CGSizeMake(300, MAXFLOAT)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{
NSFontAttributeName:label.font
}
context:nil].size;
label.frame = CGRectMake(50, 100, size.width, size.height);
//20.label添加边框、圆角
label.layer.borderWidth = 1;
label.layer.borderColor = [UIColor redColor].CGColor;
label.layer.cornerRadius = 10;
//21.label的clipsToBounds默认为NO 设置为YES为裁剪超出俯视图范围的子视图部分
label.clipsToBounds = YES;
//22.label的transform 设置label的倾斜度
label.transform = CGAffineTransformMakeRotation(-0.3);
网友评论