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
!