0

我想使用为我的标签设置文本方向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)];
4

1 回答 1

1

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

 NSMutableParagraphStyle *style  = [[NSMutableParagraphStyle alloc] init];
        style.alignment = alignment;// type NSTextAlignment
        NSDictionary *attributtes = @{NSParagraphStyleAttributeName : style,};
于 2014-09-24T12:19:33.340 回答