如,设计稿如下
整体的文字是居左对齐的,但是最后一行文字是居右对齐的
主要代码: 使用 NSMutableParagraphStyle 的 .alignment = NSTextAlignmentRight 属性设置
代码
descString = [NSString stringWithFormat:@"%@ \n\n %@\n%@",tagString,descString,source];
NSAttributedString *attr = getLineSpaceAttributedString(descString, 5, appFont(15, NO));
NSArray *array = [tagString componentsSeparatedByString:@"/"];
NSString *first = array[0];
NSString *change = array[1];
NSMutableAttributedString *strAtt = [[NSMutableAttributedString alloc] initWithAttributedString:attr];
[strAtt addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"0xffffff"] range:NSMakeRange(0, [tagString length])];
[strAtt addAttribute:NSFontAttributeName value:appFont(13, NO) range:NSMakeRange([first length], [change length]+1)];
///获取最后一行文字的range
NSRange authorRange = [descString rangeOfString:source];
NSMutableParagraphStyle *descStyle = [[NSMutableParagraphStyle alloc]init];
//设置居右对齐的样式
descStyle.alignment = NSTextAlignmentRight;
/// 设置最后一样的样式
[strAtt addAttribute:NSParagraphStyleAttributeName value:descStyle range:authorRange];