Is there a way开发者_StackOverflow中文版 one can change the spacing between textLabel and detailTextLabel in a UITableViewCell ? (without subclassing UITableViewCell)
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™.
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.
精彩评论