0

我对 iphone 开发非常陌生,并且在某一点上感到震惊。实际上,我需要的是在 a 中显示一些结果,UITableView然后UILablesUITableView's单元格中显示。我怎样才能UILabel's根据文本调整内容。实际上文本不是静态的。它可能在运行时发生变化。所以我需要将动态大小设置为 UILabel 。其次假设如果文本是null那么它不应该显示任何空间,并且下一个UILabel应该立即开始。我该怎么做有可能吗?这是我的代码..

self.tableView=[[UITableView alloc] initWithFrame:CGRectMake(0,150,320,800) style:UITableViewStylePlain];
            self.tableView.delegate=self;
            self.tableView.dataSource=self;
            self.tableView.scrollEnabled = NO;
            [self.view addSubview:self.tableView];

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

        UITableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        if(cell == nil)
        {
            cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault    reuseIdentifier:CellIdentifier]autorelease];
        }
        NSMutableDictionary *d = (NSMutableDictionary *) [arr2 objectAtIndex:indexPath.row];
        cell.accessoryType= UITableViewCellAccessoryDetailDisclosureButton;
       UILabel  *name1= [[UILabel alloc]initWithFrame:CGRectMake(10, 5, 320, 40)];
        name1.font=[UIFont boldSystemFontOfSize:16];
        [name1 setTextAlignment:UITextAlignmentLeft];
        [name1 setText:[d valueForKey:@"Name"]];
        name1.tag = 111;
        [cell addSubview:name1];
        [name1 release];

        UILabel *codetype=[[UILabel alloc]initWithFrame:CGRectMake(10, 45, 320,15)];
        codetype .font=[UIFont boldSystemFontOfSize:12];        
        [codetype setTextAlignment:UITextAlignmentLeft];
        [codetype  setText:[d valueForKey:@"Code"]];
        codetype.tag=112;
        [cell addSubview:codetype];
        [codetype  release];    

        UILabel *id=[[UILabel alloc]initWithFrame:CGRectMake(10, 68, 320,10)];
        id .font=[UIFont fontWithName:@"arial" size:12];        
        [id setTextAlignment:UITextAlignmentLeft];
        [id  setText:[d valueForKey:@"ID"]];
        id.tag=113;
        [cell id ];
        [id  release]; 

        UILabel *address=[[UILabel alloc]initWithFrame:CGRectMake(10, 85, 320,10)];
        address .font=[UIFont fontWithName:@"arial" size:12];        
        [address setTextAlignment:UITextAlignmentLeft];
        [address  setText:[d valueForKey:@"address"]];
        line2.tag=114;
        [cell addSubview: address];
        [address  release]; 

        UILabel *city = [[UILabel alloc]initWithFrame:CGRectMake(10, 105, 320, 10)];
        city .font=[UIFont fontWithName:@"arial" size:12];        
        [city  setTextAlignment:UITextAlignmentLeft];
        [city  setText:[d valueForKey:@"City"]];
        city.tag=115;
        [cell addSubview:city ];
        [city  release];
}

在这里,我已将所有 UILabel 设置为静态大小。但我需要的是动态大小。假设如果地址标签为空,那么它不应该显示任何空间,下一个标签,即城市应该在 id 之后立即开始..怎么样可能的?

4

3 回答 3

0

