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

 1 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath  
 2   
 3 {  
 4   
 5     // 在tableView的协议方法中制定 每个cell的高度  
 6   
 7       
 8   
 9     UIImage *image = [self.array objectAtIndex:indexPath.row];  
10   
11       
12   
13     // cell的高度 = cell的宽度 * 图片的高度 / 图片的宽度  
14   
15     CGFloat cellHeight = tableView.bounds.size.width * image.size.height / image.size.width;  
16   
17     return cellHeight;  
18   
19       
20   
21 }  
22   
23 /////////////////////////////////////////////////////////////////  
24   
25 (文字) 自适应高度:  
26   
27 - (void)viewDidLoad  
28   
29 {  
30   
31     [super viewDidLoad];  
32   
33     // Do any additional setup after loading the view.  
34   
35     UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 280, 100)];  
36   
37     label.backgroundColor = [UIColor cyanColor];  
38   
39    // label.text = [self.array objectAtIndex:indexPath.row];  
40   
41     [self.view addSubview:label];  
42   
43     [label release];  
44   
45    // return label.frame.size.height;  
46   
47       
48   
49     // 不限制label显示的行数  
50   
51     label.numberOfLines = 0;  
52   
53     // 让label自己适应内容大小  
54   
55     label.text = @"呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵";  
56   
57     [label sizeToFit];  
58   
59 } 

 

posted @ 2016-06-06 15:42  超级马力  阅读(1177)  评论(0编辑  收藏  举报