Is there a "space" that is NOT trimmed from the end of the UILabel?

I see a lot of questions and answers about trimming the end-of-line space. My question is the opposite.

I deliberately DO WANT the space character at the end of my line, and I DO NOT want it to automatically truncate when assigned to a property text



I tried to add all the unicode variations I could think of (NO-BREAK SPACE / u00a0; PUNCTUATION SPACE / u0008; TAG SPACE, etc.), but they are all cut off.

In my experience of past life programs, there has always been a "cosmic" character that was not considered "white space" on purpose, so it would be immune to automatic cropping.


source to share

2 answers

I've been looking for an answer for a week, I found that using unicode "0009" (tab) works for UILabel



Adding the tabs area didn't work for me as I wanted to just add 1 space to the start and to the label, so I came up with a solution that doesn't look pretty but is customizable and life-saving:

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]}];




All Articles