2014-09-24 12 views
0

我想使用NSMutableAttributedString爲我的標籤設置文本方向。例如,我有聊天視圖,其中包含消息和時間。我想使用UILabel設置郵件的左對齊方式和時間方向的右對齊方式。iPhone sdk:使用NSMutableAttributedString使用相同標籤的多個文本對齊

我用下面的代碼,但它不工作,

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

請幫助。

+0

NSWritingDirectionAttributeName需要兩個參數,如果我記得很清楚。檢查文檔。 – Larme 2014-09-24 12:11:30

+0

您是否考慮過使用單獨的時間標籤和單獨的標籤作爲消息? – AppHandwerker 2014-09-24 12:13:25

回答

1

,如果我理解正確的話,這應該幫助:

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

謝謝,它適用於我。 – 2014-09-26 10:46:44