Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IOS Multiple right and left align on same line

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

like image 501
user3433920 Avatar asked Mar 24 '14 18:03

user3433920


2 Answers

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
like image 135
Leszek Szary Avatar answered Nov 06 '22 18:11

Leszek Szary


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;
like image 2
Surender Rathore Avatar answered Nov 06 '22 20:11

Surender Rathore