Reputation: 20289
I want an UIBarButonSystemItem
with another text (similar to e.g. UIBarButtonSystemItem.Cancel
) on my bottom toolbar.
This is what I have now (Code is in C# but it doesn't matter because you could provide your solution in Objective-C):
UILabel markLabel = new UILabel (new RectangleF(0,0,150,30));
markLabel.Text = "Mark";
UIView containerView = new UIView (new RectangleF(0,0,150,30));
containerView.AddSubview (markLabel);
var markButton = new UIBarButtonItem (markLabel);
How should such a button be created? Using an UIButton
(I failed here) or an UIView
with UILabel
as above? Also I have always to provide the frame properties. What is if you want to use Auto-Layout? How can the size be adapted to changing content size (e.g. internationalization) and what about the spacing element?
Edit:
This now works with UIButton
:
var temp = UIButton.FromType (UIButtonType.System);
temp.SetTitle ("Mark", UIControlState.Normal);
temp.SizeToFit ();
var markButton = new UIBarButtonItem (temp);
But the color and font size has to be adapted. Some problem still remains: If the text gets wider it doesn't shrink the font size even AdjustsFontSizeToFitWidth
is used. Also the positioning is different. E.g. the cancel button has a different spacing to the border of the screen than my button.
Upvotes: 0
Views: 626
Reputation: 619
A button can be made using UIButton
like this:
UIButton button = new UIButton (new RectangleF(5, 5, 30, 30));
button.SetTitle ("Hello, world", UIControlState.Normal);
In this example, the frame properties are hard-coded. If you want to make sizes auto-adjustable, you should add properties to a class that can be called from any other class (like the AppDelegate).
public static float ScreenWidth { get; set; }
public static float ScreenHeight { get; set; }
Then, add a call to the new properties in the ViewWillAppear
of the first view that will appear on your screen (and update frame sizes afterwards), and in the ViewDidRotate
method in all classes where you want controls to be auto-adjustable (and also update frame sizes afterwards):
public override void DidRotate (UIInterfaceOrientation fromInterfaceOrientation)
{
base.DidRotate (fromInterfaceOrientation);
AppDelegate.ScreenWidth = this.View.Bounds.Width;
AppDelegate.ScreenHeight = this.View.Bounds.Height;
UpdateFrameSize ();
}
Fontsize can be altered with the button.Font
property:
button.Font = UIFont.SystemFontOfSize (12);
Sadly, there's no option to wrap text in a button. If you want text to be displayed and wrapped to fit the screen, you should use a UILabel
next, above or beneath the button that will display the text you want to display. You can use the UILabel.Lines
and UILabel.LineBreakMode
properties of an UILabel.
lblClient.Lines = 5;
lblClient.LineBreakMode = UILineBreakMode.WordWrap;
Experiment a bit with these properties and methods, and see what fits best to your project. If something is not clear, or you wish some more explanation on the problems in your example, don't hesitate to ask. Good luck!
Upvotes: 1