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];
        

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值