在iOS上使用NSMutableAttributedString具有相同标签的多个文本对齐方式

问题描述 投票:0回答:1

我想使用NSMutableAttributedString设置标签的文本方向。例如,我有聊天视图,其中包含消息和时间。我想使用UILabel设置消息的左对齐和时间的右对齐。

我已经使用了以下代码,但是它不起作用,

 NSString *Time = [Functions stringFromGivenDate:msg.time withFormate:@"hh:mm a"];
 NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@\n%@",msg.text,Time]];
 NSDictionary *attrDictionary = @{NSWritingDirectionAttributeName:@[@(NSTextWritingDirectionOverride)]};

[str addAttributes:attrDictionary range:NSMakeRange(msg.text.length+1, Time.length)];
ios text-alignment nsmutableattributedstring
1个回答
1
投票

如果我理解正确,这应该会有所帮助:]

 NSMutableParagraphStyle *style  = [[NSMutableParagraphStyle alloc] init];
        style.alignment = alignment;// type NSTextAlignment
        NSDictionary *attributtes = @{NSParagraphStyleAttributeName : style,};
© www.soinside.com 2019 - 2024. All rights reserved.