CustomCell 自适应高度+label自动换行+ UITextView 根据内容自动调整高度
TextView在上下左右分别有一个8px的padding,当使用[NSString sizeWithFont:constrainedToSize:lineBreakMode:]时,需要将UITextView.contentSize.width减去16像素(左右的padding 2 x 8px)。同时返回的高度中再加上16像素(上下的padding),这样得到的才是UITextView真正适应内容的高度。
示例代码如下:
1 | +
( float )
heightForTextView: (UITextView *)textView WithText: ( NSString *)
strText{ |
2 | float fPadding
= 16.0; //
8.0px x 2 |
3 | CGSize
constraint = CGSizeMake(textView.contentSize.width - fPadding, CGFLOAT_MAX); |
5 | CGSize
size = [strText sizeWithFont: textView.font constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap]; |
7 | float fHeight
= size.height + 16.0; |
CustomCell 自适应高度
//Customiz the height of table view cell
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
Status * sts = [statuses objectAtIndex:indexPath.row]; NSString *cellText = sts.text; UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:17.0]; CGSize constraintSize = CGSizeMake(240.0f, MAXFLOAT); CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap]; return labelSize.height + 20;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
//cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
// cell.textLabel.numberOfLines = 0;
//cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:17.0];
}
Status *sts = [statuses objectAtIndex:indexPath.row];
//cell.textLabel.text = sts.user.screenName;
//cell.detailTextLabel.text = sts.text;
UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(80, 0, 240, 20)];
label.text = sts.user.screenName;
[cell addSubview:label];
[label release];
NSString *cellText = sts.text;
UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:12.0];
CGSize constraintSize = CGSizeMake(240.0f, MAXFLOAT);
CGSize labelHeight = [cellText sizeWithFont:cellFont];
CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
UILabel* tv = [[UILabel alloc]initWithFrame:CGRectMake(80, 20, labelSize.width, labelSize.height)];
tv.text = sts.text;
tv.textColor = [UIColor yellowColor]; tv.numberOfLines = ceil(labelSize.height/labelHeight.height); tv.lineBreakMode = UILineBreakModeWordWrap;
tv.backgroundColor = [UIColor grayColor];
//[label sizeToFit];
[cell addSubview:tv];
[tv release];
// Configure the cell.
return cell;
}
UILabel 自动换行
CGSize titleBrandSizeForHeight = [titleBrand.text sizeWithFont:titleBrand.font];
CGSize titleBrandSizeForLines = [titleBrand.text sizeWithFont:titleBrand.font constrainedToSize:CGSizeMake(infoWidth, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap]; titleBrand.numberOfLines = ceil(titleBrandSizeForLines.height/titleBrandSizeForHeight.height);
if (titleBrand.numberOfLines <= 1) { titleBrand.frame = CGRectMake(5, titleBrand.frame.size.height , infoWidth, titleBrandSizeForHeight.height); }else { titleBrand.frame = CGRectMake(5, titleBrand.frame.size.height , infoWidth, titleBrand.numberOfLines*titleBrandSizeForHeight.height); }
UITextView 根据内容自动调整高度- (void)textViewDidChange:(UITextView *)textView{ if(textView.text.length > 20)//一行最多多少字节 { //TextView底面背景图片根据内容自动调整高度
UIImage *img = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"inputbox" ofType:@"png"]];
[BgImage setImage:[img stretchableImageWithLeftCapWidth:21 topCapHeight:14]];
UIFont *font = [UIFont systemFontOfSize:12];
CGSize size = [textView.text sizeWithFont:font constrainedToSize:CGSizeMake(320, 140) lineBreakMode:UILineBreakModeWordWrap]; BgImage.frame = CGRectMake(0, 202-size.height+15, 320, size.height+28);
InputTextVeiw.contentInset = UIEdgeInsetsZero;//以换行为基准 [textView setFrame:CGRectMake(51, 210-size.height+18, 200, size.height+5)]; } }
|