我有一个UIButton
,我需要能够将其标签设置为任何文本,因此需要垂直拉伸。
使用\n
字符设置文本时,UIButton会正确扩展:
self.myButton.titleLabel.textAlignment = NSTextAlignmentCenter;
self.myButton.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
self.myButton.titleLabel.numberOfLines = 0;
[self.myButton setTitle:@"Lorem ipsum dolor sit amet, consectetuer \nadipiscing elit, sed diam nonummy nibh \neuismod tincidunt ut laoreet dolore \nmagna aliquam erat volutpat. \nUt wisi enim ad minim veniam, \nquis nostrud exerci tation ullamcorper \nsuscipit lobortis nisl ut aliquip ex \nea commodo consequat." forState:UIControlStateNormal];
但是,如果我没有任何\n
,而是让标签根据内容自行换行,那么这些行似乎并不重要UIButton
高度
self.myButton.titleLabel.textAlignment = NSTextAlignmentCenter;
self.myButton.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
self.myButton.titleLabel.numberOfLines = 0;
[self.myButton setTitle:@"Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat." forState:UIControlStateNormal];
UIButton
或其UILabel
上是否存在控制此行为的配置,或者这是iOS错误? (在iOS 9.1上运行)
注意: 我真的不想每次文本更改时手动重新计算UIButton的帧大小,我想知道如果在UIButton上有自动布局约束或配置属性以获得正确的行为
答案 0 :(得分:2)
您需要创建UIButton
的子类,并覆盖intrinsicContentSize
:
@implementation HeightExpandingButton
- (CGSize)intrinsicContentSize {
CGFloat width = self.bounds.size.width;
CGSize labelSize = [self.titleLabel sizeThatFits:CGSizeMake(width, 100000)];
labelSize.width = width;
return labelSize;
}
@end
结果:
答案 1 :(得分:0)
使用以下来实现这一目标。按钮的框架将根据文本计算。
[button sizeToFit];
或者你可以试试这个
NSMutableParagraphStyle *textStyle = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];
textStyle.lineBreakMode = NSLineBreakByWordWrapping;
textStyle.alignment = NSTextAlignmentCenter;
NSDictionary *attributes = @{NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue" size:14],NSParagraphStyleAttributeName:textStyle};
button.frame.size = [buttonTitle sizeWithAttributes:attributes];
[button setTitle:buttonTitle forState:UIControlStateNormal];
或
CGSize maximumLabelSize; //set maximum able size that you can afford. CGSizeMake(310, CGFLOAT_MAX); or CGSizeMake(CGFLOAT_MAX,310);
CGRect textRect = [buttonTitle boundingRectWithSize:maximumLabelSize
options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
attributes:attributes
context:nil];
如果需要,您可以添加额外的填充。
答案 2 :(得分:0)
试试这个......
- (CGSize)getDynamicSizeWithText:(NSString *)text {
CGSize maximumLabelSize = CGSizeMake(self.view.frame.size.width ,CGFLOAT_MAX);
CGRect expectedLabelSize = [text boundingRectWithSize:maximumLabelSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15.0f], NSFontAttributeName : text} context:nil];
return expectedLabelSize.size;
}
然后......
//title = text to display on button
CGSize size = [self getDynamicSizeWithText:title];
[self.myButton setFrame:CGRectMake(self.myButton.frame.origin.x, self.myButton.frame.origin.y, size.width, size.height)];
答案 3 :(得分:0)
如果您还没有使用AutoLayout,请尝试以下代码。希望它为你工作。
self.myButton.titleLabel.numberOfLines=0;
self.myButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
NSString *titleOfButton=@"Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.";
[self.myButton setTitle:titleOfButton forState:UIControlStateNormal];
CGSize constraint1=CGSizeMake(YOUR_BUTTON_WIDTH, 5000.0f);
CGSize size1=[titleOfButton sizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedToSize:constraint1 lineBreakMode:NSLineBreakByWordWrapping];
[self.myButton setFrame:CGRectMake(self.myButton.frame.origin.x, self.myButton.frame.origin.y, self.myButton.frame.size.width, size1.height+20)];
谢谢:)
答案 4 :(得分:0)
您需要计算按钮标题。这是最简单的方法。试试这个;
NSDictionary *attributes = @{ NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue" size:15]};
CGSize size1 = [self.myButton.titleLabel.text sizeWithAttributes:attributes];