iOS 设置多行文字的某一行文字的对齐属性

如,设计稿如下
请添加图片描述

整体的文字是居左对齐的,但是最后一行文字是居右对齐的

主要代码: 使用 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];
        

猜你喜欢

转载自blog.csdn.net/LIUXIAOXIAOBO/article/details/130497022