Created
September 27, 2011 17:01
-
-
Save martinbowling/1245619 to your computer and use it in GitHub Desktop.
CustomNavButton & CustomNavBackButton
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CustomNavBarBackButton : UIButton | |
{ | |
private const float MAX_WIDTH = 160.0f; | |
private const int CapWidth = 14; | |
UIImage buttonImage; | |
string buttonTitle; | |
public event NSAction Tapped; | |
public CustomNavBarBackButton(UIImage ButtonImage) | |
{ | |
buttonImage = ButtonImage.StretchableImage(CapWidth,0); | |
PrepareButton(); | |
} | |
public CustomNavBarBackButton(UIImage ButtonImage, string ButtonTitle) | |
{ | |
buttonImage = ButtonImage.StretchableImage(CapWidth,0); | |
buttonTitle = ButtonTitle; | |
PrepareButton(); | |
} | |
public CustomNavBarBackButton(UIImage ButtonImage, string ButtonTitle, NSAction tapped) | |
{ | |
buttonImage = ButtonImage.StretchableImage(CapWidth,0); | |
buttonTitle = ButtonTitle; | |
if (tapped != null){ | |
Tapped += tapped; | |
} | |
PrepareButton(); | |
} | |
public void SetText(string buttonTitle) | |
{ | |
NSString bt; | |
if (!string.IsNullOrEmpty(buttonTitle)) { | |
SetTitle(buttonTitle, UIControlState.Normal); | |
bt = new NSString(buttonTitle); | |
} | |
else { | |
SetTitle("Back", UIControlState.Normal); | |
bt = new NSString("Back"); | |
} | |
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(); | |
}; | |
} | |
} | |
} | |
public class CustomNavBarButton : UIButton | |
{ | |
private const float MAX_WIDTH = 160.0f; | |
private const int CapWidth = 7; | |
UIImage buttonImage; | |
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; | |
} | |
PrepareButton(); | |
} | |
public void SetText(string buttonTitle) | |
{ | |
NSString bt; | |
if (!string.IsNullOrEmpty(buttonTitle)) { | |
SetTitle(buttonTitle, UIControlState.Normal); | |
bt = new NSString(buttonTitle); | |
} | |
else { | |
SetTitle("Back", UIControlState.Normal); | |
bt = new NSString("Back"); | |
} | |
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(); | |
}; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment