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]];
@"...";
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.
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.
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:
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.
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 "
take a look at this
the solution is add trailing visible charaters and set them transparent.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With