Created
November 20, 2014 22:22
-
-
Save RedTahr/28ba31087b9dbddba4ff to your computer and use it in GitHub Desktop.
iOS helper functions in Xamarin
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
// http://stackoverflow.com/questions/10310917/uicolor-from-hex-in-monotouch | |
public static UIColor GetColorFromHexString(string hexValue) { | |
hexValue = hexValue.Substring(1, 6); // string will be passed in with a leading # | |
var r = Convert.ToByte(hexValue.Substring(0, 2), 16); | |
var g = Convert.ToByte(hexValue.Substring(2, 2), 16); | |
var b = Convert.ToByte(hexValue.Substring(4, 2), 16); | |
return UIColor.FromRGB(r, g, b); | |
} |
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
//http://forums.xamarin.com/discussion/14269/objective-c-image-crop-function | |
public static UIImage CropSquare(UIImage image) { | |
var dimension = Math.Min((float)image.Size.Height, (float)image.Size.Width); | |
float x = ((float)image.Size.Width / 2) - (dimension / 2); | |
if (x < 0) { x = 0; } | |
float y = ((float)image.Size.Height / 2) - (dimension / 2); | |
if (y < 0) { y = 0; } | |
RectangleF rect = new RectangleF(x, y, dimension, dimension); | |
using (CGImage cgcrop = image.CGImage.WithImageInRect(rect)) { | |
UIImage cropped = UIImage.FromImage(cgcrop); | |
return cropped; | |
} | |
} |
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
//http://blog.xamarin.com/ios-6-7-compatibility-tips/ | |
public static bool IsiOS7OrHigher { | |
get { return UIDevice.CurrentDevice.CheckSystemVersion(7, 0); } | |
} | |
public static bool IsiOS8OrHigher { | |
get { return UIDevice.CurrentDevice.CheckSystemVersion(8, 0); } | |
} |
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
// bit hackish I feel, but works mostly | |
public static bool MoveToNextTextField(UITextField textField) { | |
int nextTag = textField.Tag + 1; | |
UIResponder nextResponder = textField.Superview.ViewWithTag(nextTag); | |
if(nextResponder != null) { | |
nextResponder.BecomeFirstResponder(); | |
} | |
else { | |
textField.ResignFirstResponder(); | |
} | |
return false; | |
} |
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 static string GetPicturesFolder() { | |
string picturesFolder; | |
//if (IsiOS8OrHigher) { // Xamarin suggested iOS8 support, but appears unnecessary at last time we checked | |
// picturesFolder = NSFileManager.DefaultManager.GetUrls(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomain.User)[0].ToString(); | |
//} | |
//else { | |
picturesFolder = Environment.GetFolderPath(Environment.SpecialFolder.Personal); | |
//} | |
return picturesFolder; | |
} |
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
//http://stackoverflow.com/questions/8344596/monotouch-uitableviewcell-height | |
// StringSize now deprecated. See this forum post http://forums.xamarin.com/discussion/8154/uitextview-contentheight, | |
// specifically this answer from a xamarin employee http://forums.xamarin.com/discussion/comment/29997/#Comment_29997 | |
// Table cells that don't use the full cell width need an adjustment. | |
public static float GetViewHeight(UIView view, string viewText, float fontSize = 12f, int viewWidthAdjustment = 0) { | |
if (Utils.IsiOS7OrHigher) { | |
var text = new NSMutableAttributedString(viewText); | |
text.AddAttribute(UIStringAttributeKey.Font, UIFont.SystemFontOfSize(fontSize), new NSRange(0, text.Length)); | |
var ctxt = new NSStringDrawingContext(); | |
var boundingRect = text.GetBoundingRect(new SizeF(view.Frame.Width - viewWidthAdjustment, float.MaxValue), NSStringDrawingOptions.UsesFontLeading | NSStringDrawingOptions.UsesLineFragmentOrigin, ctxt); | |
//Add some padding | |
return boundingRect.Height + 24; | |
} | |
else { | |
string item = viewText; | |
SizeF size = new SizeF(view.Frame.Width - viewWidthAdjustment, float.MaxValue); | |
// TODO: if the font size of the table row items change in the storyboard, make sure this changes too | |
float height = view.StringSize(item, UIFont.SystemFontOfSize(fontSize), size, UILineBreakMode.WordWrap).Height + 24; | |
return height; | |
} | |
} |
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
// scale image, maintain aspect ratio | |
public static UIImage Scale(UIImage image, int limitingDimension) { | |
float scaleFactor = Math.Max(limitingDimension / image.Size.Height, limitingDimension / image.Size.Width); | |
if(scaleFactor > 1) { | |
return image; | |
} | |
return image.Scale(new SizeF(image.Size.Width * scaleFactor, image.Size.Height * scaleFactor)); | |
} |
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
// Displays a UIAlertView and returns the index of the button pressed. | |
public static Task<int> ShowAlert(string title, string message, params string[] buttons) { | |
var tcs = new TaskCompletionSource<int>(); | |
var alert = new UIAlertView { | |
Title = title, | |
Message = message | |
}; | |
foreach(var button in buttons) | |
alert.AddButton(button); | |
alert.Clicked += (s, e) => tcs.TrySetResult(e.ButtonIndex); | |
alert.Show(); | |
return tcs.Task; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment