I need to horizontally center layout a UITextField, however, since the clear button is part of it, the (text entry portion of) text field doesn't look centered - the clear button isn't always visible but it is taken into account when laying out.
In the image above, the UITextField is horizontally centered, however, without the clear button (X on the right) and with only search text, it doesn't look like centered.
A way I can think of, is to subclass UITextField and provide alignmentRectInsets, for inset on the right I would use clearButtonRect(forBounds:) to get the size of clear button, whose width will be the right inset for alignmentRectInsets.
Question:
Apple's doc specifically mentions that one should not call clearButtonRect(forBounds:) directly, so I'm feeling a bit nervous for doing it; However, from the doc it feels like Apple's intention is to let people not changing the rect, and in my case I'm just getting its size, I guess it's fine?
Is there any better way of achieving this? I know I can tweak edgeInsets, or give the text field a leftView, to make the text field looking centered; But they all need some hardcoded assumption for the size of the clear button.
Thanks!
You can make the UITextField Alignment settings as middle then it will work. Check the below image
You can control over here.It may helps to you.Thank you.
Related
I have always had this problem and could never figure out how to solve it. My issue is that I want to produce a similar thing as the photo: Image
I want to be able to have text fields connect like this and be able to have a 'label' on the left while still being able to input on the right. Any help is greatly appreciated.
If I am not mistaken, I think that what you want is to have a label on the left and a textview on the right. The image you have supplied looks like the textViews and labels reside within a UITableView controller as static cells. What they have most likely have done here is simply extended the label from the left to the centre and the textview from the right to the centre, such that each takes up half the cell (and added constraints). Then you can set the text alignment to left for the label and right for the textview. The final thing you should do is get rid of the border of the textview, which can be found in the attributes inspector and is called Border Style. You want to set it to the far left option, like this:
As the background of the cell is white and the textview's also, you shouldn't need to change it, but if you do there is an attribute for that a bit further down that you can have a play with.
I've been doing iOS for a while now, but when it comes to dynamically hiding / showing elements, I'm a bit lost.
Coming from Android, I'm used to being able to simply set views to visibility gone, but this doesn't exist on iOS.
So let's say I have the following scenario:
Basically I want to have a table, but the table should not fill the entire view controller. Instead it should leave place for optionally either a button, a multiline label, or possibly both at the bottom (if visible, these should be fixed, not scroll).
One way to solve this would be to use auto layout and modify constraints, like adding a zero height constraint. But that would make iOS kill one of the other constraints, which would make it hard to change it again. For the label, I wouldn't always want to have a height constraint, because it could be multiline, and should take the size it needs.
Maybe it's easier to skip autolayout here and modify frames instead, I don't know.
My question is: What approach would be best here?
Is there some other way of doing this I haven't thought of, or do I have to try to do what I described above?
I'm not primarily looking for code (code can be ok), but I'm more interested in a description of how it can be done.
I'd like to support iOS 7.
This problem had a variety of solutions, and opinion based, but I'm facing such questions a lot, when I don't know what to choose and what would be the "right thing".
So, I my opinion, the best solution here is using autolayout, you need to set height of label manually, but you have a few methods for this, at least you can play with it and if you don't succeed ask question about it. Using frames, you'll face same problem of calculating height, right? But with auto layout, you only need to set height, vertical space to 0, when you need to hide message.
You can also use constrains with priority lower 1000, and remove completely constraints from message (button, label) if you don't need it at all anymore.
For example, taking your layout image, you can make UIView with subviews: button, label. Top constraint connect to the UITableView, other constraints to the sides.Label and button will calculate the view's height. The only question here is label height.
So in ios assuming that the background of both these objects is opaque only the front most view in the Heirarchy will be visible and interactable, An easy solution would be to change the different frames of these two things you need and make sure they are in the back of your view heirachy, and when you need them to appear use view.bringSubviewToFront(mySubview) and view.pushSubviewToBack(mySubview) to make it disappear again. View obviously would be referring to main view of your view controller.
I am having a usability issue where in an app screen, there may be too much text and I have nowhere to put that text.
I am attaching a screen shot of my screen. You see how the text on top has more text, but there is no room to put that text. So the text just ends in ....
What is the common way people handle this kind of a situation?
Thanks!
Redesign your UI so you have more room for that text. Maybe your content will need to scroll. You will need to change that label so that the max number of lines is greater (or 0 for unlimited), and make the frame larger to accomodate more lines. You can also reduce the font size a little to decrease how much extra space you'll need to give it.
If you really wanted to, you could use a UITextView for that text, which allows the text to scroll easily (it's a UIScrollView subclass). Then you could just scroll that text, though it wouldn't be very good UI and you would still want it to be more than one line, because scrolling when only one line is visible at a time would make it hard to read.
I am trying to create a textview that holds a 3 line string. As the user works, the string will update and become wider, the height (number of lines) will remain the same. Reading through the documentation I understand that the size of the container cannot change, it is readonly.
I have code that places text into the text property of a UITextView but it will not scroll the text right or left. I played around with inserting a UIView into the textview and believe that will work, but doesn’t seem like the way to about this. I’m kind of figuring that I need a custom UITextView class and a custom NSTextContainer. Wondering if the proper direction? Any help is appreciated.
Thanks,
Jeff
I want to have a check box, like the standard one (first the check box and on the right its title) but I want it to be aligned from the right. If I change the alignment to the right then only the text will move to the right but the button itself will still be at the left.
How would I achieve this without knowing the bounds of the NSButton?
Read about Interface Layout in the Interface Builder user guide. As it turns out, just make your checkbox as big as you need to initially, and then set the struts and springs to make it resize correctly as it changes.
To do the same thing in code, read about the setAutoresizingMask: method in NSView.