using System;
using UIKit;
using CoreGraphics;
namespace Shared
{
///
/// Loading UI
///
public class Loading : View
{
public bool CurStatus = false;
internal IosLoading iosLoading
{
get
{
return uiView as IosLoading;
}
set
{
uiView = value;
}
}
///
/// Loading UI
///
public Loading()
{
var bounds = UIScreen.MainScreen.Bounds;
iosLoading = new IosLoading(bounds);
}
///
/// 开始当前视图
///
public void Start()
{
Text = "Loading......";
iosLoading.Start();
CurStatus = true;
}
///
/// 开始当前视图
///
public void Start(string s)
{
Text = s;
iosLoading.Start();
}
///
/// 隐藏当前视图
///
public void Hide()
{
Text = "Loading......";
iosLoading.Hide();
CurStatus = false;
}
public string Text
{
get
{
return iosLoading.uiLabel.Text;
}
set
{
iosLoading.uiLabel.Text = value;
iosLoading.RefreshWidth();
}
}
uint mTextColor;
public uint TextColor
{
get
{
return mTextColor;
}
set
{
mTextColor = value;
iosLoading.uiLabel.TextColor = HDLUtils.GetUIColorWithUint(mTextColor);
}
}
public virtual uint LodingBackgroundColor
{
get
{
return (iosLoading as IosLoading).LodingBackgroundColor;
}
set
{
(iosLoading as IosLoading).LodingBackgroundColor = value;
}
}
///
///
///
/// 1 为灰色
public void SetUIActivityIndicatorViewStyle(int mViewStyle)
{
(iosLoading as IosLoading).SetUIActivityIndicatorViewStyle(mViewStyle);
}
///
/// 文字大小
///
/// The size of the text.
public float TextSize
{
get
{
return (float)iosLoading.uiLabel.Font.PointSize;
}
set
{
iosLoading.uiLabel.Font = UIFont.FromName(iosLoading.uiLabel.Font.Name, value);
}
}
}
internal class IosLoading : UIView
{
UIActivityIndicatorView activitySpinner = new UIActivityIndicatorView(UIActivityIndicatorViewStyle.WhiteLarge);
public UILabel uiLabel = new UILabel();
UIView backUIView = new UIView(new CGRect(0, 0, 160, 120));
public IosLoading(CGRect frame)
{
BackgroundColor = UIKit.UIColor.FromRGBA(0x32, 0x32, 0x32, 50);
Frame = frame;
Hidden = true;
AutoresizingMask = UIViewAutoresizing.All;
var f = backUIView.Frame;
f.Width = frame.Width;
//f.Height = frame.Height;
backUIView.Frame = f;
var center = backUIView.Center;
center.X = Frame.Width / 2;
center.Y = Frame.Height / 2;
backUIView.Center = center;
AddSubview(backUIView);
// create the activity spinner, center it horizontall and put it 5 points above center x
var center1 = activitySpinner.Center;
center1.X = backUIView.Frame.Width / 2;
center1.Y = (backUIView.Frame.Height - 62) / 2 + 20;
activitySpinner.Center = center1;
backUIView.AddSubview(activitySpinner);
activitySpinner.BackgroundColor = UIColor.Clear;
activitySpinner.Color = UIColor.Gray;
var frame2 = uiLabel.Frame;
frame2.Y = backUIView.Frame.Height - 42;
frame2.Width = backUIView.Frame.Width;
frame2.Height = 22;
uiLabel.Frame = frame2;
uiLabel.BackgroundColor = UIColor.Clear;
uiLabel.TextColor = UIColor.Gray;
uiLabel.TextAlignment = UITextAlignment.Center;
//uiLabel.F
backUIView.AddSubview(uiLabel);
}
public void Start()
{
if (Superview == null)
{
return;
}
Hidden = false;
activitySpinner.StartAnimating();
Superview.BringSubviewToFront(this);
}
///
/// Fades out the control and then removes it from the super view
///
public void Hide()
{
if (Superview == null)
{
return;
}
Hidden = true;
activitySpinner.StopAnimating();
Superview.SendSubviewToBack(this);
}
uint loadingBackgroundColor;
///
/// 背景颜色
///
/// The color of the background.
public virtual uint LodingBackgroundColor
{
get
{
return loadingBackgroundColor;
}
set
{
loadingBackgroundColor = value;
if (this.GetType() != typeof(ImageView))
{
byte r, g, b, a;
r = (byte)(loadingBackgroundColor / 256 / 256 % 256);
g = (byte)(loadingBackgroundColor / 256 % 256);
b = (byte)(loadingBackgroundColor % 256);
a = (byte)(loadingBackgroundColor / 256 / 256 / 256 % 256);
backUIView.BackgroundColor = UIKit.UIColor.FromRGBA(r, g, b, a);
backUIView.Layer.MasksToBounds = true;
backUIView.Layer.CornerRadius = 6;
backUIView.Layer.BorderWidth = 0;
}
}
}
public void SetUIActivityIndicatorViewStyle(int mViewStyle)
{
if (mViewStyle == 1)
{
activitySpinner.ActivityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray;
}
else
{
activitySpinner.ActivityIndicatorViewStyle = UIActivityIndicatorViewStyle.WhiteLarge;
}
}
public void RefreshWidth()
{
int maxTextWidth = GetTextWidth() + 40;
if (maxTextWidth > this.Frame.Width - 20)
{
maxTextWidth = (int)this.Frame.Width - 20;
}
else if (maxTextWidth < 120)
{
maxTextWidth = 120;
}
var mFrame = backUIView.Frame;
if ((int)mFrame.Width == maxTextWidth)
{
return;
}
mFrame.Width = maxTextWidth;
//f.Height = frame.Height;
backUIView.Frame = mFrame;
var center = backUIView.Center;
center.X = Frame.Width / 2;
center.Y = Frame.Height / 2;
backUIView.Center = center;
var center1 = activitySpinner.Center;
center1.X = backUIView.Frame.Width / 2;
activitySpinner.Center = center1;
var frame2 = uiLabel.Frame;
frame2.Width = backUIView.Frame.Width;
uiLabel.Frame = frame2;
}
///
/// 获取字体长度
///
int GetTextWidth()
{
try
{
CGSize fontSize = uiLabel.Text.StringSize(uiLabel.Font);
return (int)fontSize.Width;
}
catch
{
return 0;
}
}
}
}