FROM http://hi.baidu.com/%B0%D9ii%D6%AAi%B5%C0/blog/item/98a9d6cfe53ded440eb3457b.html

CustomCell 自适应高度+label自动换行+ UITextView 根据内容自动调整高度
2011/03/26 11:48

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)];

}

 

 

FROM http://www.cocoachina.com/bbs/simple/?t67076.html

 

在坛子里找到的 别人写的...    
//动态改变高度
    CGSize constraintSize;
    constraintSize.width = 300;
    constraintSize.height = MAXFLOAT;
    CGSize sizeFrame =[textContent sizeWithFont:textView.font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
    
        textView.frame = CGRectMake(0,0,sizeFrame.width,sizeFrame.height);

arrow
arrow
    全站熱搜

    zer931 發表在 痞客邦 留言(0) 人氣()