我在我的代码中完成了这项任务....您可以使用此代码并根据您的代码进行更改....如果您遇到任何问题,请告诉我。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    NSMutableDictionary *dicNotification =[[NSMutableDictionary alloc]initWithDictionary: [myNotification objectAtIndex:indexPath.row]];

    //code to get notification text height
    NSString *text = [dicNotification  objectForKey:@"message"];
    CGSize constraint = CGSizeMake(175, 100.0f);
    CGSize size = [text sizeWithFont: [UIFont fontWithName:@"Verdana" size:12] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
    return size.height;
}

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) 
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

        //label for message
        UILabel *lblMsg = [[UILabel alloc] initWithFrame:CGRectZero];
        lblMsg.tag = 12;
        lblMsg.backgroundColor = [UIColor clearColor];
        [lblMsg setFont:[UIFont fontWithName:@"Verdana" size:12]];
        [lblMsg setLineBreakMode:UILineBreakModeWordWrap];

        [cell addSubview:lblMsg];
    }

    NSMutableDictionary *dicNotification =[[NSMutableDictionary alloc]initWithDictionary: [myNotification objectAtIndex:indexPath.row]];

    //get the message height set frame height to imageView(BubbleMid & Top) & labelMSG
    NSString *txt = [dicNotification  objectForKey:@"message"];
    CGSize constraint = CGSizeMake(175, 2000.0f);
    CGSize size = [txt sizeWithFont: [UIFont fontWithName:@"Verdana" size:12] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];

    //set frame height to labelMsg
    UILabel *lblMsg = (UILabel *)[cell viewWithTag:12];
    lblMsg.frame = CGRectMake(15,25, 175, size.height);
    [lblMsg setNumberOfLines:size.height/16];

    //set labelMsg text
    lblMsg.text = [dicNotification  objectForKey:@"message"];

    return cell;
}
于 2012-11-23T14:27:19.483 回答
0

您可以UILable使用以下代码调整内容

CGSize sizeToMakeLabel = [name1.text sizeWithFont: name1.font]; 
name1.frame = CGRectMake(name1.frame.origin.x, name1.frame.origin.y, 
sizeToMakeLabel.width, sizeToMakeLabel.height); 

在这里设置第二个标签,如下所示..

更新

     if([[d valueForKey:@"address"] isEquelToString:@""]){
          UILabel *city=[[UILabel alloc]initWithFrame:CGRectMake(10, idLable.frame.origin.y + id.frame.size.height+10 , 320,15)];
          city .font=[UIFont fontWithName:@"arial" size:12];        
          [city  setTextAlignment:UITextAlignmentLeft];
          [city  setText:[d valueForKey:@"City"]];
          city.tag=115;
          [cell addSubview:city ];
          CGSize sizeToMakeLabel = [city.text sizeWithFont: city.font]; 
          city.frame = CGRectMake(city.frame.origin.x, city.frame.origin.y, 
sizeToMakeLabel.width, sizeToMakeLabel.height); 
        }
      else{
          UILabel * address =[[UILabel alloc]initWithFrame:CGRectMake(10, idLable.frame.origin.y + id.frame.size.height+10 , 320,15)];
address .font=[UIFont fontWithName:@"arial" size:12];        
          [address setTextAlignment:UITextAlignmentLeft];
          [address  setText:[d valueForKey:@"address"]];
          line2.tag=114;
          [cell addSubview: address];
          CGSize sizeToMakeLabel = [address.text sizeWithFont: address.font]; 
          address.frame = CGRectMake(address.frame.origin.x, address.frame.origin.y, 
sizeToMakeLabel.width, sizeToMakeLabel.height); 
        }

等等每个标签..

对整个标签遵循这个逻辑

我希望这可以帮助你...

于 2012-11-23T08:01:31.840 回答
0

问题是您必须使用 heightForRowAtIndexPath。问题是它在 cellForRowAtIndexPath 之前被调用,因此文本不在组件中,您不能简单地将组件大小调整为文本。因此,您所做的是使用 sizeWithFont 计算文本大小。

普通/标准文本的情况下

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //here you get your text string
    NSString* theText =  ... ; 

    //then calculate the size. systemFontOfSize depends on your font size
    //yourLabelWidth must be known and usually depends on scree size or if the tableview has an index
    CGSize textSize = [theText sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(yourLabelWidth, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];

    return textSize.height;
}

属性文本的情况下

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //here you get your text string
    NSAttributedString* theText =  ...;

    //then calculate the size.
    //yourLabelWidth must be known and usually depends on scree size or if the tableview has an index
    CGRect rectSize = [theText boundingRectWithSize:CGSizeMake(yourLabelWidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin context:NULL];

return rectSize.size.height;

    return textSize.height;
}
于 2012-11-23T08:05:32.497 回答