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
时间: 2024-10-24 05:46:45