Sto generando alcuni pulsanti. Nel pulsante sto mostrando la data e un codice di riferimento dato dall'utente. Voglio che questi dati siano allineati a sinistra nel pulsante.Allineare un testo di UIButton?
Quello che ho cercato:
[button.titleLabel setTextAlignment:UITextAlignmentLeft];
button.titleLabel.textAlignment = UITextAlignmentLeft;
Ma questo non funziona.
- (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++;
}
Buona chiamata con 'button.contentEdgeInsets = UIEdgeInsetsMake (0, 10, 0, 0);' Ho usato questo per impostare l'inserto superiore del pulsante per correggere il fatto che 'contentVerticalAlignment 'stava centrando la linea di base del testo del pulsante. Con gli inserti sui bordi è possibile regolare per questo e al centro in base al punto centrale verticale visivo del testo. –