码迷,mamicode.com
首页 > 移动开发 > 详细

iOS动态计算UIScrollView的contentSize示例(页面由Label组成)

时间:2015-09-11 20:44:28      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:

 1 #import "ViewController.h"
 2 
 3 @interface ViewController ()
 4 {
 5     UIScrollView *_scrollView;
 6 }
 7 //数据源
 8 @property (nonatomic,strong)NSArray *dataArray;
 9 
10 @end
11 
12 @implementation ViewController
13 
14 - (void)viewDidLoad {
15     [super viewDidLoad];
16     
17     NSString *path = [[NSBundle mainBundle] pathForResource:@"statuses" ofType:@"plist"];
18     _dataArray = [NSArray arrayWithContentsOfFile:path];
19     
20     CGFloat height = 0;
21     
22     _scrollView = [[UIScrollView alloc] init];
23     _scrollView.translatesAutoresizingMaskIntoConstraints = NO;
24     [self.view addSubview:_scrollView];
25     
26     NSArray *consScrHor = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[_scrollView]-0-|" options:NSLayoutFormatAlignmentMask metrics:nil views:NSDictionaryOfVariableBindings(_scrollView)];
27     [self.view addConstraints:consScrHor];
28     
29     NSArray *consScrVor = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[_scrollView]-0-|" options:NSLayoutFormatAlignmentMask metrics:nil views:NSDictionaryOfVariableBindings(_scrollView)];
30     [self.view addConstraints:consScrVor];
31     
32     for (NSDictionary *dict in _dataArray) {
33         //CGSize size = [dict[@"text"] boundingRectWithSize:[UIScreen mainScreen].bounds.size options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16.0]} context:nil].size;
34         //NSLog(@"size:%@",NSStringFromCGSize(size));
35         
36         UILabel *toolLabel = [[UILabel alloc] init];
37         toolLabel.text = dict[@"text"];
38         toolLabel.numberOfLines = 0;
39         CGSize size = [toolLabel sizeThatFits:CGSizeMake([UIScreen mainScreen].bounds.size.width, MAXFLOAT)];
40         toolLabel.frame = CGRectMake(0, height, 375, size.height);
41         
42         toolLabel.backgroundColor = [UIColor orangeColor];
43         [_scrollView addSubview:toolLabel];
44         
45         height+=size.height;
46         
47         UIView *spaceView = [[UIView alloc] initWithFrame:CGRectMake(0, height, 375, 30)];
48         spaceView.backgroundColor = [UIColor yellowColor];
49         [_scrollView addSubview:spaceView];
50         height+=30;
51         
52         NSLog(@"%@",dict[@"text"]);
53         NSLog(@"%@",NSStringFromCGSize(size));
54     }
55     
56     _scrollView.contentSize = CGSizeMake(375, height);
57     
58 }
59 
60 - (void)didReceiveMemoryWarning {
61     [super didReceiveMemoryWarning];
62     // Dispose of any resources that can be recreated.
63 }
64 
65 @end

 

iOS动态计算UIScrollView的contentSize示例(页面由Label组成)

标签:

原文地址:http://www.cnblogs.com/liaods/p/4801923.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!