在cell.detailTextLabel.text上格式化

时间:2012-08-25 19:50:07

标签: objective-c format subtitle

我在单元格字幕中存在格式问题。 我有一个字符串和一个我希望在单元格的副标题中显示的数字

字符串应左对齐,数字右对齐

我以这种方式尝试过:

rangeAutor = [rangeAutor stringByPaddingToLength:40-[rangeAutor length] withString:@" " startingAtIndex:0];

NSString *subTitle = [NSString stringWithFormat:@"by %@ %1.2f km", rangeAutor, rangeDistance];
cell.detailTextLabel.text = subTitle;

结果你能在这里看到:-( format problem

您对我的解决方案有什么想法吗?

由于我只有7个声誉的事实,我不能写评论我的答案:-( 所以我试着在这里做:

    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;

抢劫mayoff

我只改变了一些小东西 但现在我无法在单元格中看到rangeDistance :-( 日志输出没问题

2 个答案:

答案 0 :(得分:2)

问题是您使用的是可变宽度字体。字体中的每个字形可以具有不同的宽度。因此,您不能将autor填充到40个字符,因为“llll”后跟36个空格的宽度(在屏幕上)与“MMMM”后面的36个空格不同。

只需给你的cell另一个标签保持距离。我假设您帖子中的代码来自您的tableView:cellForRowAtIndexPath:方法。你需要做这样的事情:

- (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;
}

答案 1 :(得分:0)

尝试使用常量值而不是40-[rangeAutor length]