Home > Software design >  Center alignment UIButton xCODE programmatically
Center alignment UIButton xCODE programmatically

Time:05-10

I’m trying to center the text of UIButton programmatically.

This is what I want the UIButton to look like (I used the interface builder):

UIbutton

This is what the UIButton looks like after I set it programmatically:

UIbutton

And this is what the UIButton looks like after I set it programmatically and tried to center the text like in the first picture:

UIButton

This is the code that I used to try to center it:

previousPageButton.titleLabel?.numberOfLines = 0

previousPageButton.titleLabel?.textAlignment = .center

CodePudding user response:

Try adding:

previousPageButton.titleLabel?.lineBreakMode = .byWordWrapping

CodePudding user response:

The problem was I had set up "attributed" to the title in interface builder. I just had to set it to "plain" and it's working now.

  • Related