How to Make Uibutton's Text Alignment Center? Using Ib

How to make UIButton's text alignment center? Using IB

Solution1

You can set the key path in the storyboard

Set the text to your multiline title e.g. hello + multiline

You need to press + to move text to next line.

Sample Image

Then add the key path

titleLabel.textAlignment as Number and value 1, 1 means NSTextAlignmentCenter
titleLabel.numberOfLines as Number and value 0, 0 means any number of lines

Sample Image

This will not be reflected on IB/Xcode, but will be in centre at run time (device/simulator)

If you want to see the changes on Xcode you need to do the following: (remember you can skip these steps)

  1. Subclass the UIButton to make the button designable:

    import UIKit
    @IBDesignable class UIDesignableButton: UIButton {}

  2. Assign this designable subclass to the buttons you're modifying:

Showing how to change the class of the button using Interface Builder


  1. Iff done right, you will see the visual update in IB when the Designables state is "Up to date" (which can take several seconds):

Comparing the designable and default button in Interface Builder



Solution2

If you want to write the code, then do the long process

1.Create IBOutlet for button

2.Write code in viewDidLoad

btn.titleLabel.textAlignment = .Center
btn.titleLabel.numberOfLines = 0


Solution3

In newer version of xcode (mine is xcode 6.1) we have property attributed title

Select Attributed then select the text and press centre option below

P.S. The text was not coming multiline for that I have to set the

btn.titleLabel.numberOfLines = 0

Sample Image

Center Multi-Line Text on UIButton using IB

You can't set the text to be centered in your nib. But you can change the alignment in your code:

- (void)viewDidLoad {
[super viewDidLoad];

self.myButton.titleLabel.textAlignment = UITextAlignmentCenter;
}

set uibutton text alignment storyboard

How to make UIButton's text alignment center? Using IB

This post shows how to do it in storyboard.

Sample Image

Text is not center Aligned in Rounded UI Button - ObjectiveC

Set UIButton contentEdgeInsets will make your button title adjustments to top, left, bottom, right.

floatingButton.contentEdgeInsets = UIEdgeInsetsMake(top, left, bottom, right);

In your case, it will make title to center

floatingButton.contentEdgeInsets = UIEdgeInsetsMake(0, 2, 3, 0);

Note: as per your given code kFloatingButtonSize is not mentioned, i checked with kFloatingButtonSize = 50

Sample Image

UIButton: how to center an image and a text using imageEdgeInsets and titleEdgeInsets?

For what it's worth, here's a general solution to positioning the image centered above the text without using any magic numbers. Note that the following code is outdated and you should probably use one of the updated versions below:

// the space between the image and text
CGFloat spacing = 6.0;

// lower the text and push it left so it appears centered
// below the image
CGSize imageSize = button.imageView.frame.size;
button.titleEdgeInsets = UIEdgeInsetsMake(
0.0, - imageSize.width, - (imageSize.height + spacing), 0.0);

// raise the image and push it right so it appears centered
// above the text
CGSize titleSize = button.titleLabel.frame.size;
button.imageEdgeInsets = UIEdgeInsetsMake(
- (titleSize.height + spacing), 0.0, 0.0, - titleSize.width);

The following version contains changes to support iOS 7+ that have been recommended in comments below. I haven't tested this code myself, so I'm not sure how well it works or whether it would break if used under previous versions of iOS.

// the space between the image and text
CGFloat spacing = 6.0;

// lower the text and push it left so it appears centered
// below the image
CGSize imageSize = button.imageView.image.size;
button.titleEdgeInsets = UIEdgeInsetsMake(
0.0, - imageSize.width, - (imageSize.height + spacing), 0.0);

// raise the image and push it right so it appears centered
// above the text
CGSize titleSize = [button.titleLabel.text sizeWithAttributes:@{NSFontAttributeName: button.titleLabel.font}];
button.imageEdgeInsets = UIEdgeInsetsMake(
- (titleSize.height + spacing), 0.0, 0.0, - titleSize.width);

// increase the content height to avoid clipping
CGFloat edgeOffset = fabsf(titleSize.height - imageSize.height) / 2.0;
button.contentEdgeInsets = UIEdgeInsetsMake(edgeOffset, 0.0, edgeOffset, 0.0);

Swift 5.0 version

extension UIButton {
func alignVertical(spacing: CGFloat = 6.0) {
guard let imageSize = imageView?.image?.size,
let text = titleLabel?.text,
let font = titleLabel?.font
else { return }

titleEdgeInsets = UIEdgeInsets(
top: 0.0,
left: -imageSize.width,
bottom: -(imageSize.height + spacing),
right: 0.0
)

let titleSize = text.size(withAttributes: [.font: font])
imageEdgeInsets = UIEdgeInsets(
top: -(titleSize.height + spacing),
left: 0.0,
bottom: 0.0,
right: -titleSize.width
)

let edgeOffset = abs(titleSize.height - imageSize.height) / 2.0
contentEdgeInsets = UIEdgeInsets(
top: edgeOffset,
left: 0.0,
bottom: edgeOffset,
right: 0.0
)
}
}

Change the color and alignment of a UIButton's text

Set button title color & Alignment ...

[aboutButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

[aboutButton.titleLabel setTextAlignment:UITextAlignmentCenter];

or

aboutButton.titleLabel.textAlignment = UITextAlignmentCenter;

or

[aboutButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];

How can I set the title of a UIButton as left-aligned?

Set the contentHorizontalAlignment:

// Swift 
emailBtn.contentHorizontalAlignment = .left;

// Objective-C
emailBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;

You might also want to adjust the content left inset otherwise the text will touch the left border:

// Swift 3 and up:
emailBtn.contentEdgeInsets = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 0);

// Objective-C
emailBtn.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);

How to make text inside UIButton absolute center both verticaly and horizontaly

I have achieved the effect I want via change the title insets via Interface Builder.

Sample Image
Sample Image



Related Topics



Leave a reply



Submit