Wrapping text in UITextView in Swift - ios

UPDATE: Looks like the UITextView's width is bigger than screen's width.
It is strange since I created the UITextView using a storyboard and in preview mode it looks like it's width is less than that of the UIViewController.
I have a UITextView, in a UIViewController, that was created using storyboard.
I have similar code to populate the text in the UITextView.
self.taskGroupDescriptionTextView.text = taskGroup.description
self.taskGroupDescriptionTextView.textContainer.lineBreakMode = NSLineBreakMode.ByCharWrapping
I tried different types of lineBreakMode, break by char/word. None of them work.
My UIViewController when run on iOS simulator still has text that extends beyond the screen.

You should set constraints. Here's an example how to do it:
You click on your UITextView so that you can start editing it. At the bottom of the storyboard there are few icons and clicking on the one shown in the image will open up an editor where you can add constraints. You have plenty of options (pin to left, right, top, bottom, fix height or width, aspect ratio,... and even more if you click on a neighbour icon). Keep in mind that you have to set enough constraint so that it will be reasonable for your view controller to calculate the frame size of your view. But no worries, if you'll forget something you'll get an error.

Related

Too much text pushes my buttons below the screen

So I tried embedding my label (that has all the text) and buttons in a scrollView to perhaps be able to scroll down to them, but when I do that, everything just disappears.
my storyboard
my preview
Just added some pictures as requested (thanks).
As for adding the scrollView (which may not be the optimal solution), I just selected all 4 buttons and the label, then selected "embed in scrollView". This led to a completely blank screen in preview.
It sounds like you need to fix the bottom constraints of your scrollview, so that the scrollview's frame does not extend past the bottom of the screen. You can then set constraints on the scrollView's content size to allow the user to scroll.
From the storyboard image you posted, it looks like there isn't a bottom constraint. Select the scrollview in the storyboard, click the pin icon, and put in a number in for the bottom constraint (e.g. 0).

Aligning Elements in Xcode

I seem to be having trouble aligning elements. I want this UITextField to always be center horizontally, and stay towards the top of the app.
This is what it looks like on the storyboard:
As you can see, there are constraints set - which are blue lines (I think that means they are correct)
However when I view it in the simulator:
These are the settings in the Xcode pin popup:
Question:
How do I get the UITextField to always stay center horizontally, regardless of device or orientation?
Edit 1:
I took out all constraints, I made the UILabel the full width of the view, and centered the text inside. I assumed that this would make it center, but alas not.
Storyboard:
Simulator:
Edit 2:
See the two red squares, I'm not sure how to change that text. There are no options on edit.
I also cant see how to add the other constraints that you suggested.
remove your left and right constraints and set the following one.

UITextView wrong content position with auto layout

I have a UITextView in UIViewController with auto layout:
The TextView size changes proportionally in accordance to the View (for example on rotation, or run on different dispositive iPhone 4s..6s).
Unfortunately the content text doesn't show from first line:
I suppose that the content inset is wrong but I don't have a solution.
I don't use Interface Builder - so I'm sure someone can correct me if I'm wrong, or, see the mistake if I've missed it...
But, from your screen shot (resolution is a bit low) it looks like you've got vertical constraints when positioning the text view: there's a value showing a spacing between the view and Top Layout Guide (and again for the bottom layout guide). You could just delete these...
Specifically:
Top space to equals 135
Bottom space to equals 106
As for your second screen shot, I'm not sure if it's a different device to the image on the right... but maybe the sample text just doesn't fit in the view, and it's scrolled to the bottom of the text. That would make it look like the top text is cut off, because it's clipped by the top of the UITextView.
I had the same problem, and I tried all of the work-arounds to setContentOffset and/or scrollRangeToVisible to no avail.
I was inspired by this SO post:
https://stackoverflow.com/a/21287306/610550
I ended up disabling scrolling on the textView on the storyboard, and then using self.textView.scrollEnabled = true in viewDidLoad(). It worked perfectly!

Auto layout: Xcode 6: Centering UI elements

