Heim > Fragen und Antworten > Hauptteil
描述你的问题
在storyboard上 拖一个view 设置好约束, 拖一个label ,设置左、上、宽度约束,label 文字变化的时候view 高度也要跟随变化,如何计算label文字变化
贴上相关代码
(void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.label.text = @"嘎哈和嘎哈了韩国辣和韩国人哈哈给扔了过来哈日韩国和腊肉和嘎哈和认购或利润阿尔哈嘎哈恶化然后给hlahahghlalhrh了哈哈个哈哈入行后任何两个哈喽哈嘎哈了和老公哈理工哈哈额合格和拉和老公哈哈嘎哈和嘎哈和嘎哈恩会更好还染了个哈哈好了噶好了好给力哈尔和噶哈哈噶呵呵旮旯儿回来给海拉尔和爱好了和格拉混合阿合格和阿里干活然后两个哈哈哈噶好人来过哈哈哈给拉黑了更好拉回来感化了韩国和拉二胡和个案和老公哈额哈和嘎哈恩老公回来回来让韩国哈哈哈两个哈哈了如何给拉黑了如果还拉黑拉黑了如果拉回来后然后俩人好老公还拉黑了任何两个号恋人hall和";
[self.oriangeView layoutIfNeeded];
[self.oriangeView updateConstraints];
}
-(void)touchesBegan:(NSSet<UITouch > )touches withEvent:(UIEvent *)event
{
// self.oriangeView.bounds = CGRectMake(0.0f, 0.0f, CGRectGetWidth(self.oriangeView.bounds), CGRectGetHeight(self.label.bounds));
[self.oriangeView setNeedsLayout];
[self.oriangeView layoutIfNeeded];
// 得到cell的contentView需要的真实高度
CGFloat height = [self.oriangeView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
// 要为cell的分割线加上额外的1pt高度。因为分隔线是被加在cell底边和contentView底边之间的。
height += 1.0f;
NSLog(@"%f",height);
}
贴上报错信息
贴上相关截图
已经尝试过哪些方法仍然没解决(附上相关链接)
大家讲道理2017-05-02 09:20:30
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *label;
@property (weak, nonatomic) IBOutlet UIView *oriangeView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *upConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *smallViewConstraint;
@end
@implementation ViewController
(void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.label.text = @"嘎哈和嘎哈了韩国辣和韩国人哈哈给扔了过来哈日韩国和腊肉和嘎哈和认购或利润阿尔哈嘎哈恶化然后给hlahahghlalhrh了哈哈个哈哈入行后任何两个哈喽哈嘎哈了和老公哈理工哈哈额合格和拉和老公哈哈嘎哈和嘎哈和嘎哈恩会更好还染了个哈哈好了噶好了好给力哈尔和噶哈哈噶呵呵旮旯儿回来给海拉尔和爱好了和";
self.label.textAlignment = NSTextAlignmentLeft;
self.label.font =[UIFont systemFontOfSize:15];
// [self.oriangeView layoutIfNeeded];
// [self.oriangeView updateConstraints];
}
-(CGSize)stringSizeWithFont:(UIFont )font string:(NSString )string width:(CGFloat)width
{
CGRect rect =[string boundingRectWithSize:CGSizeMake(width, 10000) options: NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil];
return rect.size;
}
-(void)touchesBegan:(NSSet<UITouch > )touches withEvent:(UIEvent *)event
{
CGFloat height = 0;
self.label.text = @"嘎哈和嘎哈了韩国辣和韩国人哈哈给扔了过来哈日韩国和腊肉和嘎哈和认购或利润阿尔哈嘎哈恶化然后给hlahahghlalhrh了哈哈个哈哈入行后任何两个哈喽哈嘎哈了和老公哈理工哈哈额合格和拉和老公哈哈嘎哈和嘎哈和嘎哈恩会更好还染了个哈哈好了噶好了好给力哈尔和噶哈哈噶呵呵旮旯儿回来给海拉尔和爱好了和嘎哈和嘎哈了韩国辣和韩国人哈哈给扔了过来哈日韩国和腊肉和嘎哈和认购或利润阿尔哈嘎哈恶化然后给hlahahghlalhrh了哈哈个哈哈入行后任何两个哈喽哈嘎哈了和老公哈理工哈哈额合格和拉和老公哈哈嘎哈和嘎哈和嘎哈恩会更好还染了个哈哈好了噶好了好给力哈尔和噶哈哈噶呵呵旮旯儿回来给海拉尔和爱好了和";
height = [self stringSizeWithFont:[UIFont systemFontOfSize:15] string:self.label.text width:self.label.frame.size.width].height +self.upConstraint.constant ;
NSLog(@"动态返回高度%f",height);
self.smallViewConstraint.constant =height;
[UIView animateWithDuration:2 animations:^{
[self.oriangeView layoutIfNeeded];
}];
}