Swift button width depends on text length when adding title Insets - swift

hi i want to resize my button width by text length when adding title right insets
i tried set Constant 100 and Priority 1000 and it work perfect
but when i add image and right insets,
button title show like this
"apple...omputer"
and i tried this
button.invalidateIntrinsicContentSize()
and
button.sizeToFit()
still not work and still show "..." in my button text
and i tried to add width after set button title in viewdidload, but still not working
button.frame = CGRect(button.frame.origin.x, button.frame.origin.y, button.frame.width+50, button.frame.height)

Storyboard buttons
Resizing button because text became larger
Please look at screenshots above. You need to set your insets and set right constraints. Autolayout does it for you.

Related

custom keyboard UITextField inputView xcode

I am adding a custom keyboard to a view using inputView linked to a UITextField. I have set the size at Width 216, Height 320. I have it entering from the bottom on a portrait orientation. I want it to only cover 216 pixels of the screen but now when it comes up with the keyboard hugging the left side and has the correct aspect ratio. However there is a gray background that extends from the right edge of the keyboard to the right side of the view. Is there any way to remove this so it is clear and I can see the right side of the view? I have tried setting the background of the view to clear color and tried everything I can think of. Does anyone know if this is possible?
I guess you can't do it, because with keyboard opening the view moves up, so there are the "empty" rectangle in the 216x320 pts.

How to Align Label Text to the Top

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.

Return height between navBar and keyboard

I have got a tableViewController with one section and one cell.
How can I get the height between the navBar and the keyboard without any toolbar above it?
I tried this code: self.view.bounds.size.height
but I get the height of the entire view down the navBar...
I'd like to get this size to assign it to the cell and to the textView inside it.
Thanks :)
You can't get the height of the keyboard until it is displayed. You'll need to register for one of the keyboard notifications and then check the UIKeyboardFrameEndUserInfoKey value. This will give you the frame for the keyboard. Use this to adjust the height of your view.
I don't know what happens if you change the rowHeight property on a table that's already displayed, but my guess is that it should do what you want, once you compute the height to set it to.

UINavigationItem title label set width?

In my UIViewController i set my title dynamically so i do not know the length of the string that will be shown in the navigation bar and because of that i have the current situation:
alt text http://img94.imageshack.us/img94/2484/picture3vf.png
is there any way to set the width of the label that displays the title in the navigation bar? or should i think of a with, compare the text length to it and if it is too long should i resize it and display the famous "..." ?
I know that i can add an UILabel as subview to the navigationBar but i don't want to use that solution.
Thank you!
Maybe you could add some dummy buttons to the navigation bar that will sit directly underneath your buttons. That would show the normal text behavior, but also allow you to play with your colors.
I'm not sure how you did that. If I'm creating a navigation view and setting a title that is too long it will be shortened with "..." by default. These black buttons do not seem to be default buttons.
You can use sizeWithFont method of NSString to know the size your label will take. Note that you can add a maximum size if you know your label width shouldn't be greater than a certain value
// Get the size really needed for the label string
CGSize expectedLabelSize = [self.text sizeWithFont:self.font
constrainedToSize:maximumSize
lineBreakMode:self.lineBreakMode];

How to set height for a textfield in size inspector

i created a UITextField of type rounded textfield, using Interface Builder,so in order to set the Height of a textfield its window was disabled(not to set any height manually) in SizeInspector.so if we want to set height manually,what procedure should we follow.
One thing i would like to mention is ,we can do that in coding part,but what i would like to know is ,how can we acheive that in interfacebuilder.
Answers from you were always appreciated.
Change the border style, like this:
Unfortunately you can't set the Height of a UITextField using Interface Builder. You will have to set it programatically and be sure it doesn't break any rules from iPhone Human Interface Guidelines
You can change the height of UITextField by editing the .xib file. Just open it using Textedit or Dashcode and search for the IBUITextField. Edit to NSFrame parameter to whatever size u want.
increase the size of the font and the box's height will increase
Change the Border Style property of the text field to something other than the rounded rectangle. After that you can freely set the height of the box.
Right click on .xib file and click on "Open As"-> "Source Code". Then search for UITextField's NSFrame. Change the height as you want.
Successful solution with my condition:
After trying all above ways, I could not change the height of UITextField.
Finally, I found out that I put UITextField in stack view (or it was affected by some constraints).
So, I added Height Constraint for UITextField then put my expected height in Constant of Constraint.
And it works.
Specially, it works for all Border-style (None, Line, Bezel, RoundedRect).
No code is needed.
After a bit of playing around, I realized that UITextField can be thought of as a UIButton (Rounded rect style) with a UILabel on top of it. Or, it is a UITextView on top of UIButton Rounded rect style. The number of lines, dimensions and position of the label inside the button can be easily adjusted in IB.
In my case, I wanted a read only text-field so I used UILabel on top of UIButton. I adjusted the size of button as per requirement and also label size. I also had to uncheck the 'Enable' box for the button in IB so that it does not get highlighted on touching.
You can easily change that using source code. Just right click on your storyboard, open it as source code, then search for your text field and change the height.
The easiest way that i have found to accomplish this is to change the border style and then edit the other attributes to your liking. Here's an example.
emailField.layer.borderColor = [[UIColor blackColor] CGColor];
emailField.layer.borderWidth = 2.0f;
emailField.layer.cornerRadius = 8.0f;
emailField.alpha = .75f;
emailField.layer.backgroundColor = [[UIColor whiteColor] CGColor];
!
I resolved textfield (with rounded rect) height problem. You must define height constraint for your textfield object.
Define height constraint for Textfield.
We can change height of Text Field with any type of border style other than rounded rectangle by simply dragging. But for the Rounded Rectangle Text fields it is not possible...of course we can change height using some code.
And here I found a simple way. It worked for me
Select the text field and at the below right corner we can see the pins option..
There select the height constrain it is 30 pre-defined...
select that height and you can see "Add 1 Constraint" is enabled.
Click on that..therefore we have added height constraint for that text field.
Now go to the size inspector. There find the Height constraint in "Constraints"...which is above the "Content Hugging Priority"
You can edit the height there...give whatever value you want to give. Now see the textfield it is changed to its height which you have given.
It worked for me :) Can use this scenario for UISwitch, Segment, etc..
As #peterdoesco.de said, Just add an Height constraint, and change it's value, this can works fine.