【问题标题】:IOS Multiple right and left align on same lineIOS多个左右对齐在同一行
【发布时间】:2014-05-02 06:42:49
【问题描述】:

我想写在我的 tableview 单元格 detailTextLabel 的同一行。

例如 Left string right string.

我正在这样做:

NSMutableAttributedString *descriptionAttribute = [[NSMutableAttributedString alloc] initWithString:descriptionString];
NSMutableAttributedString *dateAttribute = [[NSMutableAttributedString alloc] initWithString:finalDate];
NSMutableAttributedString *shareAttribute = [[NSMutableAttributedString alloc] initWithString:@"Share"];

NSMutableParagraphStyle *dateStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[dateStyle setAlignment:NSTextAlignmentLeft];
NSMutableParagraphStyle *shareStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[shareStyle setAlignment:NSTextAlignmentRight];

[dateAttribute addAttribute:NSForegroundColorAttributeName value:[UIColor grayColor] range:NSMakeRange(0, 13)];
[dateAttribute addAttribute:NSParagraphStyleAttributeName value:dateStyle range:NSMakeRange(0, 13)];
[shareAttribute addAttribute:NSForegroundColorAttributeName value:[UIColor grayColor] range:NSMakeRange(0, 8)];
[shareAttribute addAttribute:NSParagraphStyleAttributeName value:shareStyle range:NSMakeRange(0, 5)];

[descriptionAttribute appendAttributedString:[dateAttribute mutableCopy]];
[descriptionAttribute appendAttributedString:[shareAttribute mutableCopy]];

myTableViewcell.detailTextLabel.attributedText = descriptionAttribute;

如果我在日期和共享属性字符串之间添加\n,结果很好。

但我想在同一行上有两个字符串..

一个想法?

谢谢

【问题讨论】:

  • 你可以有两个具有相同属性和不同文本左右对齐的标签
  • 好的,但是由于很多原因,我想把我的字符串写在detailTextLabel中
  • 你是怎么添加的\n你的descriptionString和finaldate是什么
  • 我在“share”字符串之前添加了\n。描述字符串是没有特定样式的简单字符串。最终日期只是一个包含日期的 NSString。
  • 我还是不明白这个问题。你想要这样的“描述 11/12/13 分享”吗

标签: ios uilabel detailtextlabel


【解决方案1】:

最好创建一个带有 2 个标签的自定义单元格,但如果出于某种原因您仍希望将其放在一个标签中,那么下面是一个解决方案。如果您知道行的高度是多少,那么您可以使用paragraphSpacingBefore 来执行以下代码中的操作。请注意,当 UILabel numberOfLines 设置为 1 时它不起作用,因此您必须将 numberOfLines 设置为例如 0:

let text = "left\nright"
let at = NSMutableAttributedString(string: text)
let p1 = NSMutableParagraphStyle()
let p2 = NSMutableParagraphStyle()
p1.alignment = .left
p2.alignment = .right
p2.paragraphSpacingBefore = -label.font.lineHeight
at.addAttribute(.paragraphStyle, value: p1, range: NSRange(location: 0, length: 4))
at.addAttribute(.paragraphStyle, value: p2, range: NSRange(location: 4, length: 6))
label.numberOfLines = 0
label.attributedText = at

【讨论】:

    【解决方案2】:

    可以间接使用 iOS 上不可用的 NSTextTable。

    如果你创建像这样的 html 表:

    <table width="100%">
      <tr>
        <td align="left">Left string</td>
        <td align="right">Right string</td>
      </tr>
    </table>
    

    然后像这样将其转换为属性字符串:

    extension NSAttributedString {
      convenience init?(html: String) {
        guard let data = html.data(using: .utf8) else {
            return nil
        }
    
        try? self.init(data: data, options: [
            NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
            NSCharacterEncodingDocumentAttribute: String.Encoding.utf8.rawValue
        ], documentAttributes: nil)
      }
    }
    

    你会得到你所需要的,如果你检查那个属性字符串中的段落属性 - 你会在那里看到 NSTextTable。

    【讨论】:

      【解决方案3】:

      试试这个

      在日期之后添加了一个 NSKernAttributeName

      NSMutableAttributedString *descriptionAttribute = [[NSMutableAttributedString alloc] initWithString:@"hello how are you"];
          NSMutableAttributedString *dateAttribute = [[NSMutableAttributedString alloc] initWithString:@"\nfinal datetime"];
          NSMutableAttributedString *shareAttribute = [[NSMutableAttributedString alloc] initWithString:@"Share"];
      
      
      
          NSMutableParagraphStyle *dateStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
      
          [dateStyle setAlignment:NSTextAlignmentLeft];
          NSMutableParagraphStyle *shareStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
          [shareStyle setAlignment:NSTextAlignmentRight];
      
          [dateAttribute addAttribute:NSForegroundColorAttributeName value:[UIColor grayColor] range:NSMakeRange(0, dateAttribute.length)];
          [dateAttribute addAttribute:NSParagraphStyleAttributeName value:dateStyle range:NSMakeRange(0, 13)];
          [dateAttribute addAttribute:NSKernAttributeName value:@170 range:NSMakeRange(dateAttribute.length-1, 1)];
      
          [shareAttribute addAttribute:NSForegroundColorAttributeName value:[UIColor grayColor] range:NSMakeRange(0, 2)];
          [shareAttribute addAttribute:NSParagraphStyleAttributeName value:shareStyle range:NSMakeRange(0, 5)];
      
      
      
          [descriptionAttribute appendAttributedString:[dateAttribute mutableCopy]];
      
          [descriptionAttribute appendAttributedString:[shareAttribute mutableCopy]];
      
          theCell.detailTextLabel.attributedText = descriptionAttribute;
      

      【讨论】:

      • 超出范围。 NSRangeException
      • 对我来说添加NSKernAttributeName 似乎等于在第一部分之后添加空格。它不考虑 ParagraphStyle 的对齐方式。事实上,代码执行相同的事件,但不设置对齐方式。
      • @EugeneDubinin 是的,它只是添加空间,但问题是您可以动态计算所需的空间并添加它。与其编写我们自己的代码在第一个字符串之后添加空格,我们可以使用这个权利,我相信这是一件好事。 :)
      • 不起作用,因为没有右对齐,只添加了空格。
      猜你喜欢
      • 2015-08-13
      • 2013-05-22
      • 2015-05-07
      • 2012-05-03
      • 1970-01-01
      • 2011-04-14
      • 1970-01-01
      • 2019-01-21
      • 1970-01-01
      相关资源
      最近更新 更多