【发布时间】:2020-02-13 14:08:43
【问题描述】:
我想使用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)];
【问题讨论】:
-
NSWritingDirectionAttributeName 需要两个参数,如果我没记错的话。检查文档。
-
您是否考虑过仅使用单独的时间标签和单独的消息标签?
标签: ios text-alignment nsmutableattributedstring