簡體   English   中英

在 iOS 上使用 NSMutableAttributedString 具有相同標簽的多個文本對齊

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

我想使用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,};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM