How to Align Label Text to the Top - iphone

I have a label in a UIScrollView that gets its string contents from a website. I was wondering how to make the label display the text so that it is not centered, but is top aligned. The text in the label keeps displaying in the center of the label, which looks awkward in a scroll view. I want the label to display the text starting from the top. Thanks!

Call -sizeToFit on this label after setting it's text. If all you have in this scrollview is a label, you could also adjust it's contentSize after that.

If you use auto layout, you need to adjust your constraint of height to "Less than or equals" and update your constant to the maximum you need.

Related

Swift: UIScrollView doesn't scroll after the user edits the height of a UITextView

I have two UITextView inside a UIScrollView and the TextView can resize as the user types more text into them. I need the page to be scrollable to account for this so I put them in a ScrollView, however the height of the ContentView doesn't change with the TextViews. Here's what I tried so far:
adding a bottom constraint from the last helper text to the bottom of
the content view with priority 1000 and constant 210. This doesn't
work because the section will be scrollable before the TextViews get
taller and on tablets or larger devices it will make my first
TextView taller.
adding a bottom constraint with a constant of 210 or lower will give me a layout error.
I also thought about calculating the height of the content view and maybe adjusting the constraint based on that but this doesn't work either.
Here is a link to the file in question: https://www.dropbox.com/s/g7q1lbj0pxx14jh/Main.storyboard?dl=0
Any suggestions?
It's also worth noting that the keyboard will cover the second TextView when the first one gets too big so I will eventually need to edit that constraint based on the height of the first TextView (to give it more padding at the bottom so that the user can bring the second TextView in view.
Here is the fixed variant https://www.dropbox.com/s/gujry7ngziaipm7/Main.storyboard?dl=0
I found just 1 error:
You forgot to specify bottom offset from last view in View Container which is UILabel - "Being clear...", so your view container doesn't know when to resize as soon as you do not specified all offsets to it.

Vertically centering UILabel programatically in scrollview that doesn't match the view swift

I have a horizontal scrollview that fills 1/3rd of the view.
I want to put a UILabel in the centre of the scroll view, which I have successfully done with:
let reagentsLocationx = mainscrollview.frame.width/2-100
However, I am struggling to centre the label vertically within the scrollview. When I use similar code for reagentsLocationy it centres the label in the view and not within the scroll view.
How do I centre the label, in Swift, just within the scrollview?
Many thanks.
I think this is because you are using frame and not bounds. I don't believe frame will ever be larger than your screen space, but bounds it equal to the view's actual size. Try basing it on the bounds of the scroll views height.
UIScrollView().contentSize.height might be what you are looking for.
No need of setting sizes in code.
Everything done in the storyboard.
Drag your scrollview into your view controller.
Set top, bottom, leading and trailing constraints from the scrollview to the parent view or safe area.
Drag your text label into the scrollview.
Set your top, bottom, leading and trailing constraints to the scrollview.
IMPORTANT set a second leading and trailing constraint, now from the text label to the parent view (UIView - not scrollview) or safe area.
This makes sure that the text label knows his width and so the scrollview knows it too.

How do I determine the origin (x,y) of the Delete button for a UITableViewCell during editing mode?

I have a UITableView that uses the standard delete editing style. I want to resize the text in the cell when the Delete button is displayed so it is visible.
How do I determine the origin of the standard Delete button so I can calculate the proper frame size for the resized text?
EDIT:
This is a subclassed UITableViewCell that contains a UILabel. The amount of text in the label varies from row to row so the height of the cell is calculated for each row and the UILabel frame adjusted accordingly when the cell is created.
I have tried adjusting the autoresizingMask of the UILabel in the cell as described in the comments and it has no effect on the layout of the text in the label when the edit (Delete) mode is active.
EDIT #2:
As requested here are some screen captures.
a) Here's the table cells w/ the UILabel control. The height of each cell is dynamically calculated based upon the amount of text.
b) When the Edit mode starts the editing control (red circle) is displayed which pushes the text off the right side of the screen.
c) Then when an editing control is selected and the Delete button is displayed it overlaps the text.
The objective is to have the text area (UILabel frame) reduce it's width and increase the height to accommodate all the text when editing starts "b)". When the Delete button is displayed "c)" the text height would stay the same and the text that does not fit can be truncated and an elipsis "..." displayed.
You don't have to - if you've added your subviews to the content view of the cell and set their resizing mask appropriately, this will happen automatically for you.
By resizing mask, I mean the options visible in the size inspector in interface builder. For example, if you want the cell to stay a fixed distance from the left and right margins, but grow or shrink in width, then you change the margin and width bars so that the preview shows the label growing and shrinking as opposed to staying the same size but moving around. I can't remember the specific appearance and I'm not at my mac at the moment, sorry, but it should be pretty obvious.

How do i bring text in UITextView inwards from both sides and from above?

I want my UITextView to show text but not have the text be as wide as the screen. The problem I face with this, however, is that I want the scrollbar to be in the position it's in now. I'm unsure how to do this. Here's an image of the text i want bringing in, to show you what i mean a bit more clearly:
You can customize the position of the text using the contentInset, contentOffset and contentSize properties of UIScrollView (which is the superclass of UITextView).

How to make a UITextView scroll horizontally as the user types?

This seems like it should be simple but I can't work out how to do it.
I've created a UITextView in interface builder. It doesn't sit in a table cell or anything fancy like that.
What I'd like to do is have the UITextView scroll itself to the left when the user has typed their way all the way to the right margin.
At the moment it just does a word wrap, I understand the word wrap makes sense in most situations but I need it to scroll instead. What I'm after is the same behavior it exhibits vertically.
I've tried adjusting the content size of the UITextView in viewDidLoad and also in viewDidAppear and that doesn't make a difference.
I've also played with the inset settings in IB but that doesn't affect the scrollable size, just WHERE it's displayed.
Does anyone have any ideas?
If you only need a single line of text that scrolls, you would probably be better served by a UITextField. UITextViews are meant for multi-line text input, which is why word wrap is the default. You can't, as far as I know, override that behavior.
Ok, it doesn't look like it can be done exactly how I wanted but I've come up with a hack to get me there.
Let's say I want a scrollable width of 600 and a visible width of 250.
The first step is to make the UITextView a wide as the area you want to be scrollable. So what you would have put into contentSize, in this case 600.
Then the right inset is set to the difference between the actual width and the width you wanted. In this case 350.
This way cursor is restrained to the width you need BUT there is text visible to the right of your desired width, after all, the UITextView IS 600 pixels wide.
Now here's where the really hackish bit comes in, don't read on if you're sensitive or have a weak stomach.
Get an image of the user interface to the right where UITextView should end. Insert it into the NIB as an UIImage view and put it back in it's place, making sure that it's on top of the UITextView.
When the view is displayed, the cutout sits on top of the UITextView and hides the text overrun.
I'm not proud, but it works.