UI_UITableViewCell自适应高度(图片,字体)
2015-10-05 08:39
330 查看
一.图片自适应高度
RootViewController.m #import "RootViewController.h" #import "MyCell.h" @interface RootViewController ()<UITableViewDataSource, UITableViewDelegate> @property(nonatomic, retain)UITableView *tableView; @property(nonatomic, retain)NSMutableArray *picArr; @property(nonatomic, retain)NSMutableArray *wordArr; @end @implementation RootViewController - (void)dealloc { [_wordArr release]; [_picArr release]; [_tableView release]; [super dealloc]; } 1.数组初始化. - (instancetype) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { self.picArr = [NSMutableArray arrayWithObjects:@"1.jpg", @"2.jpg", @"3.jpg", nil]; } self.wordArr = [NSMutableArray arrayWithObjects:@"中国共产党新闻网北京4月1日电 (万鹏)3月28日,习近平主席出席2015年博鳌论坛年会开幕式并发表了题为《迈向命运共同体 开创亚洲新未来》的主旨演讲,他强调,“亚洲是世界的亚洲。亚洲要迈向命运共同体、开创亚洲新未来,必须在世界前进的步伐中前进、在世界发展的潮流中发展。习主席的演讲传递了哪些重要信息?国务院参事室特邀研究员保育钧,中国国际问题研究院研究员杨希雨做客人民网时谈到,习主席主旨演讲展现出“五大亮点”,再次提出“亚洲方式”的新命题,开幕式本身可谓“一带一路”的各国大合唱,让人印象深刻", @"床前明月光,疑是地上霜.举头望明月,低头思故乡", @"NBA常规赛强强对话,勇士在一度落后17分的情况下,客场以110-106逆转快船,终结对手7连胜的同时豪取10连胜。库里全场轰下27分,并在第二节运球晃倒保罗,技惊四座。快船格里芬40分,外加12篮板5助攻", nil]; return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain]; [self.view addSubview:self.tableView]; [_tableView release]; self.tableView.delegate = self; self.tableView.dataSource = self; self.tableView.rowHeight = 100; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { // 根据图片的不同尺寸,返回不同的高度. // 1.先找根据图片名找图片UIImage UIImage *image = [UIImage imageNamed:self.picArr[indexPath.row]]; // 根据图片原有尺寸计算比例,然后根据目标宽度,计算出返回的高度. CGFloat picHeight = self.view.frame.size.width * image.size.height / image.size.width; // 设置字体自适应高度的时候,注意字体大小保持一致. // 文字的大小作为一个描述信息,放到字典中成为一对. NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:16], NSFontAttributeName,nil]; // 根据文本字体大小来计算文本的cell高度. // 第一个参数:文字显示的最大范围(375,0). CGRect rect = [self.wordArr[indexPath.row] boundingRectWithSize:CGSizeMake(370, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil]; return picHeight; + rect.size.height; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.picArr.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *reuse = @"reuse"; MyCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse]; if (!cell) { cell = [[[MyCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse] autorelease]; } cell.picture.image = [UIImage imageNamed:self.picArr[indexPath.row]]; cell.myLabel.text = self.wordArr[indexPath.row]; return cell; }
MyCell.h #import <UIKit/UIKit.h> @interface MyCell : UITableViewCell @property(nonatomic, retain)UIImageView *picture; @property(nonatomic, retain)UILabel *myLabel; @end
MyCell.m #import "MyCell.h" @implementation MyCell - (void)dealloc { [_myLabel release]; [_picture release]; [super dealloc]; } - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { [self createView]; } return self; } - (void)createView { self.picture = [[UIImageView alloc] init]; self.picture.backgroundColor = [UIColor greenColor]; [self.contentView addSubview:self.picture]; [_picture release]; self.myLabel = [[UILabel alloc] init]; [self.contentView addSubview:self.myLabel]; [_myLabel release]; // 设置myLabel的行数. self.myLabel.numberOfLines = 0; // 设置字体大小. self.myLabel.font = [UIFont systemFontOfSize:16]; } - (void)layoutSubviews { [super layoutSubviews]; // 最后走得一个方法,在这个方法里,属性已经被赋好值了 CGFloat picHeight = self.picture.image.size.height * self.contentView.frame.size.width / self.picture.image.size.width; self.picture.frame = CGRectMake(0, 0, self.contentView.frame.size.width, picHeight); NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:16], NSFontAttributeName,nil]; // 根据文本字体大小来计算文本的cell高度. // 第一个参数:文字显示的最大尺寸范围.(375,0); CGRect rect = [self.myLabel.text boundingRectWithSize:CGSizeMake(370, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil]; // 设置myLabel的尺寸 self.myLabel.frame = CGRectMake(0, picHeight, self.contentView.frame.size.width, rect.size.height); }
相关文章推荐
- UI10_省市区分区跳转版
- SpriteBuilder中时间线播放音效的弊端
- SpriteBuilder中时间线播放音效的弊端
- SpriteBuilder中时间线播放音效的弊端
- UITableView中的一些坑
- SpriteBuilder中音频波长超过Timeline结尾的情况
- SpriteBuilder中音频波长超过Timeline结尾的情况
- SpriteBuilder中音频波长超过Timeline结尾的情况
- 2015/10/4 iOS 笔记 细节 简单-代理过程 UITableView
- HDOJ 题目5496 Beauty of Sequence(数学)
- iOS UISwitch用法和示例
- hdu 5496 Beauty of Sequence bc#58 组合 离散化
- Android后台进程与UI进程的交互
- Fluent NHibernate关系映射
- POJ 题目1239 ||ZOJ 题目 1499 Increasing Sequences(正反两次DP)
- Android UI ActionBar功能-ActionBarSherlock 的使用
- Android UI ActionBar功能-ActionBarSherlock 的使用
- SynchronousQueue的简单应用
- iOS编程-------UIImageView、手势识别器UIGestureRecognizer
- Android UI ActionBar功能-ActionBarProvider的使用