【发布时间】:2013-06-14 07:02:40
【问题描述】:
我需要根据标题增加UIButton 的高度。
演示问题的图片如下
【问题讨论】:
标签: iphone ios uibutton cgsize
我需要根据标题增加UIButton 的高度。
演示问题的图片如下
【问题讨论】:
标签: iphone ios uibutton cgsize
正如你所说的动态高度,这是经过全面测试的
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];
【讨论】:
NSDictionary *attributes =@{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue" size:15]}; CGSize size1 = [firstTitle sizeWithAttributes:attributes];
使用此代码获取帮助
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)];
【讨论】: