Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

numberOfLines of UILabel doesn't work within cell

All,

I am very new to iphone programming. In the following code, I want the text to show all of the text within the comment label but right now it is truncating it. numberofLines is not working right either. Right now it is doing this. "My name is Fred and I aint dead..." but I want it to display the full text "My name is Fred and I aint dead yet so let me live" even if it has to be on multiple lines.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 80.0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = nil;

cell = [self.allCells objectForKey:[NSNumber numberWithInt:indexPath.row]];

if(!cell)
{
    cell = [[[NSBundle mainBundle] loadNibNamed:@"UserCell2" owner:nil options:nil] lastObject];
    cell.backgroundColor = [UIColor clearColor];
    cell.accessoryType = UITableViewCellAccessoryNone;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [self.allCells setObject:cell forKey:[NSNumber numberWithInt:indexPath.row]];
}

GSAsynImageView *imgView = (GSAsynImageView*)[cell viewWithTag:1000];
UILabel *lblTitle = (UILabel*)[cell viewWithTag:1001];
UILabel *lblComment = (UILabel*)[cell viewWithTag:1003];
UILabel *lbltime = (UILabel*)[cell viewWithTag:1004];

//lblComment setLineBreakMode:NSLineBreakByWordWrapping];
//lblComment.numberOfLines = 0;
//lblComment.lineBreakMode = UILineBreakModeCharacterWrap;

if(self.arrComments.count==0)
{
    imgView.hidden = YES;
    lblTitle.text = nil;
    lblComment.text = nil;
    lbltime.text = nil;
    if(indexPath.row==1)lblTitle.text = @"No comments yet";
}
else
{
    imgView.hidden = NO;

    NSDictionary *dcUser = [self.arrComments objectAtIndex:indexPath.row];

    NSString *strBio = [dcUser objectForKey:@"CommentTxt"];
    NSString *strDisplayName = [dcUser objectForKey:@"CommenterDisplayName"];

    NSString *imgName = [dcUser objectForKey:@"ImageName"];
    NSString *usernamex = [dcUser objectForKey:@"CommenterUserName"];

    if([imgName isKindOfClass:[NSString class]])
    {
        if([imgName rangeOfString:@"facebook"].location!=NSNotFound || [imgName rangeOfString:@"twimg"].location!=NSNotFound)
            [imgView loadImageFromPath:imgName];
        else
            [imgView loadImageFromPath:[NSString stringWithFormat:@"%@images/%c/%@/50x50%@",WEBSERVER,[usernamex characterAtIndex:0],usernamex,imgName]];
    }

    lblTitle.text = strDisplayName;
    lblComment.text = strBio;
    lbltime.text = [self getDateTitle:[dcUser objectForKey:@"Date"]];

}

return cell;
}
like image 896
nawlrus Avatar asked Feb 19 '13 05:02

nawlrus


2 Answers

try this bellow code and add in your cell..

UILabel * lblTitle = [[UILabel alloc]init];
[lblTitle setFrame:CGRectMake(110, 31, 200, 50)];        
lblTitle.text = @"your Text ";
lblTitle.lineBreakMode = UILineBreakModeWordWrap;// add this line 
lblTitle.numberOfLines = 0;// add this  line
lblTitle.font = [UIFont fontWithName:@"Helvetica" size:12];

for more detail see my blog with this post from THIS link

like image 54
Paras Joshi Avatar answered Oct 02 '22 07:10

Paras Joshi


try this...

UILabel * label = [[UILabel alloc]init];
    [label setFrame:CGRectMake(cell.frame.origin.x , cell.frame.origin.y, cell.frame.size.width, cell.frame.size.height)];
    label.text = @" Text to be displayed in label ";
    label.lineBreakMode = UILineBreakModeWordWrap ;// Wrap at word boundaries
    label.numberOfLines = 0;// this line include multiple lines
    [cell addSubview:label];
like image 25
Vaisakh Avatar answered Oct 02 '22 05:10

Vaisakh