Autolayout Multiline Uilabel Cutting Off Some Text

AutoLayout multiline UILabel cutting off some text

Found an even better answer after reading this: http://johnszumski.com/blog/auto-layout-for-table-view-cells-with-dynamic-heights

creating a UILabel subclass to override layoutSubviews like so:

- (void)layoutSubviews
{
[super layoutSubviews];

self.preferredMaxLayoutWidth = CGRectGetWidth(self.bounds);

[super layoutSubviews];
}

This ensures the preferredMaxLayoutWidth is always correct.

Update:

After several more release's of iOS and testing more usecase's i've found the above to not be sufficient for every case. As of iOS 8 (I believe), under some circumstances only didSet bounds was called in time before the screen load.

In iOS 9 very recently I came across another issue when using a modal UIVIewController with a UITableView, that both layoutSubviews and set bounds were being called after heightForRowAtIndexPath. After much debugging the only solution was to override set frame.

The below code now seems to be necessary to ensure it works across all iOS's, controllers, screen sizes etc.

override public func layoutSubviews()
{
super.layoutSubviews()
self.preferredMaxLayoutWidth = self.bounds.width
super.layoutSubviews()
}

override public var bounds: CGRect
{
didSet
{
self.preferredMaxLayoutWidth = self.bounds.width
}
}

override public var frame: CGRect
{
didSet
{
self.preferredMaxLayoutWidth = self.frame.width
}
}

Bottom of UILabel text cut off when using adjustsFontSizeToFitWidth

While I was typing out the answer and going back on every setting I had tried, setting the label's baselineAdjustement to .none fixed the issue.

label.baselineAdjustment = .none

FYI, the following documentations extracts were confusing to me:

From the documentation of UILabel baselineAdjustement:

The default value of this property is alignBaselines.

From the documentation of UIBaselineAdjustment:

.none: This is the default adjustment.

I'd be interested if anyone could confirm in the comments, but from my experiments, .none doesn't seem to be the default baselineAdjustment of UILabel. I had to specifically set the baselineAdjustment to .none to fix the bug mentioned in the question.

Attributed text UILabel cut off in UICollectionViewCell but not in UITableViewCell

Resolved it by providing the cell width for the UILabel property preferredMaxLayoutWidth minus possible offsets you are using in your layout.

self.yourUIlabel.preferredMaxLayoutWidth = CGRectGetWidth(self.bounds) - yourOffsets;

Found the answer here: http://johnszumski.com/blog/auto-layout-for-table-view-cells-with-dynamic-heights

Similar questions: Word Wrap not working for UILabel & UILabel not wrapping text correctly sometimes (auto layout)

But some question remains why I didn't need this property inside the UITableViewCell

Multiline UILabel with auto layout does not work

In the comments above you mentioned you're not currently setting preferredMaxLayoutWidth. This property tells your label that it should lay out its text over the width of that property's value. In UILabel.h:

If nonzero, this is used when determining -intrinsicContentSize for multiline labels

In other words, if you don't set that, the label's intrinsic content size is whatever width the label needs to draw its text. If you set this property to the label's bounds, it will start drawing on the next line (or else it will cut the text off if numberOfLines is 0).

In your case, I would probably do that in tableView:willDisplayCell:forRowAtIndexPath:.

UILabel AutoResize cuts off the top part of the text

What you are adjusting automatically is the Width and not the Height. The Height is something you'll have to adjust manually based on the maximum font size you will use. If the maximum (assigned initial) font size fits in height, so will the smaller one's do, after they are automatically adjusted

UILabel Subclass - Custom 'drawRect' method causes text to cut off / not show

I would actually not be subclassing UILabel for this, and make your own tooltip class composed of the outer view and the internal label with auto layout constraints. The internal label determines the whole height of the view.

Something like this with appropriately rounded off corners/triangle:
Sample Image

Alternatively, use UITextView instead if you want to assign padding: Adding space/padding to a UILabel

UILabel cutting off text

As mentioned, unless you use a monospaced font, 12 characters are going o occupy a varying amount of space.

The easiest thing to do in this case is to set the adjustsFontSizeToFitWidth to YES. This will scale the text so that it fits the width of its container.



Related Topics



Leave a reply



Submit