forked from jonathanfontaine/iOSMaterialShowcase.Xamarin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utility.cs
72 lines (66 loc) · 2.23 KB
/
Utility.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
using CoreGraphics;
using UIKit;
using CoreAnimation;
using System;
using EventKit;
namespace iOSMaterialShowcase.Xamarin
{
public class Utility
{
}
public static class UIViewExtension
{
public static void AsCircle(this UIView _view)
{
_view.Layer.CornerRadius = _view.Frame.Width * .5f;
_view.Layer.MasksToBounds = true;
}
public static void AsCircleWithTransparentCenter (this UIView _view, UIColor backgroundColor, float targetRadius)
{
var largeRadius = _view.Frame.Width * .5f;
var path = new UIBezierPath ();
path.AddArc (new CGPoint(_view.Bounds.GetMidX(), _view.Bounds.GetMidY()), (targetRadius + largeRadius) * .5f, 0f, (float)Math.PI * 2, true);
var shape = new CAShapeLayer ();
shape.Frame = _view.Bounds;
shape.Path = path.CGPath;
shape.LineWidth = largeRadius - targetRadius;
shape.StrokeColor = backgroundColor.CGColor;
shape.FillColor = UIColor.Clear.CGColor;
_view.Layer.AddSublayer(shape);
_view.Layer.MasksToBounds = true;
}
public static void SetTintColor(this UIView _view, UIColor _color, bool _recursive)
{
if (_recursive)
{
_view.TintColor = _color;
foreach (var view in _view.Subviews)
view.SetTintColor(_color, true);
}
else
_view.TintColor = _color;
}
}
public static class CGRectExtension
{
public static CGPoint Center(this CGRect _rect)
{
return new CGPoint(_rect.GetMidX(), _rect.GetMidY());
}
}
public static class UILabelExtension
{
public static void SizeToFitHeight(this UILabel _label)
{
UILabel tempLabel = new UILabel(new CGRect(0, 0, _label.Frame.Width, float.MaxValue))
{
Lines = _label.Lines,
LineBreakMode = _label.LineBreakMode,
Font = _label.Font,
Text = _label.Text
};
tempLabel.SizeToFit();
_label.Frame = new CGRect(_label.Frame.GetMinX(), _label.Frame.GetMinY(), _label.Frame.Width, tempLabel.Frame.Height);
}
}
}