himan
himan

Reputation: 459

Aligning a UIButton's text?

I am generating some buttons. In the button I am showing date and a reference id given by the user. I want these data to be left aligned in the button.

What I have tried:

[button.titleLabel setTextAlignment:UITextAlignmentLeft]; 
button.titleLabel.textAlignment = UITextAlignmentLeft;

But this doesn't work.

 - (void)viewDidLoad
{
 [super viewDidLoad];
 [scrollView setScrollEnabled:YES];
 [scrollView setContentSize:CGSizeMake(320, 500)];

int i = 0;

for (CalculatorData *data in calcDatas) {

    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];


    button.frame = CGRectMake(1 , 20 + i*40, 295, 30);

    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"MM/dd/yyyy HH:MM:SS"];

    NSString *dateString = [dateFormat stringFromDate:data.updateDate];

    NSString *title = [NSString stringWithFormat:@"%@ : %@",dateString,[data dataKey]];


    [button setTitle:title forState:UIControlStateNormal];


    [button addTarget:self action:@selector(buttonPressed:)
    forControlEvents:UIControlEventTouchUpInside];
    button.tag = data.calcId;


    [scrollView addSubview:button];
    i++;
}

Upvotes: 13

Views: 18974

Answers (8)

cheznead
cheznead

Reputation: 2809

For Swift 3, the following worked for me:

button.contentHorizontalAlignment = .left
button.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0)

Upvotes: 1

nyxee
nyxee

Reputation: 2811

Swift 3.x:

button.titleLabel?.textAlignment = .center

Upvotes: 0

Suraj K Thomas
Suraj K Thomas

Reputation: 5893

enter image description here

Have a look at Content-Alignment Vertical in storyboard

Upvotes: 1

Pablo Segura
Pablo Segura

Reputation: 49

In Swift you can use this:

uibutton/uilabel.titleLabel?.textAlignment = .Center

NSTextAlignment is a enum there for you can assign it by placing a .VALUE

Here are the possible alignment enum's case Left // Visually left aligned

case Center // Visually centered
case Right // Visually right aligned
/* !TARGET_OS_IPHONE */
// Visually right aligned
// Visually centered

case Justified // Fully-justified. The last line in a paragraph is natural-aligned.
case Natural // Indicates the default alignment for script

Upvotes: 0

dsgriffin
dsgriffin

Reputation: 68626

You can use the contentHorizontalAlignment:

"The horizontal alignment of content (text or image) within the receiver."

and the contentVerticalAlignment properties for this:

"The vertical alignment of content (text or image) within the receiver."

Example usage:

button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
button.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;

Upvotes: 36

Abha
Abha

Reputation: 1082

button.titleLabel.textAlignment = NSTextAlignmentLeft;

Upvotes: 0

Vineet Singh
Vineet Singh

Reputation: 4019

Use this :

button.contentVerticalAlignment = UIControlContentVerticalAlignmentFill; 


button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;

button.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);

Upvotes: 9

Rushabh
Rushabh

Reputation: 3203

button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
button.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);

Upvotes: 2

Related Questions