如何以编程方式使 iOS 文本标签居中对齐?

发布于 2024-11-01 22:18:46 字数 26 浏览 3 评论 0原文

我想设置文本标签的对齐方式,该怎么做?

I want to set the alignment of a text label, how can I do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

迷乱花海 2024-11-08 22:18:46

我认为这些答案对您有帮助。正确的方法是:

yourLabelName.textAlignment = NSTextAlignmentCenter;

有关更多文档,您可以阅读以下内容:
https://developer.apple.com/documentation/uikit/uilabel

在 Swift 中:-

yourLabelName.textAlignment = .center

这里.centerNSTextAlignment.center

I think there are the answers who helped you out. The correct way to do this is:

yourLabelName.textAlignment = NSTextAlignmentCenter;

for more documentation you can read this:
https://developer.apple.com/documentation/uikit/uilabel

In Swift :-

yourLabelName.textAlignment = .center

Here .center is NSTextAlignment.center

So要识趣 2024-11-08 22:18:46

在这里,

yourLabel.textAlignment = UITextAlignmentCenter

编辑

如果您的目标是 iOS6 以上,请使用 NSTextAlignmentCenter,因为 UITextAlignmentCenter 已被弃用

希望它有帮助。

Here you are,

yourLabel.textAlignment = UITextAlignmentCenter

EDIT

if you target above iOS6 use NSTextAlignmentCenter as UITextAlignmentCenter is depreciated

Hope it helps.

暮光沉寂 2024-11-08 22:18:46

从 iOS 6.0 开始,这一情况已发生变化,UITextAlignment 已被弃用。现在正确的方法是:

yourLabel.textAlignment = NSTextAlignmentCenter;

这是 NSTextAlignment 枚举,它提供了文本对齐选项:

Objective-C:

enum {
   NSTextAlignmentLeft      = 0,
   NSTextAlignmentCenter    = 1,
   NSTextAlignmentRight     = 2,
   NSTextAlignmentJustified = 3,
   NSTextAlignmentNatural   = 4,
};
typedef NSInteger NSTextAlignment;

Swift:

enum NSTextAlignment : Int {
    case Left
    case Center
    case Right
    case Justified
    case Natural
}

来源

This has changed as of iOS 6.0, UITextAlignment has been deprecated. The correct way to do this now is:

yourLabel.textAlignment = NSTextAlignmentCenter;

Here is the NSTextAlignment enumerable that gives the options for text alignment:

Objective-C:

enum {
   NSTextAlignmentLeft      = 0,
   NSTextAlignmentCenter    = 1,
   NSTextAlignmentRight     = 2,
   NSTextAlignmentJustified = 3,
   NSTextAlignmentNatural   = 4,
};
typedef NSInteger NSTextAlignment;

Swift:

enum NSTextAlignment : Int {
    case Left
    case Center
    case Right
    case Justified
    case Natural
}

Source

你爱我像她 2024-11-08 22:18:46
label.textAlignment = NSTextAlignmentCenter;

请参阅 UILabel 文档

label.textAlignment = NSTextAlignmentCenter;

see UILabel documentation

旧夏天 2024-11-08 22:18:46

如果你有一个多行 UILabel 你应该使用 NSMutableParagraphStyle

   yourLabelName.numberOfLines = 0
   let paragraphStyle = NSMutableParagraphStyle()
   paragraphStyle.alignment = .Center

   let attributes : [String : AnyObject] = [NSFontAttributeName : UIFont(name: "HelveticaNeue", size: 15)!, NSParagraphStyleAttributeName: paragraphStyle]

   let attributedText = NSAttributedString.init(string: subTitleText, attributes: attributes)
   yourLabelName.attributedText = attributedText

If you have a multiline UILabel you should use a NSMutableParagraphStyle

   yourLabelName.numberOfLines = 0
   let paragraphStyle = NSMutableParagraphStyle()
   paragraphStyle.alignment = .Center

   let attributes : [String : AnyObject] = [NSFontAttributeName : UIFont(name: "HelveticaNeue", size: 15)!, NSParagraphStyleAttributeName: paragraphStyle]

   let attributedText = NSAttributedString.init(string: subTitleText, attributes: attributes)
   yourLabelName.attributedText = attributedText
谈情不如逗狗 2024-11-08 22:18:46

在 Swift 3 及更高版本中应该是

yourlabel.textAlignment = NSTextAlignment.center

In Swift 3 and beyond it should be

yourlabel.textAlignment = NSTextAlignment.center
帝王念 2024-11-08 22:18:46

在斯威夫特4:

 let paragraphStyle = NSMutableParagraphStyle()
 paragraphStyle.alignment = NSTextAlignment.center

 // in Swift 4.2
 let attributedString = NSMutableAttributedString(string: "Your String", attributes:[NSAttributedString.Key.paragraphStyle:paragraphStyle])

 // in Swift 4.1--
 let attributedString = NSMutableAttributedString(string: "Your String", attributes: [NSAttributedStringKey.paragraphStyle:paragraphStyle])

 let yourLabel = UILabel()
 yourLabel.attributedText = attributedString

in swift 4:

 let paragraphStyle = NSMutableParagraphStyle()
 paragraphStyle.alignment = NSTextAlignment.center

 // in Swift 4.2
 let attributedString = NSMutableAttributedString(string: "Your String", attributes:[NSAttributedString.Key.paragraphStyle:paragraphStyle])

 // in Swift 4.1--
 let attributedString = NSMutableAttributedString(string: "Your String", attributes: [NSAttributedStringKey.paragraphStyle:paragraphStyle])

 let yourLabel = UILabel()
 yourLabel.attributedText = attributedString
记忆之渊 2024-11-08 22:18:46
Yourlabel.textAlignment = UITextAlignmentCenter;
Yourlabel.textAlignment = UITextAlignmentCenter;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文