简体   繁体   English

如何将uilabel的高度设置为等于uitableviewcell

[英]how to set height of uilabel equal to uitableviewcell

I have a grouped style Tableview which have uilabels in Tableviewcell. 我有一个分组样式的Tableview,它在Tableviewcell中有uilabels。 Now i want to set height of uilabels equal to height of cell how can i do ths??? 现在我想将uilabels的高度设置为等于单元格的高度,我该怎么做?

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:CellIdentifier];

}

// here i want to make height of label equal to height of cell
UILabel *category = [[UILabel alloc] initWithFrame:CGRectMake(95,1,140,25)];
category.font = [UIFont fontWithName:@"Arial" size:14.0f] ;
category.textAlignment = NSTextAlignmentRight;
[category setBackgroundColor:[UIColor clearColor]];
[cell addSubview:category];
}

In the cellForRowAtIndexPath add; 在cellForRowAtIndexPath中添加; this will return current height set for your tableview: 这将返回为您的表视图设置的当前高度:

CGFloat currentCellHeight = [self tableView:tableView heightForRowAtIndexPath:indexPath];
UILabel myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, customWidth, currentCellHeight)];

Get default cell height 获取默认的单元格高度

UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];



UILabel *category = [[UILabel alloc]
                 initWithFrame:CGRectMake(0,0,cell.frame.size.width,cell.frame.size.width)];

YOu can use following to find height in 您可以使用以下内容来查找高度

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


CGSize textSize = [[NSString stringWithFormat:@"%@ %@",label.text] sizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedToSize:CGSizeMake(200, 1000.0f) lineBreakMode:NSLineBreakByCharWrapping];
return textSize.height; //height for cell
}

如果您想要单元格的高度,请执行以下操作:

category.frame = CGRectMake(0,0,width,cell.bounds.size.height);
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:CellIdentifier];

    }

  // Look at this 

 UILabel *category = [[UILabel alloc]
                     initWithFrame:CGRectMake(0,0,cell.bounds.size.width,cell.bounds.size.height)];


    category.font = [UIFont fontWithName:@"Arial" size:14.0f] ;
    category.textAlignment = NSTextAlignmentRight;
    [category setBackgroundColor:[UIColor clearColor]];
    [cell addSubview:category];
    }

use this code... 使用此代码...

I think there's two things you can do; 我认为您可以做两件事; first try reading this link from apple and review your approach. 首先,请尝试从Apple阅读此链接并查看您的方法。 If you only need a label, why not use the cells textLabel and customize its appearance? 如果只需要标签,为什么不使用单元格textLabel并自定义其外观?

If you really want to use the label approach then consider adding it to the contentView instead of the cell. 如果您确实要使用标签方法,请考虑将其添加到contentView而不是单元格中。 You can also try to get the bounds of this property, instead of the c 您也可以尝试获取此属性的边界,而不是c

UILabel *category = [[UILabel alloc] initWithFrame:CGRectMake(0,0,width,cell.contentView.bounds.size.height)];
[cell.contentView addSubview:category];

Hope this helps. 希望这可以帮助。

//尝试此代码

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(95,1,140,cell.frame.size.height)];

just set the cell bounds to label's frame,like below. 只需将单元格边界设置为标签的框架,如下所示。

UILabel *myLabel = [[UILabel alloc] initWithFrame:cell.bounds]; UILabel * myLabel = [[UILabel alloc] initWithFrame:cell.bounds];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM