Overflow on UILabels - iphone

I have a table view that I am customizing and I am adding a UILabel as a subview of the contentView, and I want the number of lines to be relatively consistent.
I set the numberOfLines property to be 3 so it can't go more than that, but there are still some that overflow onto the 4th line.
If it overflows, I want to add a a trailing ...
How can I figure out if it overflows? I've tried truncating at different character counts of my string, but since I am using lineBreakMode = UILineBreakModeWordWrap the number of characters doesn't really predict the number of lines.
Is there a way to find out the number of lines your UILabel is using?

Don't use lineBreakMode = UILineBreakModeWordWrap, just set the numberOfLines property to 3.

Related

UILabel Minimum Number Of Lines

If you take a close look an an iMessage conversation cell, you’ll notice that the preview text is always two lines long. This can’t be a hard coded row height because the rows adjust to dynamic type. How can you always force a label to take up a certain number of lines even if there isn’t enough text to do so?
Set the label's numberOfLines to 2 and end the label's text with a linefeed \n. Set the wrapping to word wrap to prevent the ellipses from appearing.
This guarantees that the label text consists of at least 2 lines worth of material. Thus it can never be less than 2 lines, and since the maximum number of lines is 2, it can never be more than 2 lines. Thus it will always be (wait for it) 2 lines.
One way I think this could be possible is having a UIView as a parent of the UILabel.
Fix the height of the UIView based on device size class. Let's say for example 50 points for Width = Compact and Height = Regular.
Embed UILabel in UIView
Set number of Lines = 0 for UILabel
Now match UILabel leading , trailing ,top edge with the superview and leave the height as it is , also don't set the bottom constraints.
Select the superview i.e the UIView and UILabel together and select Equal Heights.
Open the constraint window and change label height less than or equal to SuperView height.
Short Message
Long Message
Height Constraint of the UILabel with respect to SuperView.

Line Spacing in xcode/swift Label and TextView

My question is related to solving a problem and also to improve my understanding of swift.
I have a UILabel and a TextView. I have the same size font in both. However, in the TextView I am using attributed strings. The fonts appear the same but the line spacing is greater in the label. My problem is I cannot find a way to change the line spacing in the Label other than switching to Attributed String for the Label or for that matter even determining what the present spacing is.
Q1: Is there no other way to control the label spacing other than Attributed Strings?
Q2: Because of issues like this, should I be thinking that Attributed Strings might be a "best practice" regardless -- to give more control?
You can set linespacing in Label Attribute inspector.
See screenshot.

How to determine the number of lines being used in UILabel (Swift)?

cell.bodyText.numberOfLines = 0
cell.bodyText.text = newsBody
cell.bodyText.sizeToFit()
I set the number of lines to 0 because the number of lines can vary (I am retrieving news articles and since every news article is obviously different in length, numberOfLines is 0)
If I do not use sizeToFit() after assinging newsBody to the label, and check the label height (cell.bodyText.frame.size.height), I get really large numbers (when I tested it, an article that had 25 lines of text, the height was apparently 4000). If I use sizeToFitand then check the height, I get 21 no matter how short or long the label is. (21 is the height I set the UILabel in the storyboard).
How can I get an accurate representation of how many lines are in the UILabel or even just the height of the label?
I assume you're doing this work in tableView(_:cellForRowAtIndexPath:). That's not reliable for querying final layout. Try using the delegate method tableView(_:willDisplayCell:forRowAtIndexPath:). That's called immediately before displaying the cell, and is the appropriate place to work out any final layout issues since everything has been applied at that point.

Devide text to UITextViews

I have long text and 2 textViews.
I need to insert this text to 2 textViews(they have no scrolling).
It's like book pages(first page is a one textView, second page is a another textView).
So, question is: how to determine which text length I need insert to first textView?
You can set the complete text to the first textView, calculate the visible text range and set the remaining text to the second textView. An alternative is to manually calculate the range of the text that will be visible in the first textView, however it requires ugly recursion with sizeWithFont that i can't believe is so fast, i'd follow the first way.
See this answer to learn how to get the visible text range.
First figure out length of each line, font, and how many lines you can fill into the each textview. Use following to do this
CGFloat stringWidth = [text sizeWithFont:[UIFont fontWithName:your_textview_font_name size:your_textview_font_size]].width;
Then use substrings operation.
Shouldn't be difficult to do this!

UILabel need to setNumberOfLines to fit my text size

I have a UILabel. I need to set the number of lines to fit to it's text.
You can set the numberOfLines property to 0, which removes any line limit and uses as many lines as needed.