0

I am trying to create a UILabel where some of the text is aligned to the right and some of the text is aligned to the left. It is similar to the UITableViewCell with the small arrow:

enter image description here

I am trying to do it with NSAttributedString , but can't figure out what is the correct way to tackle this.

Here is some code which isn't working. It is aligned to the right.

NSMutableAttributedString *att = [[NSMutableAttributedString alloc] initWithString:@"Label >"];

        NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
        paragraph.alignment = NSTextAlignmentLeft;

        [att addAttribute:NSParagraphStyleAttributeName value:paragraph range:NSMakeRange(0, @"Label".length)];

        NSMutableParagraphStyle *rightParagraph = [[NSMutableParagraphStyle alloc] init];
        paragraph.alignment = NSTextAlignmentRight;
        [att addAttribute:NSParagraphStyleAttributeName value:rightParagraph range:NSMakeRange(5, 1)];
Avba
  • 14,822
  • 20
  • 92
  • 192
  • 4
    Why not use 2 labels? – Wain Apr 06 '15 at 12:38
  • 1
    Agreed. Use two labels if you can. – Rory McKinnel Apr 06 '15 at 12:41
  • Because the sizes of the labels varies. This case the text is constant but i will need to calculate the sizes of the labels so that they don't overlap. – Avba Apr 06 '15 at 12:46
  • 1
    possible duplicate of [NSMutableAttributedString add different alignments](http://stackoverflow.com/questions/16737503/nsmutableattributedstring-add-different-alignments) – A-Live Apr 06 '15 at 12:56
  • Thanks,I came across that post, but I couldn't get it to work in a clean way because they add \t and \n. I don't know what kind of text i will be getting so it could be a bug in the future – Avba Apr 06 '15 at 12:58
  • I'm sure you can make several labels to not overlap with autolayout. – A-Live Apr 06 '15 at 13:02
  • I am sure I could. I want a solution to the question though :) – Avba Apr 06 '15 at 13:05
  • @AvnerBarr simply put, the approach you are taking cannot be achieved in iOS. You really need to use two labels like everyone has suggested. There is no such thing as left & right alignment within the same label. You have to know the limitations of technology – Sam B Apr 06 '15 at 13:31

3 Answers3

0

You can use NSAttributedString to achieve your requirements, but it will be much better and cleaner approach to use two UILabels instead.

Sanjay Mohnani
  • 5,947
  • 30
  • 46
  • I don't agree. Depends on the case. I have complicated string analysis to perform. This is only a simple example. The question is not about performance. The question is how to implement an attributed string with 2 alignments – Avba Apr 06 '15 at 12:54
0

I did it before with that code, Hope it also working for you.

NSString* alphaString = @“some text”;

NSMutableParagraphStyle* paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.alignment = NSTextAlignmentLeft;

NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc] 
                                               initWithString:alphaString 
                                               attributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                               [UIFont fontWithName:@"HelveticaNeue" size:13],      NSFontAttributeName, 
    paragraphStyle, NSParagraphStyleAttributeName, nil]];

NSString * betaString = @“some other text”;

NSMutableParagraphStyle* paragraphStyle2 = [[NSMutableParagraphStyle alloc] init];
paragraphStyle2.alignment = NSTextAlignmentRight;


[attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:betaString attributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"HelveticaNeue" size:13], NSFontAttributeName, paragraphStyle2, NSParagraphStyleAttributeName, nil]]];

yourLabel.attributedText = attributedString;
Avineet Gupta
  • 586
  • 10
  • 21
0

Use 2 labels.Assign the needed TextAlignment property to them. And after setting label text value, write this line :

[textLabel sizeToFit];

Though sizes of the labels varies it will set to minimum size. and will avoid text overlap.

Vivek Deore
  • 71
  • 10