Chris Byatt
Chris Byatt

Reputation: 3819

Create UIButton the width of its title

I'm trying to make a UIButton the same width as its title by doing this:

CGSize stringSize = [[tagArray lastObject] sizeWithAttributes:@{NSFontAttributeName:@"System 14.0"}];
tagButton.frame = CGRectMake(x, y, stringSize.width, 30);

However it's crashing on the CGSize line. Any pointers? The object in the array is just a regular NSString.

Upvotes: 0

Views: 32

Answers (1)

Justin Moser
Justin Moser

Reputation: 2005

Change the first line to:

CGSize stringSize = [[tagArray lastObject] sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14.0]}];

Upvotes: 1

Related Questions