Attractive iOS UIButtons at runtime
I have been using the Glass Button Generator from here to generate PNGs on iOS that look like UIGlassButtons yet don't use the UIGlassButton class (since it's private). These are then set as the image for custom UIButton instances. Works really nicely and looks great.
However I am now in a situation where I need to set the text on the buttons dynamically at runtime. The above approach won't work. I also need to dynamically set the size of the button in at least the vertical dimension.
UIButton as-is looks pretty ugly and flat. There must be some way of generating attractive buttons at runtime开发者_运维技巧?
Non monotouch-specific solutions welcome, easy enough to translate.
Cheers in advance.
I wanted to write my own GlassButton for a while, feel free to cut and paste it into your code, I made it part of my MonoTouch.Dialog library:
https://github.com/migueldeicaza/MonoTouch.Dialog/blob/master/MonoTouch.Dialog/Utilities/GlassButton.cs
You use it like this:
var glossy = new GlassButton (new RectangleF (0, 0, 120, 50)) {
NormalColor = UIColor.Red,
HighlightedColor = UIColor.Black,
DisabledColor = UIColor.Black
};
glossy.Tapped += delegate {
Console.WriteLine ("I have been tapped");
}
window.AddSubview (glossy);
Subclass UIButton and add a UILabel as a subview. This would be easier to do in straight C# than messing with Interface Builder.
Expose it's text property, and maybe others such as the font size, etc.
That should be all you need to get it working.
You can just put the button without nothing, and then, paint a UILabel with the text, the background of the UILabel with clearColor, put the UILabel just above the button, it will look like a part of the button.
精彩评论