I'm trying to create simple application (for testing purpose) with NSScrollView and 2 multi-line labels inside it (title and document). I can't use integrated TextView because I need 1 general scroller to scroll all content, not just for one selected TextView.
So generally I need fixed one-line title and document text with shared scroller.
I have no mind how to make this.
I've attached my sample project (only Storyboards) with scoller, you can take a look here: scroller.zip
I'm not sure if this is possible in the designer, but it is possible to set correct constraints in the runtime.
Follow the steps:
In the storyboard select your text field
Set bottom margin constraint (28 in
your example)
Set height constraint (242 in your example)
Connect the height constraint to an outlet in the viewcontroller (for example #IBOutlet weak var labelHeightConstraint: NSLayoutConstraint!)
In the viewcontroller's viewDidLoad method set constraints height to expected height (for example
labelHeightConstraint.constant = 1000)
Run :)
Hope it helps!
Related
How to design the following layout with StackView
I know that it can be easily created without using stackview but is it possible using stackview because i have lots of ui which already designed using stackview how can i add divider between views like above image
For that i set the following constraints
**But run time it shows like **
You can use some simple Autolayout constraints to get this kind of interface with a UIStackView.
The below image describe the hierarchy and constraints that you can apply:
Also the Accept and Reject buttons have Equal Width constraint.
Output screenshot:
You can do that using UIStackViews, here is how:
First, create a subclass of UIView to use as the class of the UIView in between the 2 buttons.
class CustomWidthView: UIView {
override var intrinsicContentSize: CGSize {
return CGSize(width: 1, height: self.frame.height)
}
}
You only need to override intrinsicContentSize.
Now, add your 2 buttons and an empty UIView between them, and set the class of the UIView to be CustomWidthView. Embed your 2 buttons and the view between them in a stack view, and set the position of the stack view properly using the appropriate constraints.
Select the stack view and from Attributes inspector, find the property named Distribution and from the drop down menu next to it, select Fill Proportionally.
To reflect your changes in the UI Builder, select the custom width view and go to Size inspector, go down to the bottom of the list and you'll find a property called Intrinsic Size, change its value to be Placeholder, and from the width drop down menu, select 1.
There may be a better way to achieve this, but this is the one I found for now and I'd like to find a better one.
Since UIStackView uses auto layout to arrange its subviews, you can modify its behavior by creating constraints.
I assume you have three views in the stack: the accept button, the divider, and the cancel button.
Create two constraints:
An equal-width constraint between the accept button and the cancel button.
A fixed-width constraint on the divider (presumably setting its width to 1).
See also this Q&A.
Every time I click-and-drag a UILabel to storyboard, and then add text to Text field, the text is cut off, so I then have to click-and-drag the UILabel to stretch it big enough so that the text appears.
I can't figure out how to make the UILabel automatically get big enough to fit the text.
This would save time while designing new views.
On any element in Interface Builder you can select the element and hit...
Editor > Size to Fit Content (keyboard shortcut: CMD+=)
This will do a "sizeToFit" on the selected element. Labels will fit their text size, image view will resize to the image size, etc...
In Xcode 6.1, I had to set Content Compression Resistance Priority to a higher value, likely because I have other constraints that were conflicting, or causing the sizeToFit option to be disabled.
Before:
After:
For the text label:
Set Lines to 0
Set Height to Greater than or equal to 30 (or other value)
Now the height will be adjusted based on the lines number.
Starting with iOS 6, there's a new API, available in Interface Builder as well called 'Auto Layout'. You can specify some contraints and UIKit will resize / move your views based on those contraints. If you don't want to use Autolayout you can use the autoresizeMask property of UIViews (which is settable in Interface Builder as well).
Check out the Autolayout Guide!
To resize the views in Interface Builder as you build them, see the reply from #Fogmeister
I'm working on iOS RSS app, and my last view, which is a UIViewController, is similar to the attached image. I inserted in my DetailView.xib, one Image View to pass the images of the RSS feeds and two Text View to pass the title and summary respectively.
The question is, how can i make the same, but inside a UITableview?
you can use custom cells for it and can add this custom cell at particular index. At first index you just add image view and at second index you just add textview.
Check out this pretty good tutorial Custom UITableViewCell Using Interface Builder.
i hope it helps you.
You can achieve this particular thing by using Custom Table View Cell.
Table View gets created using single Table View Cell again and again. It is much more efficient and uses less memory.
You should check this tutorial.
I hope it will help you.
Thanks
You can make TableView height UITableView.automaticDimension and make sure UITextView autoscroll is disabled and constraint should be leading, trailing, bottom and top.
Here's the link this might work for you:
How to make a UITextView Expand with the text like the Notes app
I am working on the auto layout. However, there is a small problem about I can not figure it out. Google it but can not find the clue.
As we can see, the tutorial first created uitextfield1 without any constraints at all. However, when I am trying to drag and drop another uitextfield2 to the view, there is constraints associated with it
My question is how to create an uitextfield with no constraint ( width + height )
Edited (for more details what I am trying to achieve)
For the picture above, we can see the textfield2 has the width constraint of 203. However, the textfield1 does not have width constraint like below
all the components in the view has there own constrains, but Xcode only shows you the ones for the selected component. If you don't want the Autolayout constraints in a view or Storyboard then disable it in Xcode
How do I arrange some UILabels and/or UIButtons of a variable length? I just want to add them to a UITableViewCell and they should arrange in a left-to-right flow, much like lines of text in a paragraph.
I only found possibilities to create lables with a fixed size and position using "initWithFrame:...". Same seems to be true for Interface Builder, as far as I can tell. Any solution is appreciated no matter if it's done in code or using a custom cell XIB-file.
UITableViewCell, UILabel, and UIButton are all subclasses of UIView and the documentation for UIView says:
Layout and subview management
A view may contain zero or more subviews.
Each view defines its own default resizing behavior in relation to its parent view.
A view can manually change the size and position of its subviews as needed.
So, it is certainly possible to do.
You can create your labels and buttons using initWithFrame: with the argument CGRectZero and then resize them (based on the text or whatever) using setBounds: or setFrame: (because right now you're just going to set the size of the view). Then, add these views as subviews of the cell's contentView.
Then, in a custom subclass of UITableViewCell you can implement your solution by overriding the default behavior (which does nothing) of layoutSubviews: to set the origin field of the subview's frames (i.e., CGRect) that will position the subviews in the cell's content view (the size has already been set). You may need to call setNeedsLayout: or layoutIfNeeded:.
This is really a rough outline of how it is possible to implement a solution because there are a lot of details left out. For example, if you resize a button based on the the text of the titleLabel you'll probably want to pad some to the width and height otherwise the button will be the size of the label and will look odd. In the layoutSubviews: method there could be a fair amount of logic to layout the labels and buttons the way you want (e.g., it would be simpler if all the subviews of a cell where of the same type such as all labels) esp. if the subviews could wrap to a new line.
For multiline UILabels to get the width and height you should use the NSString method sizeWithFont:constrainedToSize:lineBreakMode: you can then use the sizes you get to lay everything out where it needs to be.
I want to do the same thing to allow users to enter tags into a text field - a bit like how when you type in an email address, the address gets converted into a blue tag (the the users name in when that users email address is already in your contacts list). Haven't written it yet, but will be happy to share with you once I do. I can't commit to how long it I will take to write this unfortunately. However if no one else has code they can share and you need to get the job done quickly - Just as a tip, consider this:
Create tag view objects where each object knows the size of the parent text field/tag container view and where each tag object has a utility method which a further tag object can use to insert itself at the right position. This approach makes it easy to manage the view and relayout tags using a simple iteration flow.
Hi
If you still need an answer...
To get the size of text (and to then calculate the frame of the UILabel/UIButton etc) use the sizeWithFont: NSString function which will give you the width/height of a string of text using a specified font.
There is a little bit of maths that you'll need to do to work out the best fit, where to place the UILabels, and the spacing, but you will have the data that you need to do it.
Hope this helps!