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 } 
相關文章
相關標籤/搜索