让我们缩短 cell.textLabel 的长度

发布于 2025-01-05 17:34:24 字数 533 浏览 2 评论 0原文

我有一个带有一些表格的应用程序。

此表中有两个标签 - 带标题的 textLabel 和带日期的 detailTextLabel。但是当标题很长时,它会显示在 detailTextLabel 上。

我该如何解决这个问题?

附注
我尝试以这种方式重写 layoutSubviews 方法:

- (void)layoutSubviews {
    [super layoutSubviews];

    CGSize size = self.bounds.size;
    CGRect frame = CGRectMake(4.0f, 4.0f, size.width, size.height); 
    self.textLabel.frame =  frame;
    self.textLabel.contentMode = UIViewContentModeScaleAspectFit;
}

但它不起作用

I got an app with some tables.

In this table there are two labels - the textLabel with titles and the detailTextLabel with dates. But when title is really long, it is showed over the detailTextLabel.

How can I solve this problem?

P.S.
I tried to override layoutSubviews method in this way:

- (void)layoutSubviews {
    [super layoutSubviews];

    CGSize size = self.bounds.size;
    CGRect frame = CGRectMake(4.0f, 4.0f, size.width, size.height); 
    self.textLabel.frame =  frame;
    self.textLabel.contentMode = UIViewContentModeScaleAspectFit;
}

But it doesn't work

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浮萍、无处依 2025-01-12 17:34:24

首先我创建空单元格。然后添加 UILabel 作为带有标签的子视图。并更改 UILabel 的框架

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Recent Dream";

    UITableViewCell *cell;// = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

        NSLog(@"new cell");

        UILabel *textLabel = [[UILabel alloc] init];
        textLabel.frame = CGRectMake(10, 0, self.view.frame.size.width -110, 48);
        textLabel.backgroundColor = [UIColor clearColor];
        textLabel.font = [UIFont boldSystemFontOfSize:16];
        [textLabel setTag:1];
        [cell.contentView addSubview:textLabel];
    }

    // Configure the cell...

    Dream *tempDream = [self.dreams objectAtIndex:indexPath.row];

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"MM/dd/yyyy"];

    //Optionally for time zone converstions
    [formatter setTimeZone:[NSTimeZone timeZoneWithName:@"..."]];

    NSString *stringFromDate = [formatter stringFromDate:tempDream.dateAdd];

    UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"bodyWrapper.png"]];

    cell.contentView.backgroundColor = background;

    UILabel *textLabel = (UILabel *)[cell.contentView viewWithTag:1];

    textLabel.text = tempDream.title;

    cell.textLabel.text = @"";
    cell.detailTextLabel.text = stringFromDate;

    return cell;
}

Firstly i create empty cell. Then add there UILabel as subView with tag. And change UILabel's frame

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Recent Dream";

    UITableViewCell *cell;// = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

        NSLog(@"new cell");

        UILabel *textLabel = [[UILabel alloc] init];
        textLabel.frame = CGRectMake(10, 0, self.view.frame.size.width -110, 48);
        textLabel.backgroundColor = [UIColor clearColor];
        textLabel.font = [UIFont boldSystemFontOfSize:16];
        [textLabel setTag:1];
        [cell.contentView addSubview:textLabel];
    }

    // Configure the cell...

    Dream *tempDream = [self.dreams objectAtIndex:indexPath.row];

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"MM/dd/yyyy"];

    //Optionally for time zone converstions
    [formatter setTimeZone:[NSTimeZone timeZoneWithName:@"..."]];

    NSString *stringFromDate = [formatter stringFromDate:tempDream.dateAdd];

    UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"bodyWrapper.png"]];

    cell.contentView.backgroundColor = background;

    UILabel *textLabel = (UILabel *)[cell.contentView viewWithTag:1];

    textLabel.text = tempDream.title;

    cell.textLabel.text = @"";
    cell.detailTextLabel.text = stringFromDate;

    return cell;
}
若能看破又如何 2025-01-12 17:34:24
float newWidth = 30; //to try increase the value, while all will be right.
CGRect frame = CGRectMake(4.0f, 4.0f, size.width - newWidth, size.height);
float newWidth = 30; //to try increase the value, while all will be right.
CGRect frame = CGRectMake(4.0f, 4.0f, size.width - newWidth, size.height);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文