4

I see LOTS of questions and answers about trimming the white space of of the end of a string. My question is the opposite.

I intentionally DO WANT a blank space character at the end of my string and I do NOT want it to be automatically trimmed away when assigned to the text property of a UILabel.

I have tried adding all the unicode variations I could think of (NO-BREAK SPACE /u00a0; PUNCTUATION SPACE /u0008; TAG SPACE, etc) but they all get trimmed.

In my past life programming experience there was always available a "space" character what was not considered a "white space" specifically so it would be immune from auto-trimming.

Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
HirsuteJim
  • 529
  • 4
  • 13

2 Answers2

3

I search for the answer for about a week, i've found that using the unicode "0009" (tab space) works for the UILabel

Ching
  • 31
  • 2
1

Adding tab space didn't help me as I wanted just to add 1 space to the beginning and to the and of the label, so I came up with the solution which doesn't look nice, but is configurable & saved my life:

NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:text attributes:@{NSForegroundColorAttributeName : [UIColor blackColor]}];

[string insertAttributedString:[self emptyAtributedWhitespace] atIndex:0];
[string appendAttributedString:[self emptyAtributedWhitespace]];

label.attributedText = string;

...

- (NSAttributedString *)emptyAtributedWhitespace
{
    // You can put any random string there or how many spaces you want
    return [[NSAttributedString alloc] initWithString:@"_" attributes:@{ NSForegroundColorAttributeName : [UIColor clearColor]}];
}
B.S.
  • 21,660
  • 14
  • 87
  • 109