quantum_jim
quantum_jim

Reputation: 218

UILabel with trailing spaces doesn't center properly

I have a string that is a random sequence of numbers including spaces

NSString *myString;

I print that to a label

_myLabel.text = myString;

I wanted it to be centered, so I use

myLabel.textAlignment = NSTextAlignmentCenter;

But it doesn't work properly. When centering, it ignore any trailing zeros. But the trailing zeros are characters too! Can I make it recognize them?

Upvotes: 3

Views: 1042

Answers (1)

typedef
typedef

Reputation: 937

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.

Upvotes: 4

Related Questions