-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExtensions.cs
95 lines (75 loc) · 2.82 KB
/
Extensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
using Foundation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UIKit;
namespace Docutain_SDK_Example_Xamarin_iOS
{
internal static class Extensions
{
internal static string Localized(this string str)
{
return NSBundle.MainBundle.GetLocalizedString(str);
}
internal static string NameOfClass(this NSObject obj)
{
return obj.GetType().Name;
}
internal static string Identifier(this UITableViewCell cell)
{
return cell.NameOfClass();
}
internal static void RegisterCell<T>(this UITableView tableView) where T : UITableViewCell
{
tableView.RegisterClassForCellReuse(typeof(T), new NSString(typeof(T).Name));
}
internal static T DequeueCell<T>(this UITableView tableView) where T : UITableViewCell
{
var cell = tableView.DequeueReusableCell(new NSString(typeof(T).Name)) as T;
return cell;
}
internal static UIColor ToUIColor(this string hexString)
{
hexString = hexString.TrimStart('#').Trim();
if (hexString.Length != 6)
{
throw new ArgumentException("Invalid hex string.", nameof(hexString));
}
var r = Convert.ToInt32(hexString.Substring(0, 2), 16);
var g = Convert.ToInt32(hexString.Substring(2, 2), 16);
var b = Convert.ToInt32(hexString.Substring(4, 2), 16);
return UIColor.FromRGB(r, g, b);
}
internal static string ToHexString(this UIColor color)
{
nfloat r, g, b, a;
color.GetRGBA(out r, out g, out b, out a);
var rInt = (int)(r * 255);
var gInt = (int)(g * 255);
var bInt = (int)(b * 255);
return $"#{rInt:X2}{gInt:X2}{bInt:X2}";
}
internal static (UIColor light, UIColor dark) GetColors(this Dictionary<string, string> dictionary)
{
var lightColor = dictionary["light"].ToUIColor();
var darkColor = dictionary["dark"].ToUIColor();
return (lightColor, darkColor);
}
internal static Dictionary<string, string> ToStringDict (this NSDictionary dictionary)
{
Dictionary<string, string> retVal = new Dictionary<string, string>((int)dictionary.Count);
foreach (KeyValuePair<NSObject, NSObject> tuple in dictionary)
{
retVal.Add(tuple.Key.ToString(), tuple.Value.ToString());
}
return retVal;
}
public static string ToLowerFirstChar(this string input)
{
if (string.IsNullOrEmpty(input))
return input;
return char.ToLower(input[0]) + input.Substring(1);
}
}
}