Autoshrink in UILabel only for width? - ios

I have some ASCII syntax diagrams which must not have line breaks in the middle.
These don't have to be editable so I thought the best way is to use an UILabel with auto shrink option. But this option shrinks the text also if the content doesn't fit the height of the labels frame rectangle.
I just want to shrink only if the content doesn't fit the width. It would be absolutely fine to scroll vertically through the text.
What is the best way to do this with UILabel or any other UI element?

Use UITextView with 'editable' property set to false.

So let me rephrase your question. I guess what you want is a UILabel which can show multiple lines, but the longest line need to fit into the width of UILabel. If this is what you want, well the imagination is weird to me...
But anyway, I feel there's a conflict in your settings. First, allowing multiple lines implies you set "Lines" attribute (number of lines) as 0, which allows unlimited lines. But then Autoshrink will play no effect. I'm afraid it is not possible to be done by just setting the storyboard and instead, you need to write some code.
I guess people have raised related questions earlier, by which they want to dynamically change the font size when the text become too long. I guess you want to take a look about this:
Autoshrink on a UILabel with multiple lines
The last issue is you also want the scrolling effect (this is why I feel the outlooking will be weird.) But in short, to achieve this you need 1) dynamically change the UILabel height, most likely using the same technique as explained in the reference thread, and 2) wrap the UILabel in a scroll view. Maybe this can achieve what you want.

Related

Autolayout, several UILabels, lines in each, same font size?

I have a number of UILabels on a view and a varying amount of text in each. I need the font size to be the same in each.
I’m using autolayout to position / size the labels.
However some of the “g” characters are being cut off.
I’m not sure how I can resolve this ?
change the lines property of the lables to 0 and hook then properly to each other or to the superView so when there content is large they wrap smoothly
You cannot control font size directly via auto-layout constraints. You can disable adjusting font on all labels and this way they will have same label.
But to better help answering your question (and underlaying issue you're trying to solve) - it would be much better if you share screenshot with your interface.
SizeToFit fixes my issue, rather than autolayout

Prevent UILabel from clipping it's text when the font size is larger than the label height

I have a circumstance in my app whereby a label may be given a font size greater than it's height. This is to do with some rather complex architecture and layout code. Normally I would increase the label's height to accommodate the larger font but that is profoundly difficult to do in my scenario and I would rather avoid it.
The next logical thing is to turn clipsToBounds off, to allow the text sublayer to overflow the bounds of the label. Unfortunately this seems to have no effect in this case as the text is still clipped.
Am I missing something?
Looking at the documentation for UILabel:
https://developer.apple.com/documentation/uikit/uilabel/1620545-textrect
I think you need to override the method textRect(forBounds:limitedToNumberOfLines:) by explicitly increasing the rectangle returned by this method to the containing size of the label’s string rather than the bounds of the label.
(This solution does of course require you to subclass.)
Hope that helps.
You should be able to get the font height from font.lineHeight and then reduce the font size until the line height is less than the label height.
The reason (need citation) is that UILabel which is embeeded in UIButton cares extra glyph information embedded in font whereas an independent UILabel doesn't.
Solution
You can nest a separate UILabel on top of your UIButton and it will solve the problem. It's ugly but it works. There are few workarounds that you ought to try.
Workarounds
Depending on the scenario here is a small checklist that I found as accepted answer or useful for someone.
1) If you're using a UIButton Make sure you're using this method
[button setTitle forState:]
otherwise you'd need to use the following code to refresh the state
[myButton setNeedsLayout];
2) You might need to adjust your font size to fit the width of the label.
[yourLabel setAdjustsFontSizeToFitWidth:YES];
3) Although setting clipToBounds works in consecutive hierarchy, You might want not want to set individually on either Button or Label.
[yourButton setClipsToBounds:NO];
[yourButton.titleLabel setClipToBounds:NO];
There are few solutions that are pointing UIButton subclassing method which are essentially trying to add UIEdgeInset to button.

How to find the biggest text size that fits a UILabel or UIButton using autolayout?

Given a layout that you set up using constraints, how do you find the biggest text size that fits your labels and buttons?
You can use adjustsFontSizeToFitWidth for labels but it only cares about width, not height. And I don't understand how button labels exactly work...
I posted an answer to a related question, but I wanted to ask it again since the other question is a bit forgotten and I don't know how I would do it for buttons.

How to handle long text UILabel

I have a view which has a lot of labels. Some of them get the text set dynamically. Sometimes the text is to long to display it within one line. I know how i can display the text in multiple lines. My problem is that when i do display the text in multiple lines then the margins are broken between the labels. Sometimes a label even swapes into the content of the next label.
I want to avoid fixing this "by hand" (repositioning of all other elements). Maybe Autolayout can fix this, but sadly i'm not able to use it for now. But it would be a good argument to convert my project from springs&struts into autolayout. However, maybe there is another way to fix this issue. Would be nice if somebody can help me out. Thanks in advance!
You can use sizeWithFont:constrainedToSize:lineBreakMode: to calculate the height which will be required by each of the labels. You should use this in a loop which iterates over each label in the order they should appear on screen. As you go, increment the y position by the height of the current label and the margin. Each label will now have the correct position and at the end of the loopy will hold the full required height.

IOS: uilabel cut my word

I have a problem with label and I don't understand where is the solution.
I set my label with "Word Wrap" and 3 lines but it cut a word when text wrap...why??
thanks
Probably bounds are too small? Stretch the UILabel out as much as possible (height-wise), set lines to 999 and see if it still cuts it.
edit: Somehow this still gets views. More recent versions of iOS no longer have as many issues with UILabel, so this no longer applies. (especially with Auto Layout)
This is a really common problem when trying to use multi-line UILabels. The short answer is to use a UITextField instead if you want multiple lines of text. You get more control over the padding around the text as a bonus.
(yes, you can 'fix' the UILabel but sometimes simpler solutions are better)

Resources