简体   繁体   English

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

[英]Multiple text alignment with same label using NSMutableAttributedString on iOS

I want to set Text direction for my label using NSMutableAttributedString .我想使用NSMutableAttributedString为我的标签设置文本方向。 for example, I have chat view, which contains message and time.例如,我有聊天视图,其中包含消息和时间。 I want to set left alignment for message and right alignment for time using UILabel .我想使用UILabel设置消息的左对齐和时间的右对齐。

I have used following code, but it's not working,我使用了以下代码,但它不起作用,

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

if I understood correctly, this should help:如果我理解正确,这应该会有所帮助:

 NSMutableParagraphStyle *style  = [[NSMutableParagraphStyle alloc] init];
        style.alignment = alignment;// type NSTextAlignment
        NSDictionary *attributtes = @{NSParagraphStyleAttributeName : style,};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM