How to set the title of UIButton as left alignment?

后端 未结 12 1981
感动是毒
感动是毒 2020-11-27 08:34

I need to display the email address from left side of a UIButton, but it is being positioned in the centre.

Is there any way to set the alignment to the

相关标签:
12条回答
  • 2020-11-27 09:27

    In Swift 3+:

    button.contentHorizontalAlignment = .left
    
    0 讨论(0)
  • 2020-11-27 09:28

    in xcode 8.2.1 in the interface builder it moves to:

    0 讨论(0)
  • 2020-11-27 09:30

    In Swift 5.0 and Xcode 10.2

    You have two ways to approaches

    1) Direct approach

    btn.contentHorizontalAlignment = .left
    

    2) SharedClass example (write once and use every ware)

    This is your shared class(like this you access all components properties)

    import UIKit
    
    class SharedClass: NSObject {
    
        static let sharedInstance = SharedClass()
    
        private override init() {
    
        }
    }
    
    //UIButton extension
    extension UIButton {
        func btnProperties() {
            contentHorizontalAlignment = .left
        }
    }
    

    In your ViewController call like this

    button.btnProperties()//This is your button
    
    0 讨论(0)
  • 2020-11-27 09:32

    Try

    button.semanticContentAttribute = UISemanticContentAttributeForceRightToLeft;
    
    0 讨论(0)
  • 2020-11-27 09:38

    Here is explained how to do it and why it works so: http://cocoathings.blogspot.com/2013/03/how-to-make-uibutton-text-left-or-right.html

    0 讨论(0)
  • 2020-11-27 09:39
    UIButton *btn;
    btn.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
    btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    
    0 讨论(0)
提交回复
热议问题