UIView动态高度取决于标签高度
UIView dynamic height depending on Label Height
我有一个标签,它动态地从数据库中获取一些数据。
这些数据是字符串,有时可以是 3-4-5 行等。
所以这个标签在 UIView 中。
--UIView
--Label
如何让 UIView
动态获取标签的特定高度?
以下代码将解决您的问题:
//调整视图高度
[yourView setFrame:CGRectMake(yourView.frame.origin.x, yourView.frame.origin.y, yourView.frame.size.width, yourLable.frame.size.height + yourLable.frame.origin.y + extraspace)];
首先用这个函数计算标签的大小及其包含的文本
func calculateSizeOfLabel(text:String,labelWidth:CGFloat,labelFont:UIFont)->CGSize{
let constrainedSize = CGSizeMake(labelWidth , 9999)
var attributesDictionary:[String:AnyObject] = [:]
attributesDictionary = [NSFontAttributeName:labelFont] as [String:AnyObject]
let string:NSMutableAttributedString = NSMutableAttributedString(string:text, attributes:attributesDictionary)
var boundingRect = string.boundingRectWithSize(constrainedSize, options:.UsesLineFragmentOrigin, context:nil)
if (boundingRect.size.width > labelWidth) {
boundingRect = CGRectMake(0,0, labelWidth, boundingRect.size.height);
}
return boundingRect.size
}
然后像这样将返回大小的高度应用到 UIView
let labelText = description.text
let labelWidth = description.bounds.width
let labelFont = description.font
let calculatedHeight = calculateSizeOfLabel(labelText,labelWidth:labelWidth,labelFont:labelFont).height
DescView.frame = CGRectMake(DescView.frame.origin.x, DescView.frame.origin.y, DescView.bounds.width,calculatedHeight)
Bellow 是您问题的有效解决方案。我用了autoLayout
。在 testView
你没有设置 heightAnchor
let testView: UIView = {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
view.backgroundColor = UIColor.redColor()
return view
}()
let testLabel: UILabel = {
let label = UILabel()
label.numberOfLines = 0
label.translatesAutoresizingMaskIntoConstraints = false
label.text = "jashfklhaslkfhaslkjdhflksadhflkasdhlkasdhflkadshkfdsjh"
return label
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(testView)
testView.centerXAnchor.constraintEqualToAnchor(view.centerXAnchor).active = true
testView.centerYAnchor.constraintEqualToAnchor(view.centerYAnchor).active = true
testView.widthAnchor.constraintEqualToConstant(100).active = true
testView.addSubview(testLabel)
testLabel.topAnchor.constraintEqualToAnchor(testView.topAnchor, constant: 10).active = true
testLabel.leftAnchor.constraintEqualToAnchor(testView.leftAnchor, constant: 10).active = true
testLabel.bottomAnchor.constraintEqualToAnchor(testView.bottomAnchor, constant: -10).active = true
testLabel.rightAnchor.constraintEqualToAnchor(testView.rightAnchor, constant: -10).active = true
}
你可以用这张照片的故事板来做
设置标签高度关系为大于等于
并将视图高度关系设置为大于或等于
它就像魔术一样工作
- (IBAction)action:(id)sender {
self.label.text = @"UIImage *imageOne = [UIImage imageNamed:@RosePot.jpUIImageJPEGRepresentationg";
NSLog(@"%f",self.label.bounds.size.height);
float height = [self getHeightForText:@"UIImage *imageOne = [UIImage imageNamed:@RosePot.jpUIImageJPEGRepresentationg" withFont:[UIFont fontWithName:@"HelveticaNeue" size:15] andWidth:self.label.bounds.size.width];
NSLog(@"%f",height);
self.constraint.constant = height + self.viewOne.bounds.size.height;
}
-(float) getHeightForText:(NSString*) text withFont:(UIFont*) font andWidth:(float) width{
CGSize constraint = CGSizeMake(width , 20000.0f);
CGSize title_size;
float totalHeight;
SEL selector = @selector(boundingRectWithSize:options:attributes:context:);
if ([text respondsToSelector:selector]) {
title_size = [text boundingRectWithSize:constraint
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{ NSFontAttributeName : font }
context:nil].size;
totalHeight = ceil(title_size.height);
} else {
title_size = [text sizeWithFont:font
constrainedToSize:constraint
lineBreakMode:NSLineBreakByWordWrapping];
totalHeight = title_size.height ;
}
CGFloat height = MAX(totalHeight, 40.0f);
return height;
}
为视图提供前导、顶部、尾随和高度约束。
并将视图名称的高度出口作为约束[因为我使用了出口名称约束]
我知道这是迟到的答案,但它可能会对其他人有所帮助。
要为 UIView 设置动态高度,请按照 Storyboard
中的简单步骤操作
- 在 UIViewController 中添加一个 UIView 并设置你喜欢的背景颜色
- 现在设置以下约束 Leading、Top、Trailing 和 Height(截至目前)。我们可以调整高度约束以进一步实现动态高度。
- 更新高度约束,如下所示:
- 现在故事板可能会向您展示不等式约束的歧义。但我们现在要解决这个问题。只需在 UIView 中添加一个标签,如图所示
- 现在设置标签前导、尾随、顶部和底部的约束
- 万岁,现在 UIView 高度将根据标签的高度增加。只需对标签进行以下更改
此技术适用于此 UIView 内的其他视图。问题是您必须为此 UIView 中存在的视图指定底部约束。
我有一个标签,它动态地从数据库中获取一些数据。 这些数据是字符串,有时可以是 3-4-5 行等。 所以这个标签在 UIView 中。
--UIView
--Label
如何让 UIView
动态获取标签的特定高度?
以下代码将解决您的问题:
//调整视图高度
[yourView setFrame:CGRectMake(yourView.frame.origin.x, yourView.frame.origin.y, yourView.frame.size.width, yourLable.frame.size.height + yourLable.frame.origin.y + extraspace)];
首先用这个函数计算标签的大小及其包含的文本
func calculateSizeOfLabel(text:String,labelWidth:CGFloat,labelFont:UIFont)->CGSize{
let constrainedSize = CGSizeMake(labelWidth , 9999)
var attributesDictionary:[String:AnyObject] = [:]
attributesDictionary = [NSFontAttributeName:labelFont] as [String:AnyObject]
let string:NSMutableAttributedString = NSMutableAttributedString(string:text, attributes:attributesDictionary)
var boundingRect = string.boundingRectWithSize(constrainedSize, options:.UsesLineFragmentOrigin, context:nil)
if (boundingRect.size.width > labelWidth) {
boundingRect = CGRectMake(0,0, labelWidth, boundingRect.size.height);
}
return boundingRect.size
}
然后像这样将返回大小的高度应用到 UIView
let labelText = description.text
let labelWidth = description.bounds.width
let labelFont = description.font
let calculatedHeight = calculateSizeOfLabel(labelText,labelWidth:labelWidth,labelFont:labelFont).height
DescView.frame = CGRectMake(DescView.frame.origin.x, DescView.frame.origin.y, DescView.bounds.width,calculatedHeight)
Bellow 是您问题的有效解决方案。我用了autoLayout
。在 testView
你没有设置 heightAnchor
let testView: UIView = {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
view.backgroundColor = UIColor.redColor()
return view
}()
let testLabel: UILabel = {
let label = UILabel()
label.numberOfLines = 0
label.translatesAutoresizingMaskIntoConstraints = false
label.text = "jashfklhaslkfhaslkjdhflksadhflkasdhlkasdhflkadshkfdsjh"
return label
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(testView)
testView.centerXAnchor.constraintEqualToAnchor(view.centerXAnchor).active = true
testView.centerYAnchor.constraintEqualToAnchor(view.centerYAnchor).active = true
testView.widthAnchor.constraintEqualToConstant(100).active = true
testView.addSubview(testLabel)
testLabel.topAnchor.constraintEqualToAnchor(testView.topAnchor, constant: 10).active = true
testLabel.leftAnchor.constraintEqualToAnchor(testView.leftAnchor, constant: 10).active = true
testLabel.bottomAnchor.constraintEqualToAnchor(testView.bottomAnchor, constant: -10).active = true
testLabel.rightAnchor.constraintEqualToAnchor(testView.rightAnchor, constant: -10).active = true
}
你可以用这张照片的故事板来做
设置标签高度关系为大于等于
并将视图高度关系设置为大于或等于
它就像魔术一样工作
- (IBAction)action:(id)sender {
self.label.text = @"UIImage *imageOne = [UIImage imageNamed:@RosePot.jpUIImageJPEGRepresentationg";
NSLog(@"%f",self.label.bounds.size.height);
float height = [self getHeightForText:@"UIImage *imageOne = [UIImage imageNamed:@RosePot.jpUIImageJPEGRepresentationg" withFont:[UIFont fontWithName:@"HelveticaNeue" size:15] andWidth:self.label.bounds.size.width];
NSLog(@"%f",height);
self.constraint.constant = height + self.viewOne.bounds.size.height;
}
-(float) getHeightForText:(NSString*) text withFont:(UIFont*) font andWidth:(float) width{
CGSize constraint = CGSizeMake(width , 20000.0f);
CGSize title_size;
float totalHeight;
SEL selector = @selector(boundingRectWithSize:options:attributes:context:);
if ([text respondsToSelector:selector]) {
title_size = [text boundingRectWithSize:constraint
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{ NSFontAttributeName : font }
context:nil].size;
totalHeight = ceil(title_size.height);
} else {
title_size = [text sizeWithFont:font
constrainedToSize:constraint
lineBreakMode:NSLineBreakByWordWrapping];
totalHeight = title_size.height ;
}
CGFloat height = MAX(totalHeight, 40.0f);
return height;
}
为视图提供前导、顶部、尾随和高度约束。
并将视图名称的高度出口作为约束[因为我使用了出口名称约束]
我知道这是迟到的答案,但它可能会对其他人有所帮助。
要为 UIView 设置动态高度,请按照 Storyboard
- 在 UIViewController 中添加一个 UIView 并设置你喜欢的背景颜色
- 现在设置以下约束 Leading、Top、Trailing 和 Height(截至目前)。我们可以调整高度约束以进一步实现动态高度。
- 更新高度约束,如下所示:
- 现在故事板可能会向您展示不等式约束的歧义。但我们现在要解决这个问题。只需在 UIView 中添加一个标签,如图所示
- 现在设置标签前导、尾随、顶部和底部的约束
- 万岁,现在 UIView 高度将根据标签的高度增加。只需对标签进行以下更改
此技术适用于此 UIView 内的其他视图。问题是您必须为此 UIView 中存在的视图指定底部约束。