UINavigationItem title label set width? - iphone

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];

Related

Swift button width depends on text length when adding title Insets

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.

iOS 5 UIButton title showing with ellipsis

I have a problem regarding the UIButton title in iOS5. They are clipped and show like in this photo. I don't want them to be clipped, i want to show the full title.
In iOS6, they work perfectly.
Please tell me how can i resolve this?
you simply need to increase the width of your UIButton to show full Title, i have posted sample snippet for better idea, try it. You just need to increase the width of button.
UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn1 setFrame:CGRectMake(20, 200, 150, 25)];
[btn1 setTitle:#"This is Long Title" forState:UIControlStateNormal];
[self.view addSubview:btn1];
Guys i have found the solution.
It was pretty simple, you just need to set the button font size in the interface builder to a larger or the same font size than that of the UIapperance font proxy.
I think AutoLayout may be the culprit for this. Check to see if your constraints aren't forcing the label to shrink.
I assume that you are setting your custom font from the code but you are setting the text for the buttons from the IB. So my simple explanation is that as #MichaelScaria said, your custom font is larger. The text is clipping because the size of the label inside the button is adjusted for the current font and size. Since you are changing the font and size after the text is set you need to re-set the title of the button or call 'sizeToFit' on that button.
For those of you here due to ellipsis and use of UIBarButtonItem know that there is a possibleTitles property on UIBarButtonItem with docs that read:
Use this property to provide a hint to the system on how to correctly size the bar button item to be wide enough to accommodate your widest title. Set the value of this property to an NSSet object containing all the titles you intend as possible titles for the bar button item. Use the actual text strings you intend to display.
This property applies to bar button items placed on navigation bars or toolbars.

how to set the ticker for navigation item title using Monotouch?

I have set the title for navigation item on top of the screen. But the title text exceeded from the screen width. So I need scrolling title on the top of the screen. How to do this with MonoTouch? Is it possible?
You will most likely have to set a custom view for the text in the UINavigationItem's toolbar.
I have done similar with UIBarButtonItems, basically I created a UILabel with a width and centered text and set it as the custom view for it.
Take a look at this example, it's in Objective-C but you get the idea.

Customizing icon position and title of UITabBarItem's (iphone sdk)

I have a tabBar, and I want to customise the items in the tab bar. I want to prevent the display of the title (easily achieved by setting the title to nil), and reset the icon position to be in the central vertical position. Does anyone have any idea of how this can be done?
Thanks.
Ugly hacky "solution": set top image inset to positive value, bottom inset to equal but negative value.
As MM says, use the item's imageInsets property:
tabBarItem.imageInsets = UIEdgeInsetsMake(offset, 0, -offset, 0);
You can try to use UIToolBar instead of UITabBar. UIBarButtonItem has method initWithCustomView. This is more complicated but may work properly.

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.