`
ydbc
  • 浏览: 711398 次
  • 性别: Icon_minigender_1
  • 来自: 大连
文章分类
社区版块
存档分类
最新评论

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

 
阅读更多
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 floatfPadding = 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 floatfHeight = size.height + 16.0;
8
9 returnfHeight;
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 自动换行

CGSizetitleBrandSizeForHeight = [titleBrand.textsizeWithFont:titleBrand.font];

CGSizetitleBrandSizeForLines = [titleBrand.textsizeWithFont:titleBrand.fontconstrainedToSize: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)//一行最多多少字节

{

<wbr><wbr><wbr><wbr><wbr>//TextView底面背景图片根据内容自动调整高度</wbr></wbr></wbr></wbr></wbr>

UIImage*img = [UIImageimageWithContentsOfFile:[[NSBundlemainBundle]pathForResource:@"inputbox"ofType:@"png"]];

[BgImagesetImage:[imgstretchableImageWithLeft<wbr>CapWidth<span style="color:rgb(0,0,0)">:</span><span style="color:rgb(62,1,216)">21</span>topCapHeight<span style="color:rgb(0,0,0)">:</span><span style="color:rgb(62,1,216)">14</span><span style="color:rgb(0,0,0)">]];</span></wbr>


UIFont*font = [UIFontsystemFontOfSize:12];

CGSizesize = [textView.textsizeWithFont:fontconstrainedToSize:CGSizeMake(320,140)lineBreakMode:UILineBreakModeWordWrap];

BgImage.frame=CGRectMake(0,202-size.height+15,320, size.height+28);

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

[textViewsetFrame:CGRectMake(51,210-size.height+18,200, size.height+5)];

}

}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics