Reposition images within view based on size of text - iOS - 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

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

Determine width of UITextView in heightForRowAtIndexPath with auto layout

A rather convoluted problem, looking for an elegant solution:
A UITableViewCell subclass has a UILabel on the left and a UITextView on the right. Both label and text view have dynamic content. I am using the text view rather than another label because I need the address link recognition.
The label has a higher compression resistance than the text view, so that its right edge will expand towards the right as needed (up to a certain limit). The text view's width will in turn be compressed. The text view is set to word wrap to expand downward.
Because a text view is a itself a UIScrollView, the normal automatic dimension mechanism of a dynamically sized table view cells view will not work (i.e. using UITableViewAutomaticDimension). Thus, I tried to implement heightForRowAtIndexPath. To determine the necessary height, I thought just need:
The text in the label, plus its attributes
The text in the text view, plus its attributes
The width of the table view (as it is "plain" rather than "grouped")
With this information, I reckoned, I can use sizeThatFits on helper objects to determine the necessary height.
However, I am reluctant to hard-code the horizontal margins between the label and the text view into my size calculation, so theoretically I additionally need NSLayoutConstraint outlets for
The left margin of the label to its superview
The max allowed width of the label
The horizontal distance between the label and the text field
The right margin of the text field to its superview
This seems really insane! While I was writing the implementation of this, I asked myself if there is not an easier way to accomplish the same. (That's why I did not yet write the code to post here.) After all, the only hard problem here is how to get the available width for the text view, but the way to getting there seems unduly complex.
Any ideas for a concise, elegant solution?
Side notes: This has to be calculated before the cell is rendered to be displayed. Also, setting the text view's scrollingEnabled to false produces weird results when using sizeThatFits.

Auto-layout in UITableViewCell: make UITextView appear only if content not null

In my project, I have three types of custom UITableViewCells. One for text-only, one for text + url and one for text + image. I am using auto-layout with IOS 8.0 and am trying to use constraints to make the text block only appear if not null. The text being before url and image I don't want to get an extra useless space.
Is it the right way to go or should I implement two new types for image-only and url-only?
I have been trying to set up height >= 0 for the text block in the constraints but it keeps be displayed.
If you don't want the text view to take up space, remove it from the cell for that row. Of course, for rows where the text view is needed, if it is absent, you will have to add it. Making the constraints correct will also be up to you.

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.

Fill UIScrollView with its content using autolayout in iOS

I am making a form based UIScrollView, which will contain some labels and text fields.
My ScrollView Height will increase as per the iOS device height.
PS: I do not want to add constraint to each and every element of the Scrollview, because in my case there could be 100 form fields.
What I want is, the inner content to fully occupy my scrollView like this:
Till now there a are no special constraints, the button is tagged with the bottom edge and the scroll view is pinned from the top edge. Also, the vertical spacing between scrollview and button is defined.
This is the autolayout constraint screenshot.
If the number of labels is variable, I recommend doing them in code, rather than in Interface Builder.
In code, you can use a loop to set every label to have the same width/height as the one above it. You may want to set their height to be >= a minimum value. Be sure to anchor the first label with the top, and the last label with the bottom.
But this can be cumbersome, why not just use a UITableView? you may modify the row height to let the cell fully occupy the view.
- tableView:heightForRowAtIndexPath:
- tableView:estimatedHeightForRowAtIndexPath:
I just face the same issue and already write a pod called TLFormView that do exactly that: a form based on UIScrollView.
It also has some nice features like:
declarative form taxonomy with TLFormModel Just extend it, add the properties you want and that's it. No delegate, no event handling, no boilerplate.
a nice way to handle different layouts for iPhone and iPad
conditional visibility with an NSPredicate that can access all the values in the other fields (e.g: show field A when field B has certain value)
in place help with a popover
all the fields are TLFormFields that extend UIView so you can place whatever you need.
You can try it right from the command line with pod try TLFormView.
If you want to know more I wrote some blog post about it here.
Please let me know your thoughts about it here or as a comment in the blog posts. Also any contribution is extremely welcome in the GitHub repo

Resources