Center text in a UILabel

后端 未结 8 627
醉话见心
醉话见心 2021-02-03 16:39

How do you center the text inside a UILabel?

8条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-03 17:11

    If you have a multiline UILabel you should use a NSMutableParagraphStyle

       label.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)
       label.attributedText = attributedText
    

提交回复
热议问题