Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

align the text in UITableViewCell to the right

I need to align the text in the cell to right.

but even when i set to right the text is align to left as show in the screen shot any idea why is this strange behaviour of the cell

cell.textLabel.textAlignment = NSTextAlignmentRight;
cell.detailTextLabel.textAlignment = NSTextAlignmentRight;

After reading related questions and answers on stackoverflow I could not find one solving my problem

enter image description here

like image 1000
M.Ali El-Sayed Avatar asked Oct 22 '25 00:10

M.Ali El-Sayed


2 Answers

No can not change the frame of titleLabel of Cell. Instead you can add custom label to contentView of cell.

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * cellIdentifier = @"Cell";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

if (cell==nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

    UILabel *lblStatus = [[UILabel alloc] initWithFrame:CGRectMake(12, 8, 175, 15)];
    lblStatus.backgroundColor = [UIColor clearColor];
    [lblStatus setTag:101];
    [cell.contentView addSubview:lblStatus];

 }
UILabel *lbl1 = (UILabel *)[cell.contentView viewWithTag:101];
lbl1.text=@"set text";
}
return cell;
}

You can also achieve this by prototype cell. here is a good tutorial of prototype cell.

like image 94
Mayank Jain Avatar answered Oct 23 '25 15:10

Mayank Jain


your coding is fine , u need to change the cell.textLabel. frame size to right side in my choice is use custom label

like image 35
Anbu.Karthik Avatar answered Oct 23 '25 14:10

Anbu.Karthik