简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM