Reputation: 4436
I have a grouped style Tableview which have uilabels in Tableviewcell. Now i want to set height of uilabels equal to height of cell how can i do ths???
- (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];
}
Upvotes: 0
Views: 320
Reputation: 288
just set the cell bounds to label's frame,like below.
UILabel *myLabel = [[UILabel alloc] initWithFrame:cell.bounds];
Upvotes: 0
Reputation: 7193
// try this code
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(95,1,140,cell.frame.size.height)];
Upvotes: 0
Reputation: 3663
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)];
Upvotes: 1
Reputation: 6557
In the cellForRowAtIndexPath add; 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)];
Upvotes: 1
Reputation: 947
I think there's two things you can do; first try reading this link from apple and review your approach. If you only need a label, why not use the cells textLabel
and customize its appearance?
If you really want to use the label approach then consider adding it to the contentView instead of the cell. You can also try to get the bounds of this property, instead of the c
UILabel *category = [[UILabel alloc] initWithFrame:CGRectMake(0,0,width,cell.contentView.bounds.size.height)];
[cell.contentView addSubview:category];
Hope this helps.
Upvotes: 0
Reputation: 1476
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
}
Upvotes: 0
Reputation: 5081
- (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...
Upvotes: 0
Reputation: 1351
If you want your cell's height, just do the following:
category.frame = CGRectMake(0,0,width,cell.bounds.size.height);
Upvotes: 0