iOS 7.0之前的方法:
nameLabel = [[UILabel alloc]initWithFrame:CGRectZero]; //先随便设置一个Frame 因为后面要修改的
[nameLabel setNumberOfLines:0]; //可自动换行 文字量一大 就需要换行了
nameLabel.textColor = [UIColor blackColor];
nameLabel.text = @"西门吹雪"; //设置需要自适应的字体
CGSize size = CGSizeMake(320,2000); //写一个不可能超过的范围
UIFont *font = [UIFont fontWithName:@"Arial" size:17]; //默认就是17 不写都可以
CGSize labelsize = [nameLabel.text sizeWithFont:font constrainedToSize:size lineBreakMode:UILineBreakModeWordWrap];
nameLabel.frame = CGRectMake(iconImage.right,35, labelsize.width, labelsize.height); //自适应的方法
[self.view addSubview:nameLabel];
iOS 7.0之后的方法:
nameLabel = [[UILabel alloc]initWithFrame:CGRectZero];
[nameLabel setNumberOfLines:0];
nameLabel.textColor = [UIColor blackColor];
nameLabel.text = @"西门吹雪";
CGSize labelsize = [nameLabel.text boundingRectWithSize:CGSizeMake(200, 2000) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:nameLabel.font forKey:NSFontAttributeName] context:nil].size;
nameLabel.frame = CGRectMake(iconImage.right,35, labelsize.width, labelsize.height);
[self.view addSubview:nameLabel];
相比之下这个方法就简洁多了