TTTAttributedLabel not displaying the last line when having Emoji symbols - ios

We are using "TTTAttributedLabel" for displaying labels. For calculating the correct rectangle size, we use NSString's "sizeWithFont" method, with a "constrainedToSize" the width of the field.
The calculation is fine, unless there are some Emoji symbols in the text, and the text is multi line (for example: smiley-newLine-smiley). In that case, the returned size is too small (vertically), and the last line is not shown. If the text does not contain any Emoji (e.g. X-newLine-X) - the size is correct.
Our font is "HelveticaNeue" size:16.25, in case is makes any difference.
Is there a better way to calculate the needed size, so that it will work with Emoji as well?
Thanks

I had a same situation when making auto-height label according to the contents of the label.
Everything seems fine, except when there are emojis in label content.
It was because I did't use the correct setText method for AttributedString.
[label setText:text afterInheritingLabelAttributesAndConfiguringWithBlock:^
return mutableAttributedString;
}];
This is the correct way to set AttributedString, but what I did is
[label setAttributedText:text];
So it was getting wrong label heights when it includes emojis in it.
I solved this problem by changing this set method with the correct one as described in Github manual.

Related

Dynamically resize UILabel to content

So I have a UILabel that I use to display some piece of text retrieved from a JSON object. Hence I do not know in advance the length of text. Coming from Android development I've been spoiled with the "wrap_content" parameter that will automatically adjust the layout according to the text that a textView shows.
In Xcode I seem to be limited to specifying the number of lines a UILabel can display and that's pretty much it. Is there a better, more dynamic way of doing it?
A UILabel resizes to its content by default.
You can specify the maximum number of lines (0 for unlimited).

Line Spacing for UILabel with a single line of text

Is it expected that a multi-line UILabel with a custom lineSpacing attribute include that line spacing even when the label's text fits on one line?
Here is my label:
let label = UILabel()
label.numberOfLines = 4
var paragraph = NSMutableParagraphStyle()
paragraph.lineSpacing = 5
paragraph.lineBreakMode = .ByTruncatingTail
label.attributedText = NSAttributedString(string: "Some short text", attributes: [NSParagraphStyleAttributeName: paragraph])
And here is how it is laid out. Note the additional spacing below the text.
For comparison:
What's strange is the lack of consistency. When the label extends to a second line, the bottom line no longer includes this additional spacing:
Is there a way to remove this line spacing when there is a single line of text? Or some other way to enforce some consistency so I can at least account for it?
Update
The baseline calculation also seems broken. When attempting to align a view (here, the red box) with the label's baseline, multi-line labels are partially covered.
Since you said you were using a custom font, my best guess based on prior experience is that the root cause of that issue that you seeing lies somewhere inside of the custom font itself. Whenever I am given a custom font by a client, 90% of the time, something is "wrong" with the actual font metrics (as interpreted by Apple's internal font rendering subsystem, even though it might render correctly somewhere else).
The good news is that this is fixable, but it requires rebuilding the font with new metrics, which is usually a trial/error affair. You might also need to check to see if the license you have for the font will allow such a thing (if it even matters).
That being said, these are some resources to questions that I keep around for this exact scenario whenever I start a new project:
Here's a similar question to yours with the assumption that this is a custom font issue: "Custom UIFont baseline shifted". This question deals with this issue in a UIButton "UIButton custom font vertical alignment", but both of these questions end up at the answer to this question "Custom installed font not displayed correctly in UILabel".
I have a personal testbed app for custom fonts now that I use whenever I am first given a custom font. This allows me to test the font in isolation for each rebuild iteration to make sure it's perfectly rendering. Make sure to test your changes in various font sizes and even in additional languages (yes, lots of permutations). I have had issues specifically with Thai and Chinese when using custom fonts as their ascenders extend very close to the edge of the bounding box for a UILabel. The testbed that I've created for myself includes the font rendered in basic UILabels in various sizes and various languages in various sizes (since like I said, I've had a bad experience in the past with custom fonts in certain languages that rendered fine in Roman characters).
If someone has a better solution to this, I'd love to hear it as I run into this issue with custom fonts almost every time. This is my workflow for nipping the issue in the bud before we start compensating for the font's rendering issues during layout or using individual attributed string adjustments. I'm not font expert, I'm just a guy who likes fonts to render like the built-in fonts (especially when using auto layout).
You can calculate the number of lines and set lineSpacing to 0 if there's only one line.
But there might be a better solution.
paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping
can avoid this issue when text is multiline.
explicitly setting the font with with fontName [UIFont fontWithName:#"PingFangSC-Regular" size:14] , instead of using [UIFont systemFontOfSize:14] can avoid the issue when text is single line.
Hope this is helpful for you!
This is def. a issue for UILabel. It happens for custom as well as the system fonts.
If you can use a UITextView, go with that. The UITextView has no problems with single or multi line text line-spacing and behaves correctly (single line = no line spacing).
This way you can also avoid creating a custom line count func/ext.
Yes, the lineSpacing is applied regardless of how many lines are in the label. If you're using autolayout, you can work around this by constraining your label's baseline to its parent or sibling views (as appropriate), instead of the label's top edge aligned to the parent or sibling's top. (This assumes, however, that your label's background color is the same as the color of its parent view; otherwise, you'll see the extra line spacing appear in the background color.
Another thing you can do (and this is probably preferable, now that I think about it) is to set a paragraphSpacing attribute of 0 as well. That should negate the lineSpacing for the last line in the label, regardless of how many lines you have.

Font Size resets in NSAttributedString

Am adding an image to a NSAttributedString object so I can display them properly in a UITextView.
So far, it works great. Only problem is, my Font size is 25 to match my image size but each time I add an image to the UITextView, the Font resets to some smaller size. I need to keep the font at 25 to match texts with the image size.
NSAttributedString has 3 constructors which take string, attributedString and string:attributes:.
Since I'm not using string, I can't set the attributes using the third constructor and the first 2 won't allow me to set attributes (UIFont). I therefore decided to set the Font size from Interface Builder and reset it each time I add an image to the `UITextField.
Am facing a problem here because each time I reset the font, the UITextView scrolls to the first line (assuming there is so much text, it has scrolled up). How do I set font size after adding image without the UITextView scrolling up automatically? Better still, is there a better way of doing this? Thanks.
NSMutableAttributed string has some methods you may try, including setting attributes across ranges after the attributed string is constructed.

Plain text being cut off at bottom in iOS, how to fix in xCode?

I noticed in my iOS app, for text that are using a system font with size 20 pt or more, the bottom of text is being cut off.
For example, the bottom portion of the letters y, g, p, and g are being cut off.
How do I fix this in Xcode?
If your using something like a UILabel it means you will need to increase the vertical height of that element.. Either in code or in interface builder.
If you are doing it in code you can get the label to resize itself to fit the contents by calling sizeToFit..
[myLabel sizeToFit];
Alternatively you could measure the size to the string by using sizeWithAttributes: then change the size of the label accordingly.
Updating this one for Swift 3.x in a UITableViewController.
In cellForRowAtIndexPath after the problematic label is set, but before the cell is returned from the method, add a line something like this:
cell.myLabelWhoseCharactersLikegyqGetCutOff.sizeToFit()

Customize iOS UITextView text

In my iOS application i have an UITextView in which i insert some text downloaded from the web. This text is not editable/selectable by the user and it's a sort of preview of the whole text downloaded.
So, i want to show only the first two line of the text and i want to have always a vertical centered alignment. The text can also have only one line and, if there are more than two lines or there is a very long line i want to put these ".." at the end of the visible text.
I want something like this:
I hope i explained myself.
If it is only a preview of the entire text, you can use a UILabel instead. The UILabel will have a fixed size and it will automatically truncate the text at the end and add the "..." you want. Don't forget to specify the numberOfLines property of UILabel to be 2 and the textAlignment property to NSTextAlignmentCenter if you want it centered.

Resources