Trouble Left-Aligning Uibutton Title (Ios/Swift)

Trouble left-aligning UIButton title (iOS/Swift)

Try this:

button.contentHorizontalAlignment = .left

Swift UiButton how to left align the title?

Try this one

button.contentHorizontalAlignment = .left

How to change Button Title Alignment in Swift?

Use contentHorizontalAlignment.You have to use UIControlContentHorizontalAlignment.Left.You need to usehorizontal not vertical.

btn.contentHorizontalAlignment = UIControlContentHorizontalAlignment.Left

Swift 3.x

btn.contentHorizontalAlignment = .left

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);

UIButton title alignment not changing to right direction in RTL

Use right alignment from storyboard as

Sample Image

or from programmatically as

buttonShowOnMap.contentHorizontalAlignment = .left//For left alignment

buttonShowOnMap.contentHorizontalAlignment = .right//For right alignment

UIButton title and image alignment query

Change the imageEdgeInsets property on the UIButton and then just use setImage:forState:

Aligning text and image on UIButton with imageEdgeInsets and titleEdgeInsets

I agree the documentation on imageEdgeInsets and titleEdgeInsets should be better, but I figured out how to get the correct positioning without resorting to trial and error.

The general idea is here at this question, but that was if you wanted both text and image centered. We don't want the image and text to be centered individually, we want the image and the text to be centered together as a single entity. This is in fact what UIButton already does so we simply need to adjust the spacing.

CGFloat spacing = 10; // the amount of spacing to appear between image and title
tabBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, spacing);
tabBtn.titleEdgeInsets = UIEdgeInsetsMake(0, spacing, 0, 0);

I also turned this into a category for UIButton so it will be easy to use:

UIButton+Position.h

@interface UIButton(ImageTitleCentering)

-(void) centerButtonAndImageWithSpacing:(CGFloat)spacing;

@end

UIButton+Position.m

@implementation UIButton(ImageTitleCentering)

-(void) centerButtonAndImageWithSpacing:(CGFloat)spacing {
self.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, spacing);
self.titleEdgeInsets = UIEdgeInsetsMake(0, spacing, 0, 0);
}

@end

So now all I have to do is:

[button centerButtonAndImageWithSpacing:10];

And I get what I need every time. No more messing with the edge insets manually.

EDIT: Swapping Image and Text

In response to @Javal in comments

Using this same mechanism, we can swap the image and the text. To accomplish the swap, simply use a negative spacing but also include the width of the text and the image. This will require frames to be known and layout performed already.

[self.view layoutIfNeeded];
CGFloat flippedSpacing = -(desiredSpacing + button.currentImage.size.width + button.titleLabel.frame.size.width);
[button centerButtonAndImageWithSpacing:flippedSpacing];

Of course you will probably want to make a nice method for this, potentially adding a second category method, this is left as an exercise to the reader.

How to create a left aligned title with prefersLargeTitles in navigationBar?

Here's a link which can help you to find a way out as you want : Change title according to navigation bar.

Some basic things would like to add is you can set navigation bar title to empty string or something else as you want and make your custom view visible if the bar is collapsed other then that make it invisible. You can check in the link how to observe if navigation bar is collapsed or not.

One more important thing is you should remove observers if you are switching to another screen or it can cause memory leaks.

iOS UIButton with UITextAlignmentLeft still centering text?

My test code:

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(20, 20, 200, 72);
UIImage *image = [UIImage imageNamed:@"rob.png"];
[button setImage:image forState:UIControlStateNormal];
button.imageEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
[button setTitle:@"Hello" forState:UIControlStateNormal];
button.titleEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0);
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
[self.view addSubview:button];

Note that I set button.contentHorizontalAlignment, not button.titleLabel.textAlignment. My result:

Screen shot of label



Related Topics



Leave a reply



Submit