Search code examples
objective-cwhitespacensattributedstringnsmutableattributedstringright-align

NSAttributedString with right alignment removes whitespace at the end


I've created a simple chat app where our messages are on the right side (right alignment) and all other messages are on the left side (left alignment). I'm using NSAttributedString because I heavily modify the text with colors etc. Each message is a UILabel. My problem is that at the end of the message with the right alignment I want to put a whitespace so it looks like this:

"Some example sentence "

and not like this:

"Some example sentece"

and it's removed everytime I put the whitespace there (I also tried with the non-breaking space \u00a0 and I get the same problem (the space is removed) My code for the right alignment looks like this:

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:self.text /*attributes:attrDict*/];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setAlignment:NSTextAlignmentRight];
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [attributedString length])];

later I add some other attributes with colors etc. (nothing that changes the text itself). The text is always with the whitespace at the end like this: "Some example sentece " and at the end I do something like this:

self.attributedText = attributedString;

And... my space is removed. How can I prevent my text from removing the whitespace at the end? I need it there.

EDIT:

if (self.textAlignment == NSTextAlignmentRight) {
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        [paragraphStyle setAlignment:NSTextAlignmentRight];
        [paragraphStyle setTailIndent:0.1];
        [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [attributedString length])];
    }

This is my code for the tailIndent and what it does looks like this. I have a message "test" on the right side of the chat (here it's on the left because I don't know how to right align the text :P) before tailIndent:

test

after tailIndent:

t

So what happens: The text goes from right to left leaving only the last character in this case. And the tailIndent is only 0.1!


Solution

  • I tried a few values myself, and, contrary to the expectation set by the name of the attribute (and the lack of other guidance in the doc), tailIndent must be negative.

    Here's the code (the OP's, basically) without the attribute set:

    NSString *text = @"Am I indented?";
    
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setAlignment:NSTextAlignmentRight];
    // paragraphStyle.tailIndent = -18.0;
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [attributedString length])];
    self.label.attributedText = attributedString;
    

    enter image description here

    Uncomment the line setting tailIndent to a negative value, and you get:

    enter image description here

    EDIT Any of the controlling params should be represented as objects, like an NSNumber representing the indent:

    NSNumber *theIndent = @(-18);
    
    // then, later:
    paragraphStyle.tailIndent = [theIndent intValue];
    

    Only objects, like NSNumbers, may be placed in arrays, dictionaries, core data, etc.