2

I need to increase height of the UIButton according to its title.

Image demonstrating the issue is below

enter image description here

Cœur
  • 37,241
  • 25
  • 195
  • 267
user2160335
  • 31
  • 1
  • 5
  • possible duplicate.. http://stackoverflow.com/questions/4135032/ios-uibutton-resize-according-to-text-length – Amar Jun 14 '13 at 07:08

2 Answers2

7

This is fully tested as u said dynamic height

myButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.titleLabel.numberOfLines=0;
myButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
NSString *titleOfButton=@"this is the very launch soon title here u can pass as long as big string";
[myButton setTitle:titleOfButton forState:UIControlStateNormal];
CGSize constraint1=CGSizeMake(150.0f, 5000.0f);
CGSize size1=[titleOfButton sizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedToSize:constraint1 lineBreakMode:NSLineBreakByWordWrapping];
[myButton setFrame:CGRectMake(10,50,150, size1.height+20)];
[self.view addSubview:myButton];
Anupdas
  • 10,211
  • 2
  • 35
  • 60
Vivek Sehrawat
  • 6,560
  • 2
  • 26
  • 39
0

Use this code for help

UIFont font = myButton.titleLabel.font;
CGSize textsize = [myButton.titleLabel.text sizeWithFont:font constrainedToSize:CGSizeMake(myButton.frame.size.width,999) lineBreakMode:UILineBreakModeTailTruncation]; 

[myButton setFrame:CGRectMake(myButton.frame.origin.x,myButton.frame.origin.y,myButton.frame.size.width, textsize.height)];
Ishu
  • 12,797
  • 5
  • 35
  • 51