开发者

UITableViewCell textLabel offset

开发者 https://www.devze.com 2023-02-25 16:22 出处:网络
I want to have my text in my UITableViewCell to be a little bit to the right. In other words I want to have an x-offset. Is this even possble? Do I have to create a cu开发者_如何学Cstom cell just beca

I want to have my text in my UITableViewCell to be a little bit to the right. In other words I want to have an x-offset. Is this even possble? Do I have to create a cu开发者_如何学Cstom cell just because of this?


You could use cell.indentationLevel and if needed cell.indentationWidth instead of custom UITableViewCell for this.


You can try this :

[cell setIndentationLevel:SOME_NUMBER];
[cell setIndentationWidth:SOME_OTHER_NUMBER];


A simple solution is you can change the frame of the textLabel.

CGRect textLabelFrame = cell.textLabel.frame;
textLabelFrame.origin.x += xOffset;
textLabelFrame.size.width -= xOffset;
cell.textLabel.frame = textLabelFrame;

I've also done this by creating a custom UILabel that supports edgeInsets similar to a UIButton. This is a better solution b/c you can layout the label to correct size but the above will work if you have simple needs.

[EDIT 1/2: fixed typo w/ CGRect]

[EDIT 3: fixed typo setting modified frame]

[EDIT 4: need a simple subclass]

Mea culpa. I was wrong that you can do this in tableView:cellForRowAtIndexPath. The UITableViewCell layout happens after the tableView delegate/datasource has a chance to customize the cell. I've tested the implementation below, it works.

Do as I say above but create a (simple) subclass of UITableViewCell that adds the xOffset in layoutSubviews. If you do this, you can also add an xOffset property that you can set in tableView:cellForRowAtIndexPath:.

@implementation XOffsetCell
// assumes property xOffset is defined and synthesized
- (void) layoutSubviews
{
    [super layoutSubviews];
    CGRect textLabelFrame = cell.textLabel.frame;
    textLabelFrame.origin.x += self.xOffset;
    textLabelFrame.size.width -= self.xOffset;
    cell.textLabel.frame = textLabelFrame;
}
@end

The solution that suggested adding a custom UILabel to cell.contentView is also a good solution. I saw your comment that it obscures the built-in textLabel but that's the whole point. You would not use the built-in label anymore, use the custom label instead.


I'm not trolling for votes, but wanted to show what the code provided by @iPhone monster "should" look like. His solution is a valid option. If you add the label to the cell after the if (cell == nil) as he did you will be continually adding labels to dequeued cells.

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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
        UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(50.0f, 10.0f, 150.0f, 20.0f)];
        lbl.tag = OffsetLabelTag; // define this as a constant
        [cell.contentView addSubview:lbl];
        [lbl release];
    }

    UILabel *lbl = (UILabel *)[cell.contentView viewWithTag:OffsetLabelTag];
    [lbl setText:@"test text"];

    return cell;
}


You can do that with Autolayouts:

UITableViewCell *cell = [UITableViewCell new];
cell.textLabel.translatesAutoresizingMaskIntoConstraints = NO;
[cell.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(indentation)-[textLabel]-(indentation)-|"
                                                                         options:0
                                                                         metrics:@{@"indentation": @35}
                                                                           views:@{@"textLabel": cell.textLabel}]];

Or using Parus lib:

[cell.contentView addConstraints:(PVVFL(@"H:|-(indentation)-[textLabel]-(indentation)-|")
                                  .withViews(@{@"textLabel": textLabel})
                                  .metrics(@{@"indentation": @35}).asArray)];


Other way:


- (NSInteger)tableView:(UITableView *)tableView
indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath
{
return intOffset;  // but is like '\t', not points
}


There is another not very pretty and smart way : ) Position of Label in TableViewCell depends on size of image assigned to cell.imageView.image property. So if you want your text to move right you can add several blank pixel columns to image in cell.


Based off of https://stackoverflow.com/a/5659826/1058199, here is a Swift version.

This works within the cell to ONLY move the textLabel to the right. Swift 5.4

override func layoutSubviews() {
    super.layoutSubviews()
    var textLabelFrame = self.textLabel?.frame
    textLabelFrame?.origin.x += 32
    textLabelFrame?.size.width -= 32
    self.textLabel?.frame = textLabelFrame ?? CGRect.zero
}


Will cell.textLabel.textAlignment = UITextAlignmentCenter; be enough for you. It could align the text to the center creating that space for you

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号