我想設置文本標籤的對齊方式,我該怎麼做?如何以編程方式爲iOS中的文本標籤對齊?
回答
我認爲有幫助你的答案。要做到這一點,正確的做法是:如果你有一個多的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
label.textAlignment = NSTextAlignmentCenter;
給你,
yourLabel.textAlignment = UITextAlignmentCenter
編輯
,如果你的目標上面iOS6的使用NSTextAlignmentCenter
作爲UITextAlignmentCenter
折舊
希望它有幫助。
這已改變爲iOS 6.0,UITextAlignment has been deprecated。現在這樣做的正確的方法是:
yourLabel.textAlignment = NSTextAlignmentCenter;
這裏是NSTextAlignment枚舉,讓文本對齊選項:
的Objective-C:
enum {
NSTextAlignmentLeft = 0,
NSTextAlignmentCenter = 1,
NSTextAlignmentRight = 2,
NSTextAlignmentJustified = 3,
NSTextAlignmentNatural = 4,
};
typedef NSInteger NSTextAlignment;
斯威夫特:
enum NSTextAlignment : Int {
case Left
case Center
case Right
case Justified
case Natural
}
@nari請接受此答案。是最新的 – mm24 2015-01-28 11:47:35
Yourlabel.textAlignment = UITextAlignmentCenter;
此答案已發佈 – 2013-10-31 13:24:08
'UITextAlignmentCenter'已棄用。改爲使用'NSTextAlignmentCenter',如下所示。 – cloudrave 2014-01-12 01:42:27
:
yourLabelName.textAlignment = NSTextAlignmentCenter;
更多的文檔,你可以閱讀在Swift 3及以後,它應該是
yourlabel.textAlignment = NSTextAlignment.center
- 1. 如何以編程方式對齊和移動兩個標籤?
- 2. 以編程方式創建的標籤不會右對齊
- 3. 如何以編程方式設置UIButton的垂直對齊 - iOS
- 4. 如何以對角方式對齊同一行上的標籤和文本框?
- 5. GTK + 3 CSS:如何指定標籤的文本對齊方式?
- 6. 如何以編程方式在QTableView的列上設置文本對齊方式?
- 7. 如何設置標籤文本對齊頂部iOS中的UITableViewCell
- 8. 如何以編程方式刪除標籤/按鈕/文本框?
- 9. 如何以編程方式對齊iOS應用程序中的按鈕?
- 10. 以編程方式選擇標籤自定義標籤欄ios
- 11. swift:標籤和按鈕文本的中心對齊方式
- 12. 以正確的方式對齊圖像中的標籤文本-RefGdx
- 13. 如何將文本對齊到標籤文本的中心?
- 14. 標籤文本對齊
- 15. ASP.NET標籤文本對齊
- 16. 在div標籤中設置文本對齊方式
- 17. 以編程方式對齊視圖
- 18. TextureView以編程方式對齊父權
- 19. iOS以編程方式中心對象
- 20. 如何在div標記中調整文本的對齊方式
- 21. Java標籤中的文本對齊
- 22. 垂直對齊標籤中的文本
- 23. 對齊火花標籤中的文本
- 24. 如何在iphone中以編程方式添加標籤和文本框
- 25. 以編程方式更改標籤方向,編程方式
- 26. UITableViewCell中的文本標籤對齊頂部在IOS
- 27. 左對齊標籤 - iOS
- 28. 如何在Android中的標籤欄中居中對齊文本
- 29. ASP.NET標籤的對齊方式
- 30. wx.StaticBox標籤的對齊方式
在ios 6中顯示 – khatz0406 2013-11-19 10:40:32