Last active
April 27, 2022 13:33
-
-
Save HeathHopkins/8289364 to your computer and use it in GitHub Desktop.
Xamarin.iOS Extension Methods
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
using System; | |
using MonoTouch.UIKit; | |
namespace System | |
{ | |
public static class ExtensionMethods | |
{ | |
public static UIColor ToUIColor(this string hexString) | |
{ | |
hexString = hexString.Replace("#", ""); | |
if (hexString.Length == 3) | |
hexString = hexString + hexString; | |
if (hexString.Length != 6) | |
throw new Exception("Invalid hex string"); | |
int red = Int32.Parse(hexString.Substring(0,2), System.Globalization.NumberStyles.AllowHexSpecifier); | |
int green = Int32.Parse(hexString.Substring(2,2), System.Globalization.NumberStyles.AllowHexSpecifier); | |
int blue = Int32.Parse(hexString.Substring(4,2), System.Globalization.NumberStyles.AllowHexSpecifier); | |
return UIColor.FromRGB(red, green, blue); | |
} | |
public static SizeF StringSize(this UILabel label) | |
{ | |
return new NSString(label.Text).StringSize(label.Font); | |
} | |
public static void HideGradientBackground(this UIView view) | |
{ | |
try | |
{ | |
foreach(var subview in view.Subviews) | |
{ | |
if(subview.GetType() == typeof(UIImageView)) | |
subview.Hidden = true; | |
subview.HideGradientBackground(); | |
} | |
} | |
catch | |
{ | |
// nothing to do | |
} | |
} | |
/// <summary> | |
/// Returns a lighter color | |
/// </summary> | |
/// <param name="steps">The number of steps to lighten.</param> | |
public static UIColor Lighten(this UIColor color, int steps) | |
{ | |
int modifier = 16 * steps; | |
float rF, gF, bF, aF; | |
color.GetRGBA(out rF, out gF, out bF, out aF); | |
int r, g, b, a; | |
r = (int)Math.Ceiling(rF * 255); | |
g = (int)Math.Ceiling(gF * 255); | |
b = (int)Math.Ceiling(bF * 255); | |
a = (int)Math.Ceiling(aF * 255); | |
r += modifier; | |
g += modifier; | |
b += modifier; | |
// leave 'a' alone? | |
r = r > 255 ? 255 : r; | |
g = g > 255 ? 255 : g; | |
b = b > 255 ? 255 : b; | |
return UIColor.FromRGBA(r, g, b, a); | |
} | |
/// <summary> | |
/// Returns a darker color | |
/// </summary> | |
/// <param name="steps">The number of steps to darken.</param> | |
public static UIColor Darken(this UIColor color, int steps) | |
{ | |
int modifier = 16 * steps; | |
float rF, gF, bF, aF; | |
color.GetRGBA(out rF, out gF, out bF, out aF); | |
int r, g, b, a; | |
r = (int)Math.Ceiling(rF * 255); | |
g = (int)Math.Ceiling(gF * 255); | |
b = (int)Math.Ceiling(bF * 255); | |
a = (int)Math.Ceiling(aF * 255); | |
r -= modifier; | |
g -= modifier; | |
b -= modifier; | |
// leave 'a' alone? | |
r = r < 0 ? 0 : r; | |
g = g < 0 ? 0 : g; | |
b = b < 0 ? 0 : b; | |
return UIColor.FromRGBA(r, g, b, a); | |
} | |
} | |
} |
I think the intent must be to double each digit, right? So:
#333 => 3+3, 3+3, 3+3 => #333333
#123 => 1+1, 2+2, 3+3 => #112233
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Line 13 is wrong:
It works for #333, but doesn't work for #123 where #123123 - is a different color.