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:
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
How to Use Avsamplebufferdisplaylayer in iOS 8 for Rtp H264 Streams with Gstreamer
Get Selected Index of Uitableview
"Interfaceorientation" Is Deprecated in iOS 8, How to Change This Method Objective C
Tableview Reloaddata VS. Beginupdates & Endupdates
Can't Prevent 'Touchmove' from Scrolling Window on iOS
Nstimer - How to Delay in Swift
Get iOS Itunes App Store Id of an App Itself
How to Check If a Uialertcontroller Is Already Presenting
Swiftui: Unwanted Split View on iPad
How to Disable Caching from Nsurlsessiontask
How to Push Two View Controllers But Animate Transition Only for the Second One
Pod Error in Xcode "Id: Framework Not Found Pods"
Spacing Between Uitableviewcells
Cross Directional Uiscrollviews - How to Modify the Scrolling Behaviour
iOS 11 Uisearchbar in Uinavigationbar
Upload Multiple Images in Swift Using Alamofire
How to Hide First Section Header in Uitableview (Grouped Style)