IOS---UITableViewCell自适应行高(非AutoLayout)

如题所示,本程序是使用非AutoLayout写的UITableView自适应行高,之后笔者将会写一个基于AutoLayout的自适应行高的小demo。

PS:此小程序只适用于刚接触IOS的小朋友,只用做参考,毫无技术性,大神勿喷。

上代码:

//UITableViewCell
#import <UIKit/UIKit.h>

@interface commentaryCell : UITableViewCell

@property (retain, nonatomic)  UILabel *userID;

@property (retain, nonatomic) UILabel *date;

@property (retain, nonatomic) UILabel *commentary;

-(void)setContent:(NSString *)userid_dic :(NSString *)date_dic :(NSString *)comment_dic;
@end

#import "commentaryCell.h"

float width;
float height;
NSString *commentaryStr;

@implementation commentaryCell

@synthesize userID;
@synthesize date;
@synthesize commentary;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.contentView.backgroundColor = [UIColor whiteColor];

        [self createView];

    }
    return self;
}

-(void)createView{
    width = self.contentView.frame.size.width;
    height = self.contentView.frame.size.height;
    //用户ID
    userID = [[UILabel alloc] initWithFrame:CGRectMake(width*0.05, height*0.3, width*0.3, height*0.3)];
    userID.font = [UIFont systemFontOfSize:14];
    [userID setTextColor:[UIColor colorWithRed:85.0/255.0 green:85.0/255.0 blue:85.0/255.0 alpha:0.6]];
    [self.contentView addSubview:userID];

    //时间图标
    //时间
    date = [[UILabel alloc] initWithFrame:CGRectMake(width*0.4, height*0.3, width*0.5, height*0.3)];
    date.font = [UIFont systemFontOfSize:14];
    [date setTextColor:[UIColor colorWithRed:85.0/255.0 green:85.0/255.0 blue:85.0/255.0 alpha:0.6]];
    [self.contentView addSubview:date];

    //评论
    commentary = [[UILabel alloc] init];
    [commentary setNumberOfLines:0];
    commentary.font = [UIFont systemFontOfSize:16];
    [commentary setTextColor:[UIColor colorWithRed:85.0/255.0 green:85.0/255.0 blue:85.0/255.0 alpha:1]];
    [self.contentView addSubview:commentary];
}

-(void)setContent:(NSString *)userid_dic :(NSString *)date_dic :(NSString *)comment_dic{
    userID.text = userid_dic;
    date.text = date_dic;

    [commentary setNumberOfLines:0];
    commentary.text = comment_dic;
    commentary.font = [UIFont systemFontOfSize:16];
    CGSize commentSize = [self returnSize:commentary.text font:commentary.font];
    [commentary setFrame:CGRectMake(width*0.05, 41, commentSize.width, commentSize.height)];
}

//返回Label的Size
-(CGSize)returnSize:(NSString *)text font:(UIFont *)font{
    float width = [UIScreen mainScreen].bounds.size.width;
    float height = [UIScreen mainScreen].bounds.size.height;
    CGSize _Size = CGSizeMake(width*0.9, height);
    CGSize Size = [text sizeWithFont:font constrainedToSize:_Size lineBreakMode:NSLineBreakByWordWrapping];
    return Size;
}
时间: 2024-07-30 20:55:16

IOS---UITableViewCell自适应行高(非AutoLayout)的相关文章

Java 设置Excel自适应行高、列宽

在excel中,可通过设置自适应行高或列宽自动排版,是一种比较常用的快速调整表格整体布局的方法.设置自适应时,可考虑2种情况: 1.固定数据,设置行高.列宽自适应数据(常见的设置自适应方法) 2.固定列宽,缩小数据字体大小适应列宽 本文将通过java程序来介绍具体的自适应设置方法.   工具:Free Spire.XLS for Java (免费版) 注:可通过官网下载包,并解压将lib文件夹下的jar文件导入java程序:或者通过maven仓库下载并导入. 参考如下导入效果: Java 代码示

ios label 自动计算行高详解

在OC当中自动计算行高主要调用系统的 p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #ffffff } span.s1 { } span.s2 { color: #00b1ff } span.s3 { color: #de38a6 } span.s4 { color: #eb905a } span.s5 { color: #8b87ff } - (--boundingRectWithSize:(CGSize)

iOS 动态计算行高,宽等

UILabel有两个计算文字大小的方法: 1.针对对富文本计算NSAttributedString - (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:(nullable NSDictionary*)attributes context:(nullable NSStringDrawingContext *)context NS_AVAILABLE(10_11,

iOS UITableViewCell自适应高度

在cell.m文件中 1)初始化方法中: self.lalName=[[UILabel alloc] initWithFrame:CGRectMake(71, 5, 250, 40)]; [self addSubview:self.lalName]; 2)创建方法: //给用户介绍赋值并且实现自动换行 -(void)setIntroductionText:(NSString*)text{ //获得当前cell的高度 CGRect frame=[self frame]; //文本赋值 self.l

rmReport 自适应行高(自动行高)

这个问题 1.先中主项数据--属性--stretched(伸展):true 选中主项数据中的所有列--属性--其他属性--自动折行 --伸展

java POI excel 单元个的内容 自适应行高

public static void main(String[] args) { InputStream is = null; Workbook book = null; try { is = new FileInputStream("D:\\test.xls"); book = new HSSFWorkbook(is); Sheet sheet = book.getSheetAt(0); for(int i = 0; i <= 10; i ++) { Row row = she

iOS开发tips-UITableView、UICollectionView行高/尺寸自适应

UITableView 我们都知道UITableView从iOS 8开始实现行高的自适应相对比较简单,首先必须设置estimatedRowHeight给出预估高度,设置rowHeight为UITableViewAutomaticDimension(注意:如果不修改rowHeight默认就是UITableViewAutomaticDimension),对于这两个参数除了直接修改tableview对应的属性之外仍然支持使用对应的代理方法设置.最后只要在UITableViewCell中设置conten

wpf datagrid row height 行高自动计算使每行行高自适应文本

wpf 的datagrid的行高 要么是Auto,要么是定值:但会带来麻烦就是每行行高都一样. 当需要按内容(主要是wrap 换行的textbox或textblock)来动态调整行高的时候,需要用到dataGrid的LoadingRow 事件. 参考两个网页: http://stackoverflow.com/questions/9264398/how-to-calculate-wpf-textblock-width-for-its-known-font-size-and-characters

ios开发日记11 对tableView三种计算动态行高方法的分析

tableView是一个神奇的东西,可以这么说,就算是一个初学者如果能把tableView玩的很6,那编一般的iOS的需求都问题不大了.tableView是日常开发中用烂了的控件,但是关于tableView中的自定义cell的动态行高,还是有一些玄机的. AD: tableView是一个神奇的东西,可以这么说,就算是一个初学者如果能把tableView玩的很6,那编一般的iOS的需求都问题不大了.tableView是日常开发中用烂了的控件,但是关于tableView中的自定义cell的动态行高,