How to increase size of UILabel Automatically adding any text? - ios

I am using UILabel for showing text. I am giving small UILabel size and a lot of text in the label. My intension is to give a lot of text in small label automatically label sizes also adjusted automatically, how to achieve this functionality? Any one please help me. I am new for iOS.
Thanks in advance.

You can try this method of UILabel,
[myLabel sizeToFit];

Related

Text is not centered when increasing the height of Textfield?

I was looking at this post on increasing the height size of textfield. However, I have an issue when I have the height of 40, I get this
This can be solved by increasing the font size, but how do I keep the font size the same?
Show this screenshot and try it
You just need to clear your text property and only need to give placeholder text.
You don't need to worry just run your code and check on device or simulator your input text will be looking fine.
Hope it helps you.
As #Inder Kumar Rathore, and #Saheb Roy pointed out that it's a bug in xcode. When you run it, it is just fine.

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.

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).

How to prevent a UITableViewCell's textLabel from truncating its text on iPad

I am using a UITableView on an iPad and for some reason the UITableViewCell.textLabel's text is getting truncated even though there's plenty of space to display the text. I tried all of the following, none of which fixed it:
Set a flexible width autosizing mask on the cell
Calling [cell sizeToFit] after setting the text
Calling [cell.textLabel setNumberOfLines:0] and [cell.textLabel setLineBreakMode:NSLineBreakByWordWrapping]
I haven't yet tried subclassing UITableViewCell and setting the frame explicitly in the layoutSubviews method. Trying to avoid that as I feel like there should be a better solution. I also don't want to resize the text--there is plenty of space to fit the text at the full font size.
u set set textLabel number of line 0
cell.textLabel.numberOfLines=0;
cell.textLabel.lineBreakMode=UILineBreakModeWordWrap; or NSLineBreakByWordWrapping;
hope it help you
Set the "setNumberOfLines" property of the label to wrap the text to required number of lines.
If you don't want the "..." at the end of the text if it is too long then use
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap; or NSLineBreakByWordWrapping for ios 6 or more
Try to set height and width of textlable and tableviewcell dynamicaly refer this link
The answer had nothing to do with UITableViewCell in the end. There was a section of the code truncating the text to display in the table view cell! Thanks everyone for the help up to this point.
I had the same problem. this happened to me in ios 6 and this also happened to me with you customized cells. I used multiline and NSLineBreakByWordWrapping.
I've solved changing all labels to use NSLineBreakByCharWrapping

Resources