我正在生成一些按钮。在按钮中,我显示日期和用户提供的参考 ID。我希望这些数据在按钮中保持对齐。
我试过的:
[button.titleLabel setTextAlignment:UITextAlignmentLeft];
button.titleLabel.textAlignment = UITextAlignmentLeft;
但这不起作用。
- (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++;
}