ios - 带有尾随空格的 UILabel 不能正确居中

标签 ios objective-c uilabel

我有一个字符串,它是一个包含空格的随机数字序列

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 the font, 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/

相关文章:

ios - 为什么 UILabel.text 会导致 “fatal error: unexpectedly found nil while unwrapping an Optional value” ?

swift - AVCaptureVideo 不显示标签

ios - 需要轻量级迁移吗?

ios - 在iOS8中获取contactID时出错-崩溃

ios - 带有 MagicalRecord 的 JSONModel

ios - 我有一个 TableView 和五个数据源

ios - 如何在您的程序中连接到 iTunes 资料库。这里面用到了什么api或者类

ios - 在 UIAlertController 中更改标题颜色

ios - 似乎无法删除 UIView?

ios - translatesAutoresizingMaskIntoConstraints 和 UILabel