Skip to content

Instantly share code, notes, and snippets.

@martinbowling
Created September 27, 2011 16:58
Show Gist options
  • Save martinbowling/1245610 to your computer and use it in GitHub Desktop.
Save martinbowling/1245610 to your computer and use it in GitHub Desktop.
CustomNavButton
using System;
using System.Drawing;
using MonoTouch.CoreGraphics;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using MonoTouch.ObjCRuntime;
namespace SWVCVB
{
public class CustomNavBarButton : UIButton
{
private const float MAX_WIDTH = 160.0f;
private const int CapWidth = 7;
UIImage buttonImage;
UIImage selectedImage;
string buttonTitle = "";
public event NSAction Tapped;
public CustomNavBarButton(UIImage ButtonImage)
{
buttonImage = ButtonImage.StretchableImage(CapWidth,0);
PrepareButton();
}
public CustomNavBarButton(UIImage ButtonImage, string ButtonTitle)
{
buttonImage = ButtonImage.StretchableImage(CapWidth,0);
buttonTitle = ButtonTitle;
PrepareButton();
}
public CustomNavBarButton(UIImage ButtonImage, string ButtonTitle, NSAction tapped)
{
buttonImage = ButtonImage.StretchableImage(CapWidth,0);
buttonTitle = ButtonTitle;
if (tapped != null){
Tapped += tapped;
}
if (!string.IsNullOrEmpty (ButtonTitle)) {
PrepareButton();
}
else {
PrepareButtonNoText();
}
}
public CustomNavBarButton(UIImage ButtonImage, UIImage SelectedImage, NSAction tapped)
{
buttonImage = ButtonImage; //.StretchableImage(CapWidth,0);
selectedImage = SelectedImage;
if (tapped != null){
Tapped += tapped;
}
PrepareButtonNoText();
}
public void SetText(string buttonTitle)
{
NSString bt;
if (!string.IsNullOrEmpty(buttonTitle)) {
SetTitle(buttonTitle, UIControlState.Normal);
bt = new NSString(buttonTitle);
}
else {
SetTitle("", UIControlState.Normal);
bt = new NSString("");
}
SizeF textSize = bt.StringSize(TitleLabel.Font);
if ((textSize.Width + (CapWidth * 1.5)) > MAX_WIDTH){
Frame = new RectangleF(Frame.X, Frame.Y, MAX_WIDTH,Frame.Size.Height);
}
else {
Frame = new RectangleF(Frame.X,Frame.Y,textSize.Width + (CapWidth * 1.5f), Frame.Size.Height);
}
}
private void PrepareButton()
{
//= UIButton.FromType(UIButtonType.Custom);
TitleLabel.Font = UIFont.SystemFontOfSize(UIFont.SmallSystemFontSize);
TitleLabel.TextColor = UIColor.White;
TitleLabel.ShadowColor = UIColor.DarkGray;
TitleLabel.ShadowOffset = new SizeF(0,-1);
TitleLabel.LineBreakMode = UILineBreakMode.TailTruncation;
TitleEdgeInsets = new UIEdgeInsets(0f,6.0f,0f,3.0f);
Frame = new RectangleF(0,0,0,buttonImage.Size.Height);
SetText(buttonTitle);
SetBackgroundImage (buttonImage, UIControlState.Normal);
if (Tapped != null){
TouchUpInside += delegate(object sender, EventArgs e) {
Tapped();
};
}
}
private void PrepareButtonNoText()
{
//= UIButton.FromType(UIButtonType.Custom);
//TitleLabel.Font = UIFont.SystemFontOfSize(UIFont.SmallSystemFontSize);
//TitleLabel.TextColor = UIColor.White;
//TitleLabel.ShadowColor = UIColor.DarkGray;
//TitleLabel.ShadowOffset = new SizeF(0,-1);
//TitleLabel.LineBreakMode = UILineBreakMode.TailTruncation;
//TitleEdgeInsets = new UIEdgeInsets(0f,6.0f,0f,3.0f);
Frame = new RectangleF(0,0,buttonImage.Size.Width,buttonImage.Size.Height);
//SetText(buttonTitle);
SetBackgroundImage (buttonImage, UIControlState.Normal);
SetBackgroundImage(selectedImage, UIControlState.Selected);
if (Tapped != null){
TouchUpInside += delegate(object sender, EventArgs e) {
Tapped();
};
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment