UILabel text includes several spaces at the end

In Objective-C, I have a UILabel that says "Download." Every 0.5 seconds a line will get another '.' until there are 3 points, then it will start. The problem I am facing is that my line is centered, and when each point appears, the line jumps. The reason that line skipping occurs is because UILabel continues to trim right spaces.

Here is an example of what is happening at the moment.

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

And that is what I want.

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

Pointing the UILabel to wide and left justified is not an option. This needs to be done with the text.

I found a link, but I still can not figure it out.

What I have now is this.

 [NSString stringWithFormat:@"%-3.3s", [@"" UTF8String]]; [NSString stringWithFormat:@"%-3.3s", [@"." UTF8String]]; [NSString stringWithFormat:@"%-3.3s", [@".." UTF8String]]; @"..."; 
+6
source share
3 answers

How the text on the label works - it is formatted with standard formatting information - for example, cropping, etc. If you do not want ios to control this. You can programmatically set the Text attribute. it does not go through formatting. Here is the apple documentation on this

attributedText The stylized text displayed by the label.

@property (non-atomic, copy) NSAttributedString * attributedText Discussion. By default, this property is zero. Assigning a new value to this property also replaces the value of the text property with the same string data, although without any formatting information. In addition, assigning a new value updates the values ​​in the font, textColor, and other properties associated with the style so that they reflect style information starting at location 0 in the attribute string.

Essentially set the uilabel attribitedtext attribute to "Download"

+4
source

take a look at this

The solution adds trailing visible characters and sets them transparent.

+1
source

Set label.textAlignment = NSTextAlignmentNatural . Starting with ios 9, this is the default value, before that it was NSTextAlignmentLeft.

Now that your text needs to be centered, wrap it in a UIView with all label fields tied to the shell icon. Then center the wrapper instead of the label.

Here is the code:

 UIView* wrapper = [[UIView alloc] init]; wrapper.translatesAutoresizingMaskIntoConstraints = NO; //This sets the wrapper at the center of where you were adding the label previously [wrapper.centerXAnchor constraintEqualToAnchor: superview.centerXAnchor].active = YES; label.translatesAutoresizingMaskIntoConstraints = NO; [label.leadingAnchor constraintEqualToAnchor:wrapper.leadingAnchor].active = YES; [label.trailingAnchor constraintEqualToAnchor:wrapper.trailingAnchor].active = YES; [label.topAnchor constraintEqualToAnchor:wrapper.topAnchor].active = YES; [label.bottomAnchor constraintEqualToAnchor:wrapper.bottomAnchor].active = YES; label.textAlignment = NSTextAlignmentNatural ... 
+1
source

Source: https://habr.com/ru/post/955077/


All Articles