Marian Busoi
Marian Busoi

Reputation: 1087

iPhone UITableViewCell: repositioning the textLabel

I am new to iPhone development and I am currently working on a simple RSS reader app. The problem I am having is that I need to reposition the textLabel inside the UITableViewCells. I have tried setFrame or setCenter but it doesn't do anything. Does anyone know what I need to do inside the tableView:cellForRowAtIndexPath: method to reposition the textLabel at the top of the cell (x = 0, y = 0)?

Thank you

PS: The UITableViewCell is referenced by a variable called cell. I have tried [cell setFrame:CGRectMake(0, 0, 320, 20)] with no success.

Upvotes: 38

Views: 33618

Answers (5)

Shalom Friss
Shalom Friss

Reputation: 193

In Swift 3 it would be

override func layoutSubviews() {
    super.layoutSubviews()
    self.textLabel?.frame.origin.x = 50
}

Upvotes: 0

Kibernetik
Kibernetik

Reputation: 3028

You may try indentationLevel, separatorInset and other content indentation properties of UITableViewCell object.

Upvotes: 24

brant
brant

Reputation: 369

The reason the original poster's code doesn't work is that it appears that the frame of the textLabel is set after the UITableViewCell has been returned from your delegate method.

I noticed that I can successfully alter many properties of the textLabel, such as the text alignment, color, font, etc, but altering the frame has no effect and when I print the frame to the debugger later (like on select), the frame isn't what I set. Therefore, I conclude that the UIKit framework is altering the frame of the textLabel after it is returned from the delegate method. No doubt this is likely done because Apple engineers wanted to make sure that your text was drawn to the screen, so they measure it and alter the frame so that it will fit. They probably figured that people such as ourselves who wanted to alter the position of the text would be able to do so by subclassing, or simply adding another UILabel (or whatever) as a subview. A novice developer might have a very hard time if his or her text didn't show up in the label or was truncated because they didn't adjust the frame.

In my case, I wanted the text to be center horizontally, to be a specific color/font/size, and to be slightly higher vertically in the cell. Being too lazy to subclass this, I first tried altering the frame. When that didn't work, I tried googling the answer (found this post).

My final solution was to set the numberOfLines property to 0 and add some trailing carriage returns to my text. Now THAT is lazy.

Upvotes: 9

wal
wal

Reputation: 2144

You can create a subclass for UITableViewCell and customize de textLabel frame. See that answer: Labels aligning in UITableViewCell. It's works perfectly to me.

It's my subclass

#import "UITableViewCellFixed.h"
@implementation UITableViewCellFixed
- (void) layoutSubviews {
     [super layoutSubviews];
     self.textLabel.frame = CGRectMake(0, 0, 320, 20);
}
@end

It's my UITableViewControllerClass:

UITableViewCellFixed *cell = (UITableViewCellFixed *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCellFixed alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

Upvotes: 100

Marian Busoi
Marian Busoi

Reputation: 1087

Seems I solved my own problem. Here's some code, in case someone runs into the same problem:

UILabel *ttitle = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 20)] autorelease];
ttitle.font = [UIFont boldSystemFontOfSize:13];
ttitle.textColor    = [UIColor blackColor];
ttitle.textAlignment = UITextAlignmentLeft;

[ttitle setText:[[stories objectAtIndex: storyIndex] objectForKey: @"title"]];

[cell.contentView addSubview:ttitle];

The idea is to create your own label object, because the textLabel is automatically positioned and can't be moved around.

Cheers.

Upvotes: 10

Related Questions