IOS多个左右对齐在同一行

时间:2014-03-24 18:12:40

标签: ios uilabel detailtextlabel

我想写在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,结果是好的。

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

一个想法?

由于

3 个答案:

答案 0 :(得分:2)

最好使用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

答案 1 :(得分:1)

试试这个

在日期

之后添加了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;

答案 2 :(得分:0)

可以间接使用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。