2

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!

L3M0L
  • 429
  • 8
  • 23
  • how can you tell that the whitespace is removed? – danh Sep 04 '16 at 14:31
  • @danh the text is being written right on the edge of the label, there is no whitespace between the right edge of the label and the text. And if something I'm not itnerested in padding ;) I need a whitespace there – L3M0L Sep 04 '16 at 15:08
  • I think paragraph style provides tailIndent. Did you try that? – danh Sep 04 '16 at 15:20
  • @danh nope! I will try it out and tell You if it did work or not ;) – L3M0L Sep 04 '16 at 15:28
  • @L3M0L you can remove the last character of string because it is white space. – Saurabh Jain Sep 05 '16 at 07:08
  • @danh the tailIndent does not work (or I'm using it wrong) Any other idead? – L3M0L Sep 05 '16 at 07:56
  • @SaurabhJain I WANT the whitespace at the end of the sentence but it's removed automatically. Don't know why :( – L3M0L Sep 05 '16 at 07:57
  • @L3M0L due to text alignment right it remove. – Saurabh Jain Sep 05 '16 at 08:37
  • @SaurabhJain and? Some new ideas how to overcome this problem? – L3M0L Sep 05 '16 at 09:17
  • @L3M0L for white space you can try to change the constraint so that show some space at end of text. – Saurabh Jain Sep 05 '16 at 09:24
  • @SaurabhJain what do You mean? Can You give me an example? – L3M0L Sep 05 '16 at 09:30
  • @L3M0L self.attributedText is part storyboard? – Saurabh Jain Sep 05 '16 at 09:44
  • @SaurabhJain yes it is – L3M0L Sep 05 '16 at 10:23
  • I'm sorry that indent didn't work. A common approach is to arrange views so it looks like there's an indent, as described here: http://stackoverflow.com/a/6363621/294949. But I like the indent idea better. Perplexed why it doesn't work. Can you post code that attempts it, along with an image of it working wrong? – danh Sep 05 '16 at 18:23
  • @danh actually I don't know to what value should I set the indent. If I set it to positive value my text disappears from the view when I set to negative the same happens :/ I can't post my code now because I'm at home but tomorrow I will. Here I drew what my problem is with the attributed string: https://i.gyazo.com/f3e85d3ad32c0e18e6365390312fe443.png – L3M0L Sep 05 '16 at 18:59
  • tailIndent should be a small positive value. – danh Sep 06 '16 at 19:39
  • @danh I edited my question. Can You take a look at it? Why does the text move the whole way from right to left making the text to disappear? How should I adjust the indent for only 1 whitespace? Or maybe is there an other solution – L3M0L Sep 07 '16 at 06:51
  • Hopefully the answer is what you need. Surprised that you didn't find the same while experimenting with different values. – danh Sep 07 '16 at 12:07

1 Answers1

7

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.

danh
  • 62,181
  • 10
  • 95
  • 136
  • One question, is there a way to get the tailIndent value programmatically? Because now there is a -18.0 hard coded (in my case it's -8.0). I want to get the value for whitespace so there is no hardcoded value. – L3M0L Sep 07 '16 at 13:51
  • Please see edit and let me know if I understand your followup question correctly – danh Sep 07 '16 at 14:04
  • No, thats not what I ment, I want to know if there is a way to get the tailindent value for whitespace programatically. I don't know something like: `paragraphStyle.tailIndent = - getValueForWhitespace();` So the value will be for example `-4.34` You know what I mean? That no matter what device, no matter what font it will always give me the correct Indent, because a hardcoded -18.0 is bad – L3M0L Sep 09 '16 at 07:18
  • 1
    i think I understand now, you want to compute the size of a particular sequence of white space characters? Yes, see NSString sizewithattributes. Create a string with the spaces you want and call sizewithattributes, passing the attributes dictionary. – danh Sep 09 '16 at 10:39