format at cell.detailTextLabel.text
-
28-06-2021 - |
Question
I hava a format problem in cell-subtitle. I have a string and a number I want to show in subtitle of a cell
String should be left aligned, number right aligned
I tried it in that way:
rangeAutor = [rangeAutor stringByPaddingToLength:40-[rangeAutor length] withString:@" " startingAtIndex:0];
NSString *subTitle = [NSString stringWithFormat:@"by %@ %1.2f km", rangeAutor, rangeDistance];
cell.detailTextLabel.text = subTitle;
result can you see here :-( format problem
Do you have an idea for me for a solution?
due to the fact I only have 7 reputation, i cant write in comments my answer :-( So i try to do it here:
static NSString *const kIdentifier = @"SelectRange";
static NSInteger const kDistanceLabelTag = 1;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kIdentifier];
UILabel *distanceLabel;
if (cell) {
distanceLabel = (UILabel *)[cell.contentView viewWithTag:kDistanceLabelTag];
} else {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:kIdentifier];
CGRect cellBounds = cell.bounds;
static CGFloat const kLabelHeight = 20.0f;
distanceLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, cellBounds.size.height - kLabelHeight, cellBounds.size.width, kLabelHeight)];
distanceLabel.tag = kDistanceLabelTag;
distanceLabel.font = cell.textLabel.font;
distanceLabel.textColor = cell.textLabel.textColor;
distanceLabel.textAlignment = UITextAlignmentRight;
distanceLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
[cell.contentView addSubview:distanceLabel];
}
UIImage *icon = [UIImage imageNamed:iconName];;
cell.imageView.image = icon;
cell.textLabel.text = rangeName;
cell.detailTextLabel.text = [@"by " stringByAppendingString:rangeAutor];
distanceLabel.text = [NSString stringWithFormat:@"%1.2f km", rangeDistance];
NSLog(@"RangeDistance: %1.2f", rangeDistance);
return cell;
to rob mayoff
I changed only small things but now I cant see rangeDistance in the cell :-( output of log is ok
Solution
The problem is you're using a variable-width font. Each glyph in the font may have a different width. So you can't just pad the autor out to 40 characters, because “llll” followed by 36 spaces is a different width (on screen) than “MMMM” followed by 36 spaces.
Just give your cell
another label to hold the distance. I assume the code in your post is from your tableView:cellForRowAtIndexPath:
method. You'll need to do something like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *const kIdentifier = @"Cell";
static NSInteger const kDistanceLabelTag = 1;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kIdentifier];
UILabel *distanceLabel;
if (cell) {
distanceLabel = (UILabel *)[cell.contentView viewWithTag:kDistanceLabelTag];
} else {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:kIdentifier];
CGRect cellBounds = cell.bounds;
static CGFloat const kLabelHeight = 20.0f;
distanceLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, cellBounds.size.height - kLabelHeight, cellBounds.size.width, kLabelHeight)];
distanceLabel.tag = kDistanceLabelTag;
distanceLabel.font = cell.textLabel.font;
distanceLabel.textColor = cell.textLabel.textColor;
distanceLabel.textAlignment = UITextAlignmentRight;
distanceLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
[cell.contentView addSubview:distanceLabel];
}
cell.imageView.image = rangeImage;
cell.textLabel.text = rangeName;
cell.detailTextLabel.text = [@"by " stringByAppendingString:rangeAutor];
distanceLabel.text = [NSString stringWithFormat:@"%1.2f km", rangeDistance];
return cell;
}
OTHER TIPS
Try using a constant value instead of 40-[rangeAutor length]