您如何以编程方式将 iOS 文本标签的对齐方式居中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5686631/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you programmatically center the alignment of a text label for iOS?
提问by nari
I want to set the alignment of a text label, how can I do that?
我想设置文本标签的对齐方式,我该怎么做?
回答by Shivaay
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
有关更多文档,您可以阅读:https: //developer.apple.com/documentation/uikit/uilabel
In Swift :-
在斯威夫特:-
yourLabelName.textAlignment = .center
Here .centeris NSTextAlignment.center
这.center是NSTextAlignment.center
回答by Janak Nirmal
Here you are,
这个给你,
yourLabel.textAlignment = UITextAlignmentCenter
EDIT
编辑
if you target above iOS6 use NSTextAlignmentCenteras UITextAlignmentCenteris depreciated
如果你的目标上面iOS6的使用NSTextAlignmentCenter作为UITextAlignmentCenter折旧
Hope it helps.
希望能帮助到你。
回答by turbo
This has changed as of iOS 6.0, UITextAlignment has been deprecated. The correct way to do this now is:
从 iOS 6.0 开始,这已经改变了,UITextAlignment 已被弃用。现在执行此操作的正确方法是:
yourLabel.textAlignment = NSTextAlignmentCenter;
Here is the NSTextAlignment enumerable that gives the options for text alignment:
这是 NSTextAlignment 枚举,它提供了文本对齐的选项:
Objective-C:
目标-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
}
回答by cweinberger
回答by apinho
If you have a multiline UILabel you should use a NSMutableParagraphStyle
如果你有一个多行 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
回答by Heki
In Swift 3 and beyond it should be
在斯威夫特3和超越它应该是
yourlabel.textAlignment = NSTextAlignment.center
回答by Mr. Tann
in swift 4:
快速 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
回答by tv.ashvin
Yourlabel.textAlignment = UITextAlignmentCenter;

