iOS dev
iOS dev

Reputation: 2284

How to set position of textLabel in a tableview cell iphone


 if (cell == nil) 
        {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier] autorelease];

     cell.textLabel.text = @"Hai";
                cell.detailTextLabel.text = @"Hello";

        cell.detailTextLabel.textAlignment = UITextAlignmentLeft;
        cell.textLabel.textAlignment = UITextAlignmentLeft;
        return cell;

        }

Upvotes: 1

Views: 11734

Answers (3)

Dipen Chudasama
Dipen Chudasama

Reputation: 3093

You have to create custom cell for this. like this,

in CustomCellTableViewCell.h file

#import <UIKit/UIKit.h>

@interface CustomCellTableViewCell : UITableViewCell
@property(nonatomic,strong)UILabel *lblUnit;
@end
in CustomCellTableViewCell.m file
#import "CustomCellTableViewCell.h"

@implementation CustomCellTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    self.userInteractionEnabled = YES;
    if (self) {
        // Initialization code
        self.lblUnit = [[UILabel alloc]init];
        [self.lblUnit setTextColor:[UIColor whiteColor]];
            [self.lblUnit setFont:[UIFont fontWithName:@"Roboto-Light" size:30.0f]];
       // [self.lblUnit setFont:[UIFont systemFontOfSize:18.0]];
        self.lblUnit.textAlignment = UIBaselineAdjustmentAlignCenters;
        self.lblUnit.adjustsFontSizeToFitWidth = YES;
        [self.contentView addSubview:self.lblUnit];
       }
    return self;
}

@end
in your Tableview CellForRowAtIndexpath write below code
      cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];

    if(cell==nil){
        cell = [[CustomCellTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
    }

     cell.lblUnit.frame = CGRectMake(0, 10, 150, 40);
     cell.lblUnit.text = @"Hai";
     return cell;

Upvotes: -6

Shankar Aware
Shankar Aware

Reputation: 178

NSTextAlignmentCenter does work if you use initWithStyle:UITableViewCellStyleDefault.

Upvotes: 0

Jakob W
Jakob W

Reputation: 3377

Create a custom UITableViewCell subclass and override the layoutSubviews method.

Upvotes: 8

Related Questions