I'm using Interface Builder in Xcode 6 to make an app and am having trouble getting the text fields and button to centre on the screen for different size screens.
I thought it was a matter of selecting horizontal and vertical centering in container but it doesn't seem to be that when I try it in auto layout. Actually I've tinkered around a bit and I still haven't got it.
I just want to be able to see all of my button and text fields for any size iPhone screen and right now simulator is only showing part of these UI elements like this:
I also want to do this in storyboard and not in code as I'm not at the level of doing this in code yet.
Step 1: Make sure your size class covers all the iPhone screen at least in portrait view. So, change the size class to "wCompact hRegular".
Step 2: After setting the size class properly, add the UITextFields and UIButton to your storyboard. To me, it looks something like-
Step 3:
Before, you start adding constraints, you need to remember two things-
a. Your element(UITextField, UIButton, UIView or any component) needs to know its starting position unambiguously, and
b. Your element needs to know its size meaning, its height and width.
In this case, as you want to centre your elements, I am just assuming that it needs to be centred starting from 10 scale from the left edge and should end 10 scales away from the right edge of your iPhone screen. Now, that means, it's width will be different based on the screen size, but its height will be same.
So, I just add the constrains following way for the 1st text box-
Notice, in the size inspector, I set the text box's starting point, x and width in a way that it is 10 pt away from left edge and 10 pt away from the right edge. Don't worry, it's just simple math.
For the 2nd textField, I add the constrain, the same way-
Lastly, for the button, the constrains are following-
Now, you are good to go. Everything is centered.
By using your size class selector in the bottom of the storyboard window, set you sizes as any width and any height and then follow the below auto layout constrains. It will work for you.
First select the view you want to set the auto layout, and then select the pin option from the right bottom corner of your storyboard and then add the constrains as shown in above picture and click button Add 4 constrains
Repeat the process for all views and set the constrains as Fix the top, bottom, left and right constrains of all views except the last button that should be fix from top,left,right and fixed height.
You need to make use of the size class selector in the bottom of the storyboard window.
So for an iPhone 6 or 6 Plus in portrait you would choose a compact width and regular height like this:
And then you would do whatever auto layout stuff for the given device there

Where is this vertical spacing coming from in UILabelView?

I'm creating an iOS view that displays various static text elements. The xib looks like this:
It uses four labels for the title, timestamp, body, and footer. Every view is anchored to the sibling view above it vertically and anchored to the left/right of the parent view. All labels have a fixed height except the body which has a >= height and the number of lines set to 0 with "word wrap" as the line wrapping style. The parent view is a UIScrollView.
On the iPhone it looks like fine:
However on the iPad it looks like this:
Huh? Where is all that extra vertical space in the body label coming from? The xib and its view controller are identical between iPhone and iPad (there is no custom iPad code at the moment). I've found that the vertical space is directly related to how many line-wraps the label renders. If no lines wrap, no extra vertical space. If only a few lines wrap, there's a little extra vertical space. If nearly every line wraps, well, that's what it looks like.
First of all any ideas on why UILabel is behaving this way?
Second of all, if I can't make it stop doing this how can I work around it?
I've already tried a few things. If I call [bodyLabel sizeToFit] within -viewDidLayoutSubViews then it fixes the label but doesn't fix the layout of any of the sibling views (e.g. the Footer label is stuck way at the bottom of the screen instead of pulled up to just under the body). Any attempts to get the entire view to re-layout its children after calling sizeToFit is ignored. I've also tried sizing the UILabel by calculating height based on font, which results in the same behavior as -sizeToFit (albeit with more code).
Replacing the Body UILabel with a UITextView instead doesn't give me the weird vertical spacing issues but I need to calculate the height of the UITextView manually (using font calculations) and something about resizing the UITextView within the parent UIScrollView makes it so the UIScrollView simply refuses to scroll (as if it doesn't know its contents are too big for its bounds).
So at the moment I'm stuck. Even just an explanation of why UILabel behaves this way on the iPad layout would be helpful.
In case anyone else runs into this same issue using autolayout... I may have been able to solve the same issue by creating a constraint as Coche suggests, but I realized I had a preferredMaxLayoutWidth that was too small set on the uilabel. Once I set an accurate preferredMaxLayoutWidth (the actual width of the label) the spacing on top and bottom disappeared.
The main problem is that the method for auto resizing the text inside your Label is failing because in iPad your Label doesn't have a set width from the beginning, it is calculated on run time and that's the source of that mess. On iPhone, as your Label has a set width (on IB) there is no troubles.
There are two ways for solving the problem:
Having two storyboards : one for iPhone and one for iPad
Doing this will make that your Label knows its width since the beginning and it will just works as on iPhone.
Having just one Storyboard for both iPhone and iPad
You can go around the problem by calculating the size that best fits its text and with that result add a height constraint by code to the Label. For calculating the desiredSize you can calculate the width with this formula: Current View's width - (Leading space + Trailing Space). Here is my code
CGSize desiredSize = [_bodyLabel sizeThatFits:CGSizeMake(self.view.frame.size.width-40, 10)];
NSString *visualContraint = [NSString stringWithFormat:#"V:[_bodyLabel(%.0f)]",desiredSize.height];
[_bodyLabel addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:visualContraint
options:NSLayoutFormatDirectionLeadingToTrailing
metrics:nil
views:NSDictionaryOfVariableBindings(_bodyLabel)]];
objective-c

Resources