开发者

resize UITableViewCell textLabel

开发者 https://www.devze.com 2022-12-15 07:56 出处:网络
I have a UITableViewCell that I would like to add a view to the right (in addition to the accessory view). I tried setting th开发者_如何学编程e size of textLabel to be a few pixels narrower but it jus

I have a UITableViewCell that I would like to add a view to the right (in addition to the accessory view). I tried setting th开发者_如何学编程e size of textLabel to be a few pixels narrower but it just resizes it back.

Is there any way to resize textLabel?


Actually it CAN be resized if you create an UITableViewCell subclass and override the layoutSubviews method:

- (void)layoutSubviews {
[super layoutSubviews];  //The default implementation of the layoutSubviews

CGRect textLabelFrame = self.textLabel.frame;
textLabelFrame.size.width = _textLabelMaxWidth;
self.textLabel.frame = textLabelFrame;
}

Hope it helps.


The object referenced by the default textLabel property, in UITableViewCell instances of type UITableViewCellStyleDefault, cannot be resized, at least not in my experience. The best idea in these cases is to create your own subclass of UITableViewCell, in code or even with Interface Builder if you want, and give it the layout that you want.


I've noticed that the textLabel resizes to accomodate the accessoryView. So if you want the label to use the entire cell, you could set accessoryView to nil (I think this is the default behavior anyway). Or, for example, if you want the label to take only the left half the cell, make accessoryView be an empty UIView that spans the right half the cell. (Doesn't have to be empty, you can put stuff in there...my point is that it WILL shrink the textLabel.)


You can do resizing UITableView Cell in this simplest way.First,take a look at my sample project.

ResizableUIView

It will show you how you can resize uitableview cell base on its inner component in simplest way by creating constraints.

    tblTest.estimatedRowHeight = 100 // This is where you can set your estimated row height
    tblTest.rowHeight = UITableViewAutomaticDimension

Another important fact is your label lines :

resize UITableViewCell textLabel

it should be 0

In order to make you understand,I set UILabel inside UIView,take a look at how I created constraints on its parent UIView.

If you still need any help,just ask.

Good Luck

0

精彩评论

暂无评论...
验证码 换一张
取 消