菜鸟一枚,本文章只为记忆而已
//想让lable可以换行显示,必须先设置显示内容,再设置高度.让lable自适应高度
UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 300, 0)];
NSString *str = @"\n人生若只如初见,\n何事秋风悲画扇。\n等闲变却故人心,\n却道故人心易变。\n骊山语罢清宵半,\n泪雨霖铃终不怨。何如薄幸锦衣郎,比翼连枝当日愿";
NSMutableAttributedString *attSTR = [[NSMutableAttributedString alloc]initWithString:str];
//添加字体和字体范围
[attSTR addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:30.0f] range:NSMakeRange(0, 3)];
//添加文字颜色和范围
[attSTR addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(17, 7)];
//给字符串添加下划线和范围(value :下划线的样式)
[attSTR addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithFloat:1.0] range:NSMakeRange(10, 10)];
//设置段落
NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
//行间距
paragraph.lineSpacing = 10;
//段落间距
paragraph.paragraphSpacing = 30;
//对齐方式
paragraph.alignment = NSTextAlignmentLeft;
//指定段落开始的缩进像素
paragraph.firstLineHeadIndent = 50;
//调整全部文字的缩进像素
paragraph.headIndent = 10;
[attSTR addAttribute:NSParagraphStyleAttributeName value:paragraph range:NSMakeRange(0, [str length])];
lable.attributedText = attSTR;
lable.numberOfLines = 0;
[lable sizeToFit];
[self.view addSubview:lable];
网友评论