ios 文字 控件 自适应 高度 宽度 CustomCell 自适应高度+uilabel自动换行+ UITextView 根据内容自动调整高度

本文介绍了如何在iOS开发中实现文字控件的自适应高度,特别是针对CustomCell中的UILabel自动换行和UITextView根据内容动态调整高度。在计算TextView的高度时,需要考虑其内边距,并在使用sizeWithFont:constrainedToSize:lineBreakMode:方法时,正确减去左右padding并加上上下padding以确保高度准确。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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);
4  
5     CGSize size = [strText sizeWithFont: textView.font constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
6  
7     float fHeight = size.height + 16.0;
8  
9     return fHeight;
10 }



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(5titleBrand.frame.size.height , infoWidth, titleBrandSizeForHeight.height);

}else {

titleBrand.frame = CGRectMake(5titleBrand.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(0202-size.height+15320, size.height+28);

InputTextVeiw.contentInset = UIEdgeInsetsZero;//以换行为基准

[textView setFrame:CGRectMake(51210-size.height+18200, size.height+5)];

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值