I'm populating an UILabel with JSON data and I want it to adjust its height to the content it loads. Also the rest of the labels in the view should move. How can I do this using Swift?
Use autolayout.
You can give priority to the label you load from the json and shrink other labels.
This tutorial is a good place to start with auto layout - http://www.raywenderlich.com/115440/auto-layout-tutorial-in-ios-9-part-1-getting-started-2.
Visit http://nscookbook.com/2015/06/ios-programming-recipe-36-a-fixed-width-dynamic-height-scrollview-in-autolayout/
You can do that either in the storyboard or in the code
I would consider putting the labels in a UIStackView if your app requires iOS 9+. If for some reason the labels aren't resizing properly when you change the text, you can force it by calling [label sizeToFit];.
Related
is there a way in which a UITableView can always take a fixed height and can scale the static cells i'm using to the remaining height? I'm trying to achieve that my tableview layout is always visible on different devices.
I think I understand what you're getting at with having the table view showing on all devices. Instead of setting a specific size I'd recommend using auto layout instead! You can read more here.
Being an android developer, when I want the width and the height of something to wrap its content I simply change the width and height value in the inspector to "wrap content" and it's done. it does not seem as simple as that in Xcode. What I'm trying to achieve is the design of a messaging app, i.e.:
As you see each row has it own height and width of the UILabel according to the text size.
I tried to look for solutions using auto layout and stuff but with no success!
I was able to have dynamic height but the width of the UILabel is always stretched when used and I'm kinda lost. any straightforward solution in order to achieve this or a hint on how to use the auto layout to get the desired results ? Thank you.
labelName.sizeToFit()will change the frame of the label to fit the text
Hi Guys here i am suffering issue of dynamic height of UITableview Programmatically. I have set Autolayout of all view without any error
but lblDesc (UILabel) not expand with its size according to content. plz guide me
sometimes I do not need btnVideo (UIButton) so i have to hide btnVideo(UIButton) and dynamically all social networks UIButton come after lblDesc (UILabel)
Here i attached my code with only one view
UITableViewAutomaticDimension
https://drive.google.com/file/d/0B5mabdphYDhzWG9UMzM2MTV6cms/view?usp=sharing
You should use UITextView instead of UILabel.
Btw, never forget you setup constraints between text view and container views for all edges correctly, so according to your text size, the whole cell will be resized dynamically.
I have reviewed your code. tableview has correct setting.
Just setup correct layout constraints so that table view could know how it should change the height of cell.
Cheers
In one of my tableview i'm using a custom view created with a XIB file.
The tableview is correctly resized with autolayout, but the width of the cell is fixed.
In particular i saw that the ContentView is fixed to 320 px (the size used on the xib file).
Is there a way to use autolayout to change this value?
Sounds like you haven't set any constraints in your custom cells. The cell itself isn't the problem I guess, usually the container view is. So lets say you created the cell to fit the iPhone and it looks cut off on iPad, it most certainly because the UILabel or UITextView or any other element in the XIB isn't configured to stretch according to its constraints.
There's this beautiful tutorial by ray wenderlich, if you wanna learn more about auto-layout and how constraints secure that your app looks good on any device
If you need further help, just let me know!
I'm fairly new to iOS development and am having a hard time sizing a UITextView to it's content. I've found several examples of calculating a strings render size and/or updating the frame size of a textview, however I can't seem to get those to work.
When I add a UITextView to my view it adds a Height constraint which I think is locking it to a specific height. How can I work around it, update the constraint via code, and/or get my UITextView to resize vertically to show my content?
You can create an outlet to the vertical size constraint, and then adjust the .constant property of the constraint to amend the height of your text view.
A UITextView inherits from UIScrollView. As part of it's logic, it changes the height of its contentSize property to allow the user to scroll vertically to any portion of the TextView's content.
I would recommend using the ContentSize property of your UITextView to figure out how "tall" your content really is. You can then set the frame of your UITextView accordingly.
OR: You could figure out the size of your text using the right method from the NSString+UIKitAdditions category.
If you don't want autolayout then disable it in the xib file.
If you want to change the contraint and it's value this tutorial will help you to deal with autolayout.