void CreateShapeDrawable(MvvmAspire.Controls.Button element) { GradientDrawable shape = new GradientDrawable(); shape.SetShape(ShapeType.Rectangle); if (element.CornerRadius != 0) { shape.SetCornerRadii(new float[] { BaseUIHelper.ConvertDPToPixels(element.Corner.Top), BaseUIHelper.ConvertDPToPixels(element.Corner.Top), BaseUIHelper.ConvertDPToPixels(element.Corner.Right), BaseUIHelper.ConvertDPToPixels(element.Corner.Right), BaseUIHelper.ConvertDPToPixels(element.Corner.Bottom), BaseUIHelper.ConvertDPToPixels(element.Corner.Bottom), BaseUIHelper.ConvertDPToPixels(element.Corner.Left), BaseUIHelper.ConvertDPToPixels(element.Corner.Left) }); } if (Element.BackgroundColor != Xamarin.Forms.Color.Default) { shape.SetColor(Element.BackgroundColor.ToAndroid()); } if (element.BorderColor != Xamarin.Forms.Color.Default) { shape.SetStroke(2, element.BorderColor.ToAndroid()); } Control.Background = shape; }
void SetBackground(MvvmAspire.Controls.Button element) { if (element.BackgroundImage != null) { // seems this is not working, LoadImageAsync returns null //var bitmap = await imageLoader.LoadImageAsync(element.BackgroundImage, Forms.Context); //button.SetBackgroundDrawable(new Android.Graphics.Drawables.BitmapDrawable(bitmap)); var resourceId = UIHelper.GetDrawableResource(element.BackgroundImage); if (resourceId > 0) { Control.SetBackgroundResource(resourceId); } else { Control.SetBackgroundResource(Android.Resource.Drawable.ButtonDefault); } } else { if (element.BackgroundColor == Xamarin.Forms.Color.Transparent || element.BackgroundColor == Xamarin.Forms.Color.Default) { Control.SetBackgroundResource(Android.Resource.Drawable.ButtonDefault); } } }
void SetTypeface(MvvmAspire.Controls.Button element) { if (!string.IsNullOrEmpty(element.FontFamily)) { Control.Typeface = FontCache.GetTypeFace(element.FontFamily); } }
void SetTextLineBreakMode(MvvmAspire.Controls.Button element) { if (element.TextLineBreakMode == LineBreakMode.NoWrap) { Control.TitleLabel.LineBreakMode = UILineBreakMode.Clip; } else if (element.TextLineBreakMode == LineBreakMode.WordWrap) { Control.TitleLabel.LineBreakMode = UILineBreakMode.WordWrap; Control.TitleLabel.Lines = 2; } else if (element.TextLineBreakMode == LineBreakMode.CharacterWrap) { Control.TitleLabel.LineBreakMode = UILineBreakMode.CharacterWrap; Control.TitleLabel.Lines = 2; } else if (element.TextLineBreakMode == LineBreakMode.HeadTruncation) { Control.TitleLabel.LineBreakMode = UILineBreakMode.HeadTruncation; } else if (element.TextLineBreakMode == LineBreakMode.TailTruncation) { Control.TitleLabel.LineBreakMode = UILineBreakMode.TailTruncation; } else { Control.TitleLabel.LineBreakMode = UILineBreakMode.MiddleTruncation; } }
void SetAccesibilitySetName(MvvmAspire.Controls.Button element) { if (!string.IsNullOrEmpty(element.AccesibilitySetName)) { Control.ContentDescription = element.AccesibilitySetName; } }
void SetPadding(MvvmAspire.Controls.Button element) { Control.SetPadding(BaseUIHelper.ConvertDPToPixels(element.Padding.Left), BaseUIHelper.ConvertDPToPixels(element.Padding.Top), BaseUIHelper.ConvertDPToPixels(element.Padding.Right), BaseUIHelper.ConvertDPToPixels(element.Padding.Bottom)); }
void SetTextPadding(MvvmAspire.Controls.Button element) { // spacing between text and image left Control.ImageEdgeInsets = new UIEdgeInsets(0, 0, 0, (nfloat)element.TextPadding.Right); Control.TitleEdgeInsets = new UIEdgeInsets( (nfloat)element.TextPadding.Top, (nfloat)element.TextPadding.Left, (nfloat)element.TextPadding.Bottom, (nfloat)element.TextPadding.Right); }
void SetHighlightedBackgroundImage(MvvmAspire.Controls.Button element) { if (element.HighlightedBackgroundImage != null && !string.IsNullOrEmpty(element.HighlightedBackgroundImage.File)) { var image = UIImage.FromBundle(element.HighlightedBackgroundImage.File); Control.SetBackgroundImage(image, UIControlState.Highlighted); } }
void SetFont(MvvmAspire.Controls.Button element) { //if (!string.IsNullOrWhiteSpace(element.FontFamily)) //Control.Font = UIFont.FromName(element.FontFamily, (nfloat)element.FontSize); if (Control.Font == null || Control.Font.PointSize != (nfloat)element.FontSize) { Control.Font = UIFont.FromName(Control.Font.FamilyName, (nfloat)element.FontSize); } }
void SetTilting(MvvmAspire.Controls.Button element) { if (element.TiltEnabled) { Control.ApplyTiltingEffect(); } else { Control.RemoveTiltingEffect(); } }
void SetLongPressEvent(MvvmAspire.Controls.Button element) { var longPress = new UILongPressGestureRecognizer(recognizer => { if (recognizer.State == UIGestureRecognizerState.Began) { element.OnLongPress(); } }); Control.AddGestureRecognizer(longPress); }
protected virtual void SetCornerRadius(MvvmAspire.Controls.Button element) { if (element.Corner != 0) { int radius = 0; UIRectCorner forCorners = 0; if (element.Corner.Bottom > 0 && element.Corner.Left > 0 && element.Corner.Right > 0 && element.Corner.Top > 0) { forCorners = UIRectCorner.AllCorners; radius = Convert.ToInt32(element.Corner.Top); } else { if (element.Corner.Bottom > 0) { forCorners = forCorners | UIRectCorner.BottomRight; radius = Convert.ToInt32(element.Corner.Bottom); } if (element.Corner.Left > 0) { forCorners = forCorners | UIRectCorner.BottomLeft; radius = Convert.ToInt32(element.Corner.Left); } if (element.Corner.Right > 0) { forCorners = forCorners | UIRectCorner.TopRight; radius = Convert.ToInt32(element.Corner.Right); } if (element.Corner.Top > 0) { forCorners = forCorners | UIRectCorner.TopLeft; radius = Convert.ToInt32(element.Corner.Top); } } Control.SetCornerRadius(radius, forCorners); Control.Layer.MasksToBounds = true; Control.ClipsToBounds = true; } else { Control.SetCornerRadius(0, UIRectCorner.AllCorners); Control.ClipsToBounds = true; } }
void SetHighlightedBackgroundImagePattern(MvvmAspire.Controls.Button element) { if (element.HighlightedBackgroundImagePattern != null && !string.IsNullOrEmpty(element.HighlightedBackgroundImagePattern.File)) { var backgroundImage = UIImage.FromBundle(element.HighlightedBackgroundImagePattern.File); if (backgroundImage == null) { throw new ArgumentException("Cannot find image '" + element.HighlightedBackgroundImagePattern.File + "'."); } backgroundImage = backgroundImage.CreateResizableImage(new UIEdgeInsets(8, 8 * 2, 8, 8 * 2)); Control.SetBackgroundImage(backgroundImage, UIControlState.Highlighted); } }
void IdentifyBackgroundColor(MvvmAspire.Controls.Button element) { if (Control == null) { return; } if (element.IsEnabled) { Control.BackgroundColor = element.BackgroundColor.ToUIColor(); } else { Control.BackgroundColor = element.DisabledTextColor.ToUIColor(); } }
void SetXAlign(MvvmAspire.Controls.Button element) { Control.HorizontalAlignment = UIHelper.ConvertToHorizontalAlignment(element.XAlign); if (element.XAlign == Xamarin.Forms.TextAlignment.Start) { Control.TitleLabel.TextAlignment = UITextAlignment.Left; } else if (element.XAlign == Xamarin.Forms.TextAlignment.Center) { Control.TitleLabel.TextAlignment = UITextAlignment.Center; } else if (element.XAlign == Xamarin.Forms.TextAlignment.End) { Control.TitleLabel.TextAlignment = UITextAlignment.Right; } }
void SetAutoShrink(MvvmAspire.Controls.Button element) { if (Control == null) { return; } if (Control.Frame.Width > 0) { if (element.RefitTextEnabled) { Control.TitleLabel.MinimumScaleFactor = (nfloat)(element.MinimumFontSize / element.FontSize); Control.LayoutIfNeeded(); Control.TitleLabel.AdjustFontSizeToFit(); element.FontSize = Control.Font.PointSize; } } }
protected virtual void SetBackgroundImage(MvvmAspire.Controls.Button element) { if (element.BackgroundImage != null && !string.IsNullOrEmpty(element.BackgroundImage.File)) { var image = UIImage.FromBundle(element.BackgroundImage.File); if (image == null) { throw new ArgumentException("Cannot find image '" + element.BackgroundImage.File + "'."); } Control.SetBackgroundImage(image, UIControlState.Normal); } else { if (element.BackgroundImagePattern == null || string.IsNullOrEmpty(element.BackgroundImagePattern.File)) { Control.SetBackgroundImage(null, UIControlState.Normal); } } }
void SetTextAlignment(MvvmAspire.Controls.Button element) { if (Control == null) { return; } GravityFlags flags = GravityFlags.NoGravity; switch (element.XAlign) { case Xamarin.Forms.TextAlignment.Center: flags = GravityFlags.CenterHorizontal; break; case Xamarin.Forms.TextAlignment.Start: flags = GravityFlags.Left; break; case Xamarin.Forms.TextAlignment.End: flags = GravityFlags.Right; break; } switch (element.YAlign) { case Xamarin.Forms.TextAlignment.Center: flags |= GravityFlags.CenterVertical; break; case Xamarin.Forms.TextAlignment.Start: flags |= GravityFlags.Top; break; case Xamarin.Forms.TextAlignment.End: flags |= GravityFlags.Bottom; break; } Control.Gravity = flags; }
void SetTitlePadding(MvvmAspire.Controls.Button element) { Control.CompoundDrawablePadding = BaseUIHelper.ConvertDPToPixels(element.TextPadding.Left); }
void SetPadding(MvvmAspire.Controls.Button element) { Control.ContentEdgeInsets = new UIEdgeInsets( (nfloat)element.Padding.Top, (nfloat)element.Padding.Left, (nfloat)element.Padding.Bottom, (nfloat)element.Padding.Right); }
void SetImages(MvvmAspire.Controls.Button element) { try { ButtonText = Element.Text; if (!HaseResource(element.ImageLeft) && !HaseResource(element.ImageTop) && !HaseResource(element.ImageRight) && !HaseResource(element.ImageBottom)) { Control.SetCompoundDrawablesWithIntrinsicBounds(null, null, null, null); Control.Invalidate(); return; } Drawable leftResource = null, topResource = null, rightResource = null, bottomResource = null; if (element.ImageLeft != null && element.ImageLeft.File != null) { leftResource = ContextCompat.GetDrawable(UIHelper.Context, UIHelper.GetDrawableResource(element.ImageLeft)); } if (element.ImageTop != null && element.ImageTop.File != null) { topResource = ContextCompat.GetDrawable(UIHelper.Context, UIHelper.GetDrawableResource(element.ImageTop)); } if (element.ImageRight != null && element.ImageRight.File != null) { rightResource = ContextCompat.GetDrawable(UIHelper.Context, UIHelper.GetDrawableResource(element.ImageRight)); } if (element.ImageBottom != null && element.ImageBottom.File != null) { bottomResource = ContextCompat.GetDrawable(UIHelper.Context, UIHelper.GetDrawableResource(element.ImageBottom)); } bool hasResource = leftResource != null || rightResource != null || topResource != null || bottomResource != null; if (hasResource && !element.CenterImage) { Control.SetCompoundDrawablesWithIntrinsicBounds(leftResource, topResource, rightResource, bottomResource); } if (hasResource && element.CenterImage) { Xamarin.Forms.Size size = new Xamarin.Forms.Size(); if (leftResource != null) { if (element.ImageLeftHeight > 0 && element.ImageLeftWidth > 0) { size.Width = element.ImageLeftWidth; size.Height = element.ImageLeftHeight; } else { var maxWidth = BaseUIHelper.ConvertDPToPixels(element.Padding.Left) - BaseUIHelper.ConvertDPToPixels(element.Padding.Right) - Control.MeasuredWidth; var maxHeight = BaseUIHelper.ConvertDPToPixels(element.Padding.Top) - BaseUIHelper.ConvertDPToPixels(element.Padding.Bottom) - Control.MeasuredHeight; var temp = GetSize(leftResource.IntrinsicWidth, leftResource.IntrinsicHeight, Math.Abs(maxWidth), Math.Abs(maxHeight)); size.Width = temp.Width; size.Height = temp.Height; } var scaledDrawable = new ScaleDrawable(leftResource, 0, (int)size.Width, (int)size.Height).Drawable; scaledDrawable.SetBounds(0, 0, (int)size.Width, (int)size.Height); Control.SetCompoundDrawables(scaledDrawable, null, null, null); } if (rightResource != null) { if (element.ImageRightHeight > 0 && element.ImageRightWidth > 0) { size.Width = element.ImageRightWidth; size.Height = element.ImageRightHeight; } else { var maxWidth = BaseUIHelper.ConvertDPToPixels(element.Padding.Left) - BaseUIHelper.ConvertDPToPixels(element.Padding.Right) - Control.MeasuredWidth; var maxHeight = BaseUIHelper.ConvertDPToPixels(element.Padding.Top) - BaseUIHelper.ConvertDPToPixels(element.Padding.Bottom) - Control.MeasuredHeight; var temp = GetSize(rightResource.IntrinsicWidth, rightResource.IntrinsicHeight, Math.Abs(maxWidth), Math.Abs(maxHeight)); size.Width = temp.Width; size.Height = temp.Height; } var scaledDrawable = new ScaleDrawable(rightResource, 0, (int)size.Width, (int)size.Height).Drawable; scaledDrawable.SetBounds(0, 0, (int)size.Width, (int)size.Height); Control.SetCompoundDrawables(null, null, scaledDrawable, null); } if (!element.CenterImage) { SetImagePadding((int)size.Width); } Control.Invalidate(); } } catch (Exception e) { } }
void SetTextColor(MvvmAspire.Controls.Button element) { Control.SetTitleColor(element.TextColor.ToUIColor(), UIControlState.Normal); Control.SetTitleColor(element.DisabledTextColor.ToUIColor(), UIControlState.Disabled); }
void SetYAlign(MvvmAspire.Controls.Button element) { Control.VerticalAlignment = UIHelper.ConvertToVerticalAlignment(element.YAlign); }
/// <summary> /// Sets the button image. /// In iOS, only one image can display per button. Image prioritization follows: Left, Right, Top, Bottom. /// </summary> /// <param name="element"></param> void SetImages(MvvmAspire.Controls.Button element) { if (Control == null) { return; } string imageSource = null; if (element.ImageLeft != null && !string.IsNullOrEmpty(element.ImageLeft.File)) { imageSource = element.ImageLeft.File; } else if (element.ImageRight != null && !string.IsNullOrEmpty(element.ImageRight.File)) { imageSource = element.ImageRight.File; } else if (element.ImageTop != null && !string.IsNullOrEmpty(element.ImageTop.File)) { imageSource = element.ImageTop.File; } else if (element.ImageBottom != null && !string.IsNullOrEmpty(element.ImageBottom.File)) { imageSource = element.ImageBottom.File; } else if (element.Image != null) { imageSource = element.Image.File; } UIImage image = imageSource != null?UIImage.FromBundle(imageSource) : null; if (image != null) { Control.AdjustsImageWhenHighlighted = false; if (element.ImageRightWidth > 0 && element.ImageRightHeight > 0) { UIGraphics.BeginImageContextWithOptions(new CGSize(element.ImageRightWidth, element.ImageRightHeight), false, 0); image.Draw(new CGRect(0, 0, element.ImageRightWidth, element.ImageRightHeight)); var resizedImage = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); Control.SetImage(resizedImage, UIControlState.Normal); } else if (element.ImageLeftWidth > 0 && element.ImageLeftHeight > 0) { UIGraphics.BeginImageContextWithOptions(new CGSize(element.ImageLeftWidth, element.ImageLeftHeight), false, 0); image.Draw(new CGRect(0, 0, element.ImageLeftWidth, element.ImageLeftHeight)); var resizedImage = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); Control.SetImage(resizedImage, UIControlState.Normal); } else { var maxWidth = element.Padding.Left - element.Padding.Right - Control.Frame.Size.Width; var maxHeight = element.Padding.Top - element.Padding.Bottom - Control.Frame.Size.Height; var imageSize = GetSize(image.Size.Width, image.Size.Height, (nfloat)Math.Abs(maxWidth), (nfloat)Math.Abs(maxHeight)); //new CGSize(image.Size.Width * PointSize.Multiplier, image.Size.Height * PointSize.Multiplier); UIGraphics.BeginImageContextWithOptions(imageSize, false, 0); image.Draw(new CGRect(0, 0, imageSize.Width, imageSize.Height)); var resizedImage = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); Control.SetImage(resizedImage, UIControlState.Normal); //if (Control.ImageView != null) //{ // Control.ImageView.Frame = new CGRect(0, 0, imageSize.Width, imageSize.Height); //} // Not yet sure if this is acceptable to other apps. // Please make this configurable if needs to disable this. Control.ImageView.ContentMode = UIViewContentMode.ScaleAspectFit; } } else { Control.SetImage(image, UIControlState.Normal); Control.ContentEdgeInsets = new UIEdgeInsets(0, 0, 0, 0); } if (element.ImageRight != null && element.ImageRight.File != null) { nfloat topPadding = (nfloat)element.Padding.Top; nfloat bottomPadding = (nfloat)element.Padding.Bottom; if (!String.IsNullOrWhiteSpace(element.Text)) { Control.TitleEdgeInsets = new UIEdgeInsets(topPadding, -Control.ImageView.Frame.Size.Width - 10, bottomPadding, Control.ImageView.Frame.Size.Width); Control.ImageEdgeInsets = new UIEdgeInsets(topPadding, Control.TitleLabel.Frame.Size.Width, bottomPadding, -Control.TitleLabel.Frame.Size.Width); } else { Control.ImageEdgeInsets = new UIEdgeInsets(topPadding, 0, bottomPadding, 0); } } if (element.Image != null && element.Image.File != null) { Control.ImageEdgeInsets = new UIEdgeInsets(5, 5, 5, 5); } else if (element.ImageLeft != null && element.ImageLeft.File != null) { Control.ImageEdgeInsets = new UIEdgeInsets(0, 0, 0, 20); } }