【问题标题】:how to resize textlabel in a tableview cell如何在表格视图单元格中调整文本标签的大小
【发布时间】:2012-05-06 01:57:30
【问题描述】:

我正在编写一个包含 RSS 阅读器的应用程序。 RSS 阅读器正在下载标题、描述和图像。我放置在与描述相同的单元格中的附件视图中的图像。描述放置在文本标签中,并根据图像完美调整大小。但我希望图像显示在左侧。但是当我从附件视图中删除图像并将其移动到左侧的图像时,T​​extlabel 不会调整大小。

如何调整文本标签的大小。

CGRect f = cell.textLabel.frame;
[cell.textLabel setFrame: CGRectMake(f.origin.x-20, f.origin.y, f.size.width-50, f.size.height)];

cell.textLabel.font = [UIFont systemFontOfSize:11];
cell.textLabel.numberOfLines =3;
cell.textLabel.textColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.7];
cell.backgroundColor = [UIColor clearColor];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.selectionStyle = UITableViewCellSelectionStyleNone;

我尝试使用 setframe 函数调整它的大小,但不起作用。

希望你们中的一些人可以将我推向正确的方向。

提前致谢

【问题讨论】:

    标签: ios imageview tableview cell textlabel


    【解决方案1】:

    您需要继承 UITableViewCell 类。这是一个例子:

    .h 文件:

    #import <UIKit/UIKit.h>
    
    @interface MyCustomerCell : UITableViewCell
    
    @end
    

    .m 文件:

    #import "MyCustomerCell.h"
    
    @implementation MyCustomerCell
    
    - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
    {
        self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
        if (self) {
            // Initialization code
        }
        return self;
    }
    
    - (void)setSelected:(BOOL)selected animated:(BOOL)animated
    {
        [super setSelected:selected animated:animated];
    
        // Configure the view for the selected state
    }
    
    - (void)layoutSubviews {
        [super layoutSubviews];
        self.imageView.frame = CGRectMake(0,0,150,90);
        self.imageView.bounds = CGRectMake(0, 0, 150, 90);
        self.textLabel.frame = CGRectMake(250, 0, 200, 40);   //change this to your needed
        self.detailTextLabel.frame = CGRectMake(250, 40, 200, 40);
    }
    

    @结束

    【讨论】:

    • 我如何在我的 tableview 中实现它?
    • 在你有tableview的类中: 1. 导入头文件:#import "MyCustomerCell.h"。 2. 任何你定义单元格的地方: MyCustomerCell *cell = ... 而不是普通的 UITableViewCell *cell = ...
    • 谢谢伙计 - 解决了我的问题 :)
    • 为什么我们需要这样做?没有人可以正常访问 UITableViewCell 类的框架或边界吗?
    猜你喜欢
    • 1970-01-01
    • 2016-02-02
    • 1970-01-01
    • 1970-01-01
    • 2019-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多