【问题标题】:Multiple text alignment with same label using NSMutableAttributedString on iOS在 iOS 上使用 NSMutableAttributedString 与相同标签的多个文本对齐
【发布时间】:2020-02-13 14:08:43
【问题描述】:

我想使用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)];

【问题讨论】:

  • NSWritingDirectionAttributeName 需要两个参数,如果我没记错的话。检查文档。
  • 您是否考虑过仅使用单独的时间标签和单独的消息标签?

标签: ios text-alignment nsmutableattributedstring


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2011-09-14
    • 2023-02-22
    • 2013-02-23
    • 1970-01-01
    • 2015-10-07
    • 2023-01-13
    • 2013-04-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多