iOS - 桌面视图中的UILabel

时间:2012-11-23 07:53:17

标签: iphone ios cocoa-touch uitableview

我对iphone的发展非常陌生,并且在某种程度上受到了打击。实际上我需要的是我在UITableView中显示一些结果,然后我显示UILables UITableView's个单元格。{ 如何根据文本使UILabel's调整内容。实际上文本不是静态的。它可能在运行时更改。所以我需要将动态大小设置为UILabel。其次假设文本是null那么它不应该显示任何空格并立即显示下一个{{ 1}}应该开始。我怎么能让它成为可能? 这是我的代码..

UILabel

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

3 个答案:

答案 0 :(得分: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); 
        }
每个标签都有

等等。

对整个标签采用这种逻辑

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

答案 1 :(得分: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;
}

答案 2 :(得分: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;
}