Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UILabel text include multiple spaces at the end

In Objective-C I have a UILabel that says 'Loading'. Every 0.5 seconds the string will gain another '.' until there are 3 dots, then it starts over. The problem I'm facing is that my string is centered and when each dot appears, the string jumps. The reason the string jumps is because the UILabel keeps trimming the right whitespace.

Here is an example of whats currently happening.

+------------+
|  Loading   |
+------------+
|  Loading.  |
+------------+
| Loading..  |
+------------+
| Loading... |
+------------+

And this is what I want to happen.

+------------+
| Loading    |
+------------+
| Loading.   |
+------------+
| Loading..  |
+------------+
| Loading... |
+------------+

Telling the UILabel to have a greater width and be left aligned is not an option. This needs to be done with the text.

I found this link, but I still cant figure it out.

What I currently have is this.

[NSString stringWithFormat:@"%-3.3s", [@"" UTF8String]];
[NSString stringWithFormat:@"%-3.3s", [@"." UTF8String]];
[NSString stringWithFormat:@"%-3.3s", [@".." UTF8String]];
@"...";
like image 924
cnotethegr8 Avatar asked Oct 02 '13 06:10

cnotethegr8


People also ask

How do I set the vertical-align on a uilabel?

There’s no way to set the vertical-align on a UILabel, but you can get the same effect by changing the label's frame. I've made my labels orange so you can see clearly what's happening. You can see the text now move up of frame view. How about a label with longer text? What should we do.

How to embed uilabel inside a stackview?

Embed Label in StackView and set the following two attributes of StackView in Attribute Inspector: For better results, you can do this StackView > View > UILabel, because when you just embed an UILabel inside a StackView, the StackView resize to fit UILabel to minimum size.

How to make a label with longer text make more than one?

What should we do. f you have a label with longer text that will make more than one line, set numberOfLines to 0 (zero here means an unlimited number of lines). Embed Label in StackView and set the following two attributes of StackView in Attribute Inspector:

How do I make a label use multiple lines of text?

Setting a value of 0 allows the label to use as many lines as necessary to lay out the text within the label’s width. Use the lineBreakMode property to control how the label splits the text into multiple lines, and the truncation behavior associated with the final line. Use Auto Layout to position and optionally size the label.


2 Answers

The way text works on label is - it is formatted by standard formatting information - like trimming etc. If you don't want ios to take control on that. You can programmatically set attributedText. this does not go thru formatting. Here is apple's documentation on that

attributedText The styled text displayed by the label.

@property(nonatomic,copy) NSAttributedString *attributedText Discussion This property is nil by default. Assigning a new value to this property also replaces the value of the text property with the same string data, albeit without any formatting information. In addition, assigning a new a value updates the values in the font, textColor, and other style-related properties so that they reflect the style information starting at location 0 in the attributed string.

Essentially set uilabel's attributedtext to "Loading "

like image 184
Pradeep Mahdevu Avatar answered Oct 16 '22 04:10

Pradeep Mahdevu


take a look at this

the solution is add trailing visible charaters and set them transparent.

like image 21
enzoyang Avatar answered Oct 16 '22 02:10

enzoyang