9

I want to write on the same line of my tableview cell detailTextLabel.

For example Left string right string.

I'm doing this :

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;

If I add a \n between date and share attribute string, the result is good.

But i want to have two string on the same line..

An idea ?

Thanks

user3433920
  • 131
  • 1
  • 6

3 Answers3

10

It would be better to create a custom cell with 2 labels but if for some reason you still want to have it in a single label then below is a solution. If you know what is the height of the line then you can use paragraphSpacingBefore to do something like in code below. Note that it does not work when UILabel numberOfLines is set to 1 so you must set numberOfLines to for example 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
Leszek Szary
  • 9,763
  • 4
  • 55
  • 62
2

try this

added a NSKernAttributeName after date

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;
Surender Rathore
  • 845
  • 8
  • 18
  • Out of bound. NSRangeException – user3433920 Mar 24 '14 at 19:45
  • 2
    Adding `NSKernAttributeName` for me seems equal to adding spaces after the first part. It does not take ParagraphStyle's alignment into account. In fact the code does the same event w/o setting alignments. – Yevhen Dubinin Jun 10 '15 at 16:35
  • @EugeneDubinin Yes it just add the space, but the thing is you can calculate the space required dynamically and add it. Instead of writing our own code to add the spaces after first string, we can use this right, which I believe is a good thing. :) – iamyogish Jun 17 '15 at 08:52
  • 2
    Does not work, because nothing is right aligned, only space is added. – meaning-matters Aug 18 '15 at 21:00
0

It's possible to indirectly use NSTextTable which is unavailable on iOS.

If you create html table like:

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

And then convert it to an attributed string like that way:

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)
  }
}

You will get exactly what you need and if you check paragraph attributes in that attributed string - you will see NSTextTable there.