iOS: изменение размера UIButton в соответствии с длиной текста

В построителе интерфейса удерживание Command + = изменит размер кнопки в соответствии с ее текстом. Мне было интересно, можно ли это сделать программно до того, как кнопка была добавлена ​​в представление.

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button.titleLabel setFont:[UIFont fontWithName:@"Arial-BoldMT" size:12]];
[button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
// I need to know the width needed to accomodate NSStringVariable
[button setTitle:NSStringVariable forState:UIControlStateNormal]; 
// So that I can set the width property of the button before addSubview
[button setFrame:CGRectMake(10, 0, width, fixedHeight)];
[subNavigation addSubview:button];
126
задан buildsucceeded 17 January 2013 в 10:10
поделиться