Reputation: 459
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
Reputation: 2809
For Swift 3, the following worked for me:
button.contentHorizontalAlignment = .left
button.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0)
Upvotes: 1
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
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
Reputation: 4019
Use this :
button.contentVerticalAlignment = UIControlContentVerticalAlignmentFill;
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
button.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
Upvotes: 9
Reputation: 3203
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
button.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
Upvotes: 2