方法1:
- (CGSize)getStringRect:(NSString*)aString
{
CGSize size;
NSAttributedString* atrString = [[NSAttributedString alloc] initWithString:aString];
NSRange range = NSMakeRange(0, atrString.length);
NSDictionary* dic = [atrString attributesAtIndex:0 effectiveRange:&range];
size = [aString boundingRectWithSize:CGSizeMake(237, 200) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;
return size;
}
方法3:
UILabel *describe = (UILabel *)[cell viewWithTag:TAG_One];
NSString *describeText = info.describe;
float describeHeight=[self heightForString:describeText fontSize:16 andWidth:width];
CGRect cellFrame = [cell frame];
cellFrame.origin = CGPointMake(0, 0);
describe.text = describeText;
describe.font=[UIFont systemFontOfSize:16];
describe.frame=CGRectMake(SPACE, NAME_H+Star_Height+3*SPACE,[UIScreen mainScreen].bounds.size.width-2*SPACE, describeHeight);
[describe sizeToFit];
- (float) heightForString:(NSString *)value fontSize:(float)fontSize andWidth:(float)width
{
CGSize sizeToFit = [value sizeWithFont:[UIFont systemFontOfSize:fontSize] constrainedToSize:CGSizeMake(width, 2000) lineBreakMode:NSLineBreakByCharWrapping];
return sizeToFit.height;
}
cell.detailLabel.text=info.detailText;
self.detailHigh=[self getContentHeight:info.detailText];
cell.detailLabel.numberOfLines=0;
cell.detailLabel.frame=CGRectMake(0, 36, cell.frame.size.width, self.detailHigh);
return cell;
}
-(CGFloat)getContentHeight:(NSString*)content
{
CGSize constraint = CGSizeMake(SCREEN_WHIDTH-16, 2000.0f);
CGSize size = [content boundingRectWithSize:constraint options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:16]} context:nil].size;
return size.height<44? 0.0f:size.height+20 ;
}
5.自动扩展label的长度
-(void)addNikeNameInCell:(DTPMineTableViewFirstCell*)cell with:(DTPMineTableViewCellInfo*)info
{
UILabel *label=[[UILabel alloc] init];
[label setFont:[UIFont systemFontOfSize:17]];
[label setText:info.nickName];
[label sizeToFit];
label.frame=CGRectMake(67, 8, label.frame.size.width,29);
label.textColor=[UIColor blackColor];
[cell addSubview:label];
[self addMarkInCell:cell with:info after:label];
}