gpt4 book ai didi

ios - UILabel 高度不变

转载 作者:行者123 更新时间:2023-11-28 22:03:17 26 4
gpt4 key购买 nike

我试图更改我在自定义单元格中定义的两个标签高度,但失败了很多次。标签的高度应随填充的文本长度而变化。我已在 heightForRowAtIndexPAth 中成功实现单元格高度,但标签高度未根据单元格高度进行调整。它要么截断文本,要么缩小字体。 SetNumberOfLines 为零似乎没有效果。 ContentTextViewTransView 是我的两个 uiLabels

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Customcell";

CustomeCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

NSString *text = [_detailItem objectAtIndex:[indexPath row]];
NSString *text2 = [_detailItem2 objectAtIndex:[indexPath row]];


CGSize maxSize = CGSizeMake(296.f, FLT_MAX);
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text];
NSMutableAttributedString *attributedString2 = [[NSMutableAttributedString alloc] initWithString:text2];

CGRect rect = [attributedString boundingRectWithSize:maxSize
options:NSStringDrawingUsesLineFragmentOrigin
context:nil];
CGRect rect2 = [attributedString2 boundingRectWithSize:maxSize
options:NSStringDrawingUsesLineFragmentOrigin
context:nil];


CGSize maximumLabelSize = CGSizeMake(500,CGFLOAT_MAX);
CGSize requiredSize = [cell.contentTextView sizeThatFits:maximumLabelSize];
CGSize requiredSize2 = [cell.transView sizeThatFits:maximumLabelSize];
CGRect labelFrame = cell.contentTextView.frame;
CGRect labelFrame2 = cell.transView.frame;
labelFrame.size.height = requiredSize.height;
labelFrame2.size.height=requiredSize2.height;
cell.contentTextView.frame = labelFrame;
cell.transView.frame=labelFrame2;
[attributedString addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, [text length])];
[attributedString2 addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, [text2 length])];

[cell.contentTextView setLineBreakMode:NSLineBreakByWordWrapping];
[cell.contentTextView setMinimumScaleFactor:FONT_SIZE];
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
[style setLineSpacing:2];
[style setAlignment:(NSTextAlignment)kCTRightTextAlignment];
[cell.contentTextView setNumberOfLines:0];
[ cell.transView setNumberOfLines:0];

cell.selectionStyle = UITableViewCellSelectionStyleBlue;
tableView.separatorColor = [UIColor colorWithRed:0.576 green:0.796 blue:0.008 alpha:1];

cell.contentTextView.attributedText = attributedString;
cell.transView.attributedText=attributedString2;

return cell;

最佳答案

-(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];
UILabel *lblText = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, 275, [self dynamicHeightAtIndexPath:indexPath])];
lblText.backgroundColor = [UIColor clearColor];
[lblText setTag:1];
[cell.contentView addSubview:lblText];
}
UILabel *lbl = (UILabel *)[cell.contentView viewWithTag:1];
lbl.numberOfLines=0;
lbl.attributedText = @"label text";
return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

return [self dynamicHeightAtIndexPath:indexPath]+20;

}

-(CGFloat)dynamicHeightAtIndexPath:(NSIndexPath *)indexPath
{
CGSize maximumSize = CGSizeMake(275, 9999);
UIFont *myFont =[UIFont fontWithName:@"Halvetica" size:12]; //your font for label
CGSize stringsize = [[self.array objectAtIndex:indexPath.row] sizeWithFont:myFont
constrainedToSize:maximumSize
lineBreakMode:NSLineBreakByWordWrapping];
return stringsize.height;
}

关于ios - UILabel 高度不变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24583211/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com