我想使用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)];
如果我理解正确,这应该会有所帮助:]
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
style.alignment = alignment;// type NSTextAlignment
NSDictionary *attributtes = @{NSParagraphStyleAttributeName : style,};