UIView的自适应高度 (图像,文字)

来源:互联网 发布:里约奥运会数据 编辑:程序博客网 时间:2024/06/06 04:25
(图像)自适应高度:- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{    // 在tableView的协议方法中制定 每个cell的高度        UIImage *image = [self.array objectAtIndex:indexPath.row];        // cell的高度 = cell的宽度 * 图片的高度 / 图片的宽度    CGFloat cellHeight = tableView.bounds.size.width * image.size.height / image.size.width;    return cellHeight;    }/////////////////////////////////////////////////////////////////(文字) 自适应高度:- (void)viewDidLoad{    [super viewDidLoad];    // Do any additional setup after loading the view.    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 280, 100)];    label.backgroundColor = [UIColor cyanColor];   // label.text = [self.array objectAtIndex:indexPath.row];    [self.view addSubview:label];    [label release];   // return label.frame.size.height;        // 不限制label显示的行数    label.numberOfLines = 0;    // 让label自己适应内容大小    label.text = @"呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵";    [label sizeToFit];}


0 0