UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(10, 100, 300, 100)];
1.设置文字颜色
label.textColor = [UIColor orangeColor];
label2.textColor = [UIColor purpleColor];
lable.textColor = [UIColor colorWithRed:222/255.0 green:59/255.0 blue:17/255.0 alpha:1];
2.设置背景颜色
label.backgroundColor = [UIColor clearColor];
3.设置文字位置
label.textAlignment = UITextAlignmentLeft;
4.设置label的行数 0表示多行,自适应
label.numberOfLines = 2;
5.设置阴影
label.shadowColor = [UIColor redColor];
label.shadowOffset = CGSizeMake(1.0,1.0);
6.设置文字过长时的显示格式
label.lineBreakMode = UILineBreakModeMiddleTruncation;//截去中间
enum {
UILineBreakModeWordWrap = 0,
UILineBreakModeCharacterWrap,
UILineBreakModeClip,//截去多余部分
UILineBreakModeHeadTruncation,//截去头部
UILineBreakModeTailTruncation,//截去尾部
UILineBreakModeMiddleTruncation,//截去中间
} UILineBreakMode;
7.导入字体包设置自己的字体
在网上下载好字体文件,拖拽的到工程中,选好target
在plist中加入一行,写入字体文件打开后的名字
lable.font = [UIFont fontWithName:@"DFGirlW3-B5" size:20];
8.计算字符串的长度
a.确定一个大的容器,width或者height 一定,另一个可变,变化的变量一定要大
b.确定计算font
c.boundingRectWithSize
NSString * str = @"The NSString class declares the programmatic interface for an object that manages immutable strings.";
UIFont * font = [UIFont fontWithName:@"DFGirlW3-B5" size:20];
NSDictionary * attrDic = @{NSFontAttributeName:font};
CGSize bigsize = CGSizeMake(300, 3000);
CGSize realsize = [str boundingRectWithSize:bigsize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrDic context:nil].size;
UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(10, 100, realsize.width, realsize.height)];
label.text = str;
label.backgroundColor =[UIColor blackColor];
label.font = [UIFont fontWithName:@"DFGirlW3-B5" size:20];
label.textColor = [UIColor colorWithRed:222/255.0 green:59/255.0 blue:17/255.0 alpha:1];
label.textAlignment =NSTextAlignmentLeft;
label.numberOfLines = 0;
label.lineBreakMode = NSLineBreakByWordWrapping;