M.Ali El-Sayed
M.Ali El-Sayed

Reputation: 1779

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

Upvotes: 4

Views: 4567

Answers (6)

hbk
hbk

Reputation: 11184

actually in this case the best approach - create your own cell and manage it. But in case you need such functionality you could try to do something like

cell?.transform = CGAffineTransform(scaleX:-1, y:1)
cell?.textLabel?.transform = CGAffineTransform(scaleX:-1, y:1)
cell?.detailTextLabel?.transform = CGAffineTransform(scaleX:-1, y:1)

This will invert "visible" part of cell as you require.

Upvotes: 0

k.mand
k.mand

Reputation: 3

I used the below Button action. Button is placed out side the tableview

- (IBAction)rightBtn:(id)sender {

cell.textLabel.textAlignment=UITextAlignmentRight;
  }

Upvotes: 0

M.Ali El-Sayed
M.Ali El-Sayed

Reputation: 1779

The only solution to solve this problem is to create custuom cell first create MTTableViewCell.h


#import <UIKit/UIKit.h>

@interface MTTableViewCell : UITableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier;
@end

then create MTTableViewCell.m


#import "MTTableViewCell.h"

@implementation MTTableViewCell


- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}


- (void) layoutSubviews {
[super layoutSubviews];
//self.textLabel.frame = CGRectMake(200, 0, 310, 20);
self.detailTextLabel.textAlignment = NSTextAlignmentRight;
self.textLabel.textAlignment = NSTextAlignmentRight;

//self.textLabel.frame = CGRectMake(100, 0, 310, 20);
CGRect adjustedFrame = self.textLabel.frame;
adjustedFrame.origin.x -= 10.0f;
self.textLabel.frame = adjustedFrame;
adjustedFrame = self.detailTextLabel.frame;
adjustedFrame.origin.x -= 10.0f;
self.detailTextLabel.frame = adjustedFrame;


{
CGRect frame = self.textLabel.frame;
frame.size.width = CGRectGetWidth(self.frame);

if(self.accessoryView != nil){
if ( IDIOM == IPAD ) {
frame.size.width = frame.size.width - 210;
} else {
frame.size.width = frame.size.width - 120;
}

frame.origin.x +=30;
}
else{
frame.size.width = frame.size.width - 50;
frame.origin.x += 30.0;
}
self.textLabel.frame = frame;
}

{
CGRect frame = self.detailTextLabel.frame;
frame.size.width = CGRectGetWidth(self.frame);

if(self.accessoryView != nil){
if ( IDIOM == IPAD ) {
frame.size.width = frame.size.width - 210;
} else {
frame.size.width = frame.size.width - 120;
}
frame.origin.x += 30.0;
}
else{
frame.size.width = frame.size.width - 50;
frame.origin.x += 30.0;
}
self.detailTextLabel.frame = frame;
}


}

@end

then in your cell

cell = [[[MTTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
[cell.textLabel setNumberOfLines:2];
[cell.detailTextLabel setNumberOfLines:3];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];

that is

Upvotes: 0

Aruna kumari
Aruna kumari

Reputation: 319

better to to use custom label and add it to cell.contentView as following below

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];

if (cell == nil) 
{

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MyIdentifier"] ;

  }

label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
label.textAlignment = NSTextAlignmentRight;
[cell.contentView addSubview:label];
label.backgroundColor = [UIColor purpleColor];
 label.textColor = [UIColor blackColor];
label.text = [titlesArray objectAtIndex:indexPath.row];

return cell;

}

Upvotes: 0

Mayank Jain
Mayank Jain

Reputation: 5754

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.

Upvotes: 2

Anbu.Karthik
Anbu.Karthik

Reputation: 82759

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

Upvotes: 1

Related Questions