EvaluateTableViewCell.h #import <UIKit/UIKit.h> @interface EvaluateTableViewCell : UITableViewCell @property (nonatomic,strong) UILabel *phoneLabel; @property (nonatomic,strong) UILabel *timeLabel; @property (nonatomic,strong) UILabel *descLabel; @property (nonatomic,strong) UIView *intervalView; //評價內容並且實現自動換行 -(void)setIntroductionText:(NSString*)text; @end EvaluateTableViewCell.m
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { //使用者手機號 self.phoneLabel = [[UILabel alloc] initWithFrame:CGRectMake(7, 15, 100, 12)]; self.phoneLabel.font = FONT(12); [self addSubview:self.phoneLabel]; //評價時間 self.timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(247 * (kScreenWidth / 320), 15, 72, 11)]; self.timeLabel.font = FONT(11); self.timeLabel.textColor = [UIColor grayColor]; [self addSubview:self.timeLabel]; //分隔線 UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(8, self.timeLabel.maxY + 14, kScreenWidth - 16, 1)]; lineView.backgroundColor = [UIColor blackColor]; [self addSubview:lineView]; //評價內容 self.descLabel = [[UILabel alloc] initWithFrame:CGRectMake(11, lineView.maxY+ 29, kScreenWidth - 22, 13)]; self.descLabel.font = FONT(12); self.descLabel.numberOfLines = 0; [self addSubview:self.descLabel]; //間隔 self.intervalView = [[UIView alloc] initWithFrame:CGRectMake(0, self.descLabel.maxY + 9, kScreenWidth, 2)]; self.intervalView.backgroundColor = [UIColor blackColor]; [self addSubview:self.intervalView]; } return self; } //賦值 and 自動換行,計算出cell的高度 -(void)setIntroductionText:(NSString*)text{ //獲得當前cell高度 CGRect frame = [self frame]; //文本賦值 self.descLabel.text = text; //設定label的最大行數 CGSize size = CGSizeMake(300, 1000); CGSize labelSize = [self.descLabel.text sizeWithFont:self.descLabel.font constrainedToSize:size lineBreakMode:NSLineBreakByClipping]; self.descLabel.frame = CGRectMake(self.descLabel.frame.origin.x, self.descLabel.frame.origin.y, labelSize.width, labelSize.height); //計算出自適應的高度 frame.size.height = labelSize.height + 85; self.frame = frame; self.intervalView.frame = CGRectMake(0, self.descLabel.maxY + 9, kScreenWidth, 4); }
|