Dynamically resize UILabel to content - ios

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

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

iOS what element is most suitable for large texts eg text view

I wonder what ui element I should use for large sets of text. Eg anything between 200 - 1000 characters.
I already place the text inside a scrollview so it doesn't have to ve scrollable or editable etc, I just want to display text.
So what should I use between label / text view / text field?
Thanks in advance.
Text views are good for texts with varying lengths, but they have a scroll view of their own. You may consider using a normal UILabel and setting the "Lines" property to 0. I know that sounds strange, but setting it to 0 tells the label that it is a multi-line label. Then you can use auto layout to establish the width of the UILabel. It will grow down based on the amount of text in it. Text fields would be inappropriate for displaying texts in most cases; they are better for user input.

Autoshrink in UILabel only for width?

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.

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.

Reposition images within view based on size of text - iOS

I have been recently getting into iOS development, and I'm trying to build something that looks (very roughly) like this: http://falkendev.com/downloads/ios-sample.png
Basically, it's a page that shows simple text -- large header text that may span multiple lines, a separator line, and then smaller descriptive text that may be a variable length. This text does not need to be editable. I'm working using interface builder, but I imagine that what I want done may need to be done programmatically?
Two questions:
-- How do I go about creating these text fields so that they adjust their height based on the content? I'm assuming I would be using a standard "text" field for each, make them not editable, and then programmatically change their height? And then based on the height of the various text fields, I would need to adjust the positioning of the text fields and the divider line between them?
-- How do I go about making the page scrollable? It's possible that the descriptive text will be long and would extend off the edge of the screen. I would want the whole page to be scrollable, not just the descriptive text section. I'm assuming I would place all my elements within a scroll view... but currently when I do that and view it, the view just gets centered (cutting off both the top and the bottom) and I can't scroll it at all.
Thanks for any help!
set the scrollview content size to greater than its actual size to
make it scrollable like this :
scrollView.contentSize = CGSizeMake(YourWidth ,YourHEight ); // Here you can change either of height and width to make it more scrollable in that direction.
You can use UITextView object to have a scrollable text field...
which can scroll to show additional text..just set its editing
property to NO.
Otherwise to dynamically update label height yourself...use
NSString sizeWithFont method

Resources