using UIKit;
|
using Foundation;
|
using Shared.IO;
|
using System;
|
using CoreGraphics;
|
|
namespace Shared
|
{
|
/// <summary>
|
/// Button 按键
|
/// </summary>
|
public class Button : View
|
{
|
MyButton iosButton
|
{
|
get
|
{
|
return uiView as MyButton;
|
}
|
set
|
{
|
uiView = value;
|
}
|
}
|
|
/// <summary>
|
/// Initializes a new instance of the <see cref="Shared.Button"/> class.
|
/// </summary>
|
public Button()
|
{
|
iosButton = new MyButton(this) { };
|
iosButton.TitleLabel.Font = UIFont.FromName(FontName, Application.FontSize);
|
}
|
|
byte[] imageBytes;
|
public byte[] ImageBytes {
|
get {
|
return imageBytes;
|
}
|
set {
|
imageBytes = value;
|
IsSelected = isSelected;
|
}
|
}
|
|
int textID;
|
/// <summary>
|
/// 根据ID获取对应的备注
|
/// </summary>
|
/// <value>The text I.</value>
|
public int TextID
|
{
|
get
|
{
|
return textID;
|
}
|
set
|
{
|
textID = value;
|
Text = Language.StringByID(TextID);
|
}
|
}
|
|
/// <summary>
|
/// 文本
|
/// </summary>
|
/// <value>The text.</value>
|
public string Text
|
{
|
get
|
{
|
return iosButton.Title(UIKit.UIControlState.Normal);
|
}
|
set
|
{
|
|
iosButton.SetTitle(value, UIKit.UIControlState.Normal);
|
}
|
}
|
|
/// <summary>
|
/// 字体名称
|
/// </summary>
|
/// <value>The name of the font.</value>
|
public string FontName{
|
get{
|
return iosButton.TitleLabel.Font.Name;
|
}
|
set{
|
iosButton.TitleLabel.Font = UIFont.FromName(value, iosButton.Font.PointSize);
|
}
|
}
|
|
public bool IsMoreLines {
|
get {
|
return iosButton.TitleLabel.Lines != 1 ? true : false;
|
}
|
set {
|
//IsMoreLines = value;
|
iosButton.TitleLabel.Lines = value ? 0 : 1;
|
}
|
}
|
|
/// <summary>
|
/// 是否显示粗体
|
/// </summary>
|
bool isBold;
|
public bool IsBold
|
{
|
get
|
{
|
return isBold;
|
}
|
set
|
{
|
isBold = value;
|
if (isBold)
|
{
|
iosButton.TitleLabel.Font = UIFont.FromName("Helvetica-Bold", mTextSize);
|
}
|
else
|
{
|
iosButton.TitleLabel.Font = UIFont.FromName("Helvetica", mTextSize);
|
}
|
}
|
}
|
|
/// <summary>
|
/// 获取字体长度
|
/// </summary>
|
public int GetTextWidth()
|
{
|
int textWidth = 0;
|
CGSize fontSize = this.Text.StringSize(iosButton.TitleLabel.Font);
|
textWidth = (int)fontSize.Width;
|
|
return textWidth;
|
}
|
|
/// <summary>
|
/// 创新需要创新的信息
|
/// </summary>
|
public override void Refresh()
|
{
|
base.Refresh();
|
IsSelected = isSelected;
|
}
|
|
float mTextSize = Application.FontSize;
|
/// <summary>
|
/// 文字大小,默认12
|
/// </summary>
|
/// <value>The size of the text.</value>
|
public float TextSize
|
{
|
get
|
{
|
return (float)iosButton.TitleLabel.Font.PointSize;
|
}
|
set
|
{
|
mTextSize = value;
|
iosButton.TitleLabel.Font = UIFont.FromName(FontName, value);
|
}
|
}
|
|
|
uint textColor=0xFFFFFFFF;
|
/// <summary>
|
/// 文字颜色
|
/// </summary>
|
/// <value>The color of the text.</value>
|
public uint TextColor
|
{
|
get
|
{
|
return textColor;
|
}
|
set
|
{
|
textColor = value;
|
if (IsSelected)
|
{
|
return;
|
}
|
byte r, g, b, a;
|
r = (byte)(textColor / 256 / 256 % 256);
|
g = (byte)(textColor / 256 % 256);
|
b = (byte)(textColor % 256);
|
a = (byte)(textColor / 256 / 256 / 256 % 256);
|
iosButton.SetTitleColor(UIKit.UIColor.FromRGBA(r, g, b, a), UIControlState.Normal);
|
}
|
}
|
|
uint selecteTextColor = 0xFFFFFFFF;
|
/// <summary>
|
/// 文字颜色
|
/// </summary>
|
/// <value>The color of the text.</value>
|
public uint SelectedTextColor
|
{
|
get
|
{
|
return selecteTextColor;
|
}
|
set
|
{
|
selecteTextColor = value;
|
if (!IsSelected)
|
{
|
return;
|
}
|
byte r, g, b, a;
|
r = (byte)(selecteTextColor / 256 / 256 % 256);
|
g = (byte)(selecteTextColor / 256 % 256);
|
b = (byte)(selecteTextColor % 256);
|
a = (byte)(selecteTextColor / 256 / 256 / 256 % 256);
|
|
iosButton.SetTitleColor(UIKit.UIColor.FromRGBA(r, g, b, a), UIControlState.Normal);
|
}
|
}
|
|
bool isSelected;
|
|
/// <summary>
|
/// 选中状态
|
/// </summary>
|
/// <value><c>true</c> if this instance is selected; otherwise, <c>false</c>.</value>
|
public bool IsSelected
|
{
|
get
|
{
|
return isSelected;
|
}
|
set
|
{
|
try
|
{
|
isSelected = value;
|
if (!IsCanRefresh)
|
{
|
return;
|
}
|
if (isSelected)
|
{
|
if (null == SelectedImagePath)
|
{
|
byte r, g, b, a;
|
r = (byte)(SelectedBackgroundColor / 256 / 256 % 256);
|
g = (byte)(SelectedBackgroundColor / 256 % 256);
|
b = (byte)(SelectedBackgroundColor % 256);
|
a = (byte)(SelectedBackgroundColor / 256 / 256 / 256 % 256);
|
iosButton.BackgroundColor = UIKit.UIColor.FromRGBA(r, g, b, a);
|
}
|
else
|
{
|
iosButton.SetBackgroundImage(UIImage.FromFile(FileUtils.GetImageFilePath(SelectedImagePath)), UIControlState.Normal);
|
}
|
SelectedTextColor = selecteTextColor;
|
}
|
else
|
{
|
if (imageBytes != null)
|
{
|
iosButton.SetBackgroundImage(UIImage.LoadFromData(NSData.FromArray(imageBytes)), UIControlState.Normal);
|
return;
|
}
|
if (null == UnSelectedImagePath)
|
{
|
byte r, g, b, a;
|
r = (byte)(BackgroundColor / 256 / 256 % 256);
|
g = (byte)(BackgroundColor / 256 % 256);
|
b = (byte)(BackgroundColor % 256);
|
a = (byte)(BackgroundColor / 256 / 256 / 256 % 256);
|
iosButton.BackgroundColor = UIKit.UIColor.FromRGBA(r, g, b, a);
|
}
|
else
|
{
|
iosButton.SetBackgroundImage(UIImage.LoadFromData(NSData.FromFile(FileUtils.GetImageFilePath(UnSelectedImagePath))), UIControlState.Normal);
|
}
|
TextColor = textColor;
|
}
|
}
|
catch
|
{
|
}
|
}
|
}
|
|
|
TextAlignment textAlignment = TextAlignment.Center;
|
/// <summary>
|
/// 文字对齐方式
|
/// </summary>
|
/// <param name="horizontalAlignment">Horizontal alignment.</param>
|
/// <param name="verticalAlignment">Vertical alignment.</param>
|
public TextAlignment TextAlignment
|
{
|
get
|
{
|
return textAlignment;
|
}
|
set
|
{
|
textAlignment = value;
|
switch (value)
|
{
|
case TextAlignment.TopLeft:
|
iosButton.TitleLabel.TextAlignment = UITextAlignment.Left;
|
iosButton.VerticalAlignment = UIKit.UIControlContentVerticalAlignment.Top;
|
iosButton.HorizontalAlignment = UIKit.UIControlContentHorizontalAlignment.Left;
|
break;
|
case TextAlignment.TopCenter:
|
iosButton.TitleLabel.TextAlignment = UITextAlignment.Center;
|
iosButton.VerticalAlignment = UIKit.UIControlContentVerticalAlignment.Top;
|
iosButton.HorizontalAlignment = UIKit.UIControlContentHorizontalAlignment.Center;
|
break;
|
case TextAlignment.TopRight:
|
iosButton.TitleLabel.TextAlignment = UITextAlignment.Right;
|
iosButton.VerticalAlignment = UIKit.UIControlContentVerticalAlignment.Top;
|
iosButton.HorizontalAlignment = UIKit.UIControlContentHorizontalAlignment.Right;
|
break;
|
case TextAlignment.CenterLeft:
|
iosButton.TitleLabel.TextAlignment = UITextAlignment.Left;
|
iosButton.VerticalAlignment = UIKit.UIControlContentVerticalAlignment.Center;
|
iosButton.HorizontalAlignment = UIKit.UIControlContentHorizontalAlignment.Left;
|
break;
|
case TextAlignment.Center:
|
iosButton.TitleLabel.TextAlignment = UITextAlignment.Center;
|
iosButton.VerticalAlignment = UIKit.UIControlContentVerticalAlignment.Center;
|
iosButton.HorizontalAlignment = UIKit.UIControlContentHorizontalAlignment.Center;
|
break;
|
case TextAlignment.CenterRight:
|
iosButton.TitleLabel.TextAlignment = UITextAlignment.Right;
|
iosButton.VerticalAlignment = UIKit.UIControlContentVerticalAlignment.Center;
|
iosButton.HorizontalAlignment = UIKit.UIControlContentHorizontalAlignment.Right;
|
break;
|
case TextAlignment.BottomLeft:
|
iosButton.TitleLabel.TextAlignment = UITextAlignment.Left;
|
iosButton.VerticalAlignment = UIKit.UIControlContentVerticalAlignment.Bottom;
|
iosButton.HorizontalAlignment = UIKit.UIControlContentHorizontalAlignment.Left;
|
break;
|
case TextAlignment.BottomCenter:
|
iosButton.TitleLabel.TextAlignment = UITextAlignment.Center;
|
iosButton.VerticalAlignment = UIKit.UIControlContentVerticalAlignment.Bottom;
|
iosButton.HorizontalAlignment = UIKit.UIControlContentHorizontalAlignment.Center;
|
break;
|
case TextAlignment.BottomRight:
|
iosButton.TitleLabel.TextAlignment = UITextAlignment.Right;
|
iosButton.VerticalAlignment = UIKit.UIControlContentVerticalAlignment.Bottom;
|
iosButton.HorizontalAlignment = UIKit.UIControlContentHorizontalAlignment.Right;
|
break;
|
}
|
}
|
}
|
|
|
|
string selectedImagePath;
|
/// <summary>
|
/// 选择时背景图路径
|
/// </summary>
|
/// <value>The selected image path.</value>
|
public string SelectedImagePath
|
{
|
get
|
{
|
return selectedImagePath;
|
}
|
set
|
{
|
selectedImagePath = value;
|
IsSelected = IsSelected;
|
}
|
}
|
|
string unSelectedImagePath;
|
/// <summary>
|
/// 非选中状态的背景图路径
|
/// </summary>
|
/// <value>The un selected image path.</value>
|
public string UnSelectedImagePath
|
{
|
get
|
{
|
return unSelectedImagePath;
|
}
|
set
|
{
|
unSelectedImagePath = value;
|
IsSelected = IsSelected;
|
}
|
}
|
|
/// <summary>
|
/// 内边距
|
/// </summary>
|
/// <value>The padding.</value>
|
public override Padding Padding
|
{
|
get
|
{
|
return new Padding((int)iosButton.ContentEdgeInsets.Top, (int)iosButton.ContentEdgeInsets.Left, (int)iosButton.ContentEdgeInsets.Bottom, (int)iosButton.ContentEdgeInsets.Right);
|
}
|
set
|
{
|
iosButton.ContentEdgeInsets = new UIEdgeInsets(value.Top, value.Left, value.Bottom, value.Right);
|
}
|
}
|
|
/// <summary>
|
/// 是否使用点击
|
/// </summary>
|
/// <value><c>true</c> if enable; otherwise, <c>false</c>.</value>
|
public override bool Enable
|
{
|
get
|
{
|
return iosButton.Enabled;
|
}
|
set
|
{
|
iosButton.Enabled = value;
|
}
|
}
|
|
/// <summary>
|
/// 选择时背景颜色
|
/// </summary>
|
/// <value>The color of the text.</value>
|
public uint SelectedBackgroundColor
|
{
|
get;
|
set;
|
}
|
|
class MyButton : UIKit.UIButton
|
{
|
[Weak] View view;
|
public MyButton(View view)
|
{
|
this.view = view;
|
ContentEdgeInsets = new UIEdgeInsets(3, 3, 3, 3);
|
TitleLabel.LineBreakMode = UILineBreakMode.TailTruncation;
|
}
|
|
/// <summary>
|
/// 点击开始
|
/// </summary>
|
/// <param name="touches">Touches.</param>
|
/// <param name="evt">Evt.</param>
|
public override void TouchesBegan(NSSet touches, UIEvent evt)
|
{
|
view?.TouchEvent(EventActions.Down,(touches.AnyObject as UITouch).LocationInView(this));
|
}
|
/// <summary>
|
/// 移动
|
/// </summary>
|
/// <param name="touches">Touches.</param>
|
/// <param name="evt">Evt.</param>
|
public override void TouchesMoved(NSSet touches, UIEvent evt)
|
{
|
view?.TouchEvent(EventActions.Move,(touches.AnyObject as UITouch).LocationInView(this));
|
}
|
|
/// <summary>
|
/// 点击弹起
|
/// </summary>
|
/// <param name="touches">Touches.</param>
|
/// <param name="evt">Evt.</param>
|
public override void TouchesEnded(NSSet touches, UIEvent evt)
|
{
|
view?.TouchEvent(EventActions.Up, (touches.AnyObject as UITouch).LocationInView(this));
|
}
|
|
public override void TouchesCancelled(NSSet touches, UIEvent evt)
|
{
|
view?.TouchEvent(EventActions.Cancel, (touches.AnyObject as UITouch).LocationInView(this));
|
}
|
|
public override UILineBreakMode LineBreakMode { get => base.LineBreakMode; set => base.LineBreakMode = value; }
|
|
public override UILabel TitleLabel => base.TitleLabel;
|
|
//int isMoreLines
|
|
}
|
}
|
}
|