Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the spacing between textLabel and detailTextLabel in a UITableViewCell?

Is there a way one can change the spacing between textLabel and detailTextLabel in a UITableViewCell ? (without subclassing UITableViewCell)

like image 798
NSExplorer Avatar asked Nov 28 '25 20:11

NSExplorer


2 Answers

Create a custom subclass of UITableViewCell, and implement the -layoutSubviews method to do this:

- (void) layoutSubviews {
  [super layoutSubviews];
  //my custom repositioning here
}

If you wanted to do it without subclassing, you could do it via method swizzling, but on the whole, that's a Bad Idea™.

like image 169
Dave DeLong Avatar answered Nov 30 '25 10:11

Dave DeLong


This can be done without subclassing using NSAttributedString and the attributedText property like so:

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

NSAttributedString *text = [[NSAttributedString alloc] initWithString:@"Some Text"];
cell.textLabel.attributedText = text;

NSMutableParagraphStyle *subtitleParagraphStyle = [NSMutableParagraphStyle new];
subtitleParagraphStyle.minimumLineHeight = 20;

NSMutableAttributedString *subText = [[[NSAttributedString alloc] initWithString:@"Some Subtitle Text"] mutableCopy];
[subText addAttribute:NSParagraphStyleAttributeName value:subtitleParagraphStyle range:NSMakeRange(0, subText.length)];

cell.detailTextLabel.attributedText = subText;

What you're doing is forcing the line height of the subtitle to be larger than normal. Playing around with the line heights of the text and sub text should help you achieve what you want. Should be compatible with iOS 7+.

A few years too late, but hopefully somebody finds it useful.

like image 39
martinp Avatar answered Nov 30 '25 08:11

martinp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!