Font Size resets in NSAttributedString - ios

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.

Related

Label not displaying properly

I am beginner in iOS app development, I am working on a project that was done by someone else. So my problem is the label is not fully showing its contents:
check the second column under 'customer name'.
I did some basic alterations to the label but it makes no change at all. This is a collection view, there is another view inside the collection view cell which holds the title label and description label.
I searched everywhere but didn't get any proper answer please help.
Label in iOS does not change its size to suit the contents, you need to adjust it in your code. Your options are either decrease the font size to suit the description label size, or adjust the description label's height/width to make room for current content. I'd do a combination of both (slightly reducing the font size of the label's text, and at the same time making all the cells a bit wider and taller).
Oh, there is also a way to automatically shrink the font size if the contents doesn't fit the space. So, you'd need to check the option for your description label. Here's how you might do it: how to make UILabel autosize text in storyboard (or interface builder), NOT programmatically

Actual UIButton label font size for custom drawing

I was wondering how I would be able to obtain the actual button label's font size that the text gets rendered with... not the one obtained through label.font. The reason I need this is because since I'm allowing for the button label's text to get resized based on the width of the button, the size therefore also changes. However this change isn't reflected upon in the label.font property. This value is needed because I basically draw the text myself (and set the default text to transparent), so I'd need to be able to get the actual font size so that the text I render fits the button's bounds. Thanks, and I hope you understood my question.

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.

TTTAttributedLabel not displaying the last line when having Emoji symbols

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.

Minimum placeholder Font size for iPad app

I am making an iPad app and there are textfields with placeholders in it.
There are many textfields, so I don't know if the user will be able to see the placeholders.
Does anyone know what is the minimum font size advisable for a placeholder? I don't want the user to keep zooming to see what the placeholder says.
The placeholder's default size itself should be fine. As of modifying the size - it is a string and hence you can't modify the textsize attributes.

Resources