我有一个字符串,它是一个包含空格的随机数字序列
NSString *myString;
我把它打印到标签上
_myLabel.text = myString;
我想让它居中,所以我用
myLabel.textAlignment = NSTextAlignmentCenter;
但它不能正常工作。居中时,它会忽略任何尾随零。但是尾随的零也是字符!我能让它识别它们吗?
最佳答案
文本在标签上的工作方式是 - 它由标准格式信息格式化 - 如修剪等。如果你不希望 ios 控制它。您可以以编程方式设置 attributedText。这不会通过格式化。这是苹果的文档
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 thefont
,textColor
, and other style-related properties so that they reflect the style information starting at location 0 in the attributed string.
关于ios - 带有尾随空格的 UILabel 不能正确居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35807325/