UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
// 使用斜杠n是字體顯示為兩行
label.text = @"隨堂測驗\n(學習完成后,開始測試)";
label.font = [UIFont systemFontOfSize:10];
// 使用換行符號切莫忘記寫這句
label.numberOfLines = 0;
label.textColor = [UIColor hexChangeFloat:@"A5A5A5"];
// 利用屬性字符串修改字體大小 顏色
NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc] initWithString:label.text];
[AttributedStr addAttribute:NSForegroundColorAttributeName
value:[HuConfigration uiColorFromString:@"#D3D3D3"]
range:NSMakeRange(0, 4)];
[AttributedStr addAttribute:NSFontAttributeName
value:[UIFont customFontSize:18]
range:NSMakeRange(0 , 4)];
// 修改行間距
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
[paragraphStyle setLineSpacing:4];
[AttributedStr addAttribute:NSParagraphStyleAttributeName
value:paragraphStyle
range:NSMakeRange(0, [label.text length])];
label.attributedText = AttributedStr;
label.textAlignment = NSTextAlignmentCenter;
[self addSubview:label];
效果圖: