private void AssertButtonContentLayoutsEqual(Button.ButtonContentLayout layout1, object layout2) { var bcl = (Button.ButtonContentLayout)layout2; Assert.AreEqual(layout1.Position, bcl.Position); Assert.AreEqual(layout1.Spacing, bcl.Spacing); }
void ComputeEdgeInsets(UIButton button, Button.ButtonContentLayout layout) { if (button?.ImageView?.Image == null || string.IsNullOrEmpty(button.TitleLabel?.Text)) { return; } var position = layout.Position; var spacing = (nfloat)(layout.Spacing / 2); if (position == Button.ButtonContentLayout.ImagePosition.Left) { button.ImageEdgeInsets = new UIEdgeInsets(0, -spacing, 0, spacing); button.TitleEdgeInsets = new UIEdgeInsets(0, spacing, 0, -spacing); UpdateContentEdge(button, new UIEdgeInsets(0, 2 * spacing, 0, 2 * spacing)); return; } if (_titleChanged) { var stringToMeasure = new NSString(button.TitleLabel.Text); UIStringAttributes attribs = new UIStringAttributes { Font = button.TitleLabel.Font }; var s = stringToMeasure.GetSizeUsingAttributes(attribs);; _titleSize = new SizeF((float)s.Width, (float)s.Height); _titleChanged = false; } var labelWidth = _titleSize.Width; var imageWidth = button.ImageView.Image.Size.Width; if (position == Button.ButtonContentLayout.ImagePosition.Right) { button.ImageEdgeInsets = new UIEdgeInsets(0, labelWidth + spacing, 0, -labelWidth - spacing); button.TitleEdgeInsets = new UIEdgeInsets(0, -imageWidth - spacing, 0, imageWidth + spacing); UpdateContentEdge(button, new UIEdgeInsets(0, 2 * spacing, 0, 2 * spacing)); return; } var imageVertOffset = (_titleSize.Height / 2); var titleVertOffset = (button.ImageView.Image.Size.Height / 2); var edgeOffset = (float)Math.Min(imageVertOffset, titleVertOffset); UpdateContentEdge(button, new UIEdgeInsets(edgeOffset, 0, edgeOffset, 0)); var horizontalImageOffset = labelWidth / 2; var horizontalTitleOffset = imageWidth / 2; if (position == Button.ButtonContentLayout.ImagePosition.Bottom) { imageVertOffset = -imageVertOffset; titleVertOffset = -titleVertOffset; } button.ImageEdgeInsets = new UIEdgeInsets(-imageVertOffset, horizontalImageOffset, imageVertOffset, -horizontalImageOffset); button.TitleEdgeInsets = new UIEdgeInsets(titleVertOffset, -horizontalTitleOffset, -titleVertOffset, horizontalTitleOffset); }
static Button CreateButton(Button.ButtonContentLayout layout) { return(new Button { Text = "Click Me", ImageSource = "coffee.png", ContentLayout = layout, BackgroundColor = Color.Gray }); }
static Button CreateButton(Button.ButtonContentLayout layout) { return(new Button { Text = "Click Me On Mac", ImageSource = "bank.png", Font = Font.OfSize("Helvetica", 14), ContentLayout = layout, BackgroundColor = Color.Black, TextColor = Color.White }); }
static Button CreateButton(Button.ButtonContentLayout layout) { return(new Button { Text = "Click Me On Mac", ImageSource = "bank.png", FontFamily = "Helvetica", FontSize = 14, ContentLayout = layout, BackgroundColor = Colors.Black, TextColor = Colors.White }); }
static StackPanel CreateContentContainer(Button.ButtonContentLayout layout, WImage image, string text) { var container = new StackPanel(); var textBlock = new TextBlock { Text = text, VerticalAlignment = VerticalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Center }; var spacing = layout.Spacing; container.HorizontalAlignment = HorizontalAlignment.Center; container.VerticalAlignment = VerticalAlignment.Center; switch (layout.Position) { case Button.ButtonContentLayout.ImagePosition.Top: container.Orientation = Orientation.Vertical; image.Margin = new WThickness(0, 0, 0, spacing); container.Children.Add(image); container.Children.Add(textBlock); break; case Button.ButtonContentLayout.ImagePosition.Bottom: container.Orientation = Orientation.Vertical; image.Margin = new WThickness(0, spacing, 0, 0); container.Children.Add(textBlock); container.Children.Add(image); break; case Button.ButtonContentLayout.ImagePosition.Right: container.Orientation = Orientation.Horizontal; image.Margin = new WThickness(spacing, 0, 0, 0); container.Children.Add(textBlock); container.Children.Add(image); break; default: // Defaults to image on the left container.Orientation = Orientation.Horizontal; image.Margin = new WThickness(0, 0, spacing, 0); container.Children.Add(image); container.Children.Add(textBlock); break; } return(container); }
void UpdateBitmap() { if (Element == null || _isDisposed) { return; } FileImageSource elementImage = Button.Image; string imageFile = elementImage?.File; _imageHeight = -1; if (elementImage == null || IsNullOrEmpty(imageFile)) { SetCompoundDrawablesWithIntrinsicBounds(null, null, null, null); return; } Drawable image = Context.GetDrawable(imageFile); if (IsNullOrEmpty(Button.Text)) { // No text, so no need for relative position; just center the image // There's no option for just plain-old centering, so we'll use Top // (which handles the horizontal centering) and some tricksy padding (in OnLayout) // to handle the vertical centering // Clear any previous padding and set the image as top/center UpdateContentEdge(); SetCompoundDrawablesWithIntrinsicBounds(null, image, null, null); // Keep track of the image height so we can use it in OnLayout _imageHeight = image.IntrinsicHeight; image.Dispose(); return; } Button.ButtonContentLayout layout = Button.ContentLayout; CompoundDrawablePadding = (int)layout.Spacing; switch (layout.Position) { case Button.ButtonContentLayout.ImagePosition.Top: SetCompoundDrawablesWithIntrinsicBounds(null, image, null, null); break; case Button.ButtonContentLayout.ImagePosition.Bottom: SetCompoundDrawablesWithIntrinsicBounds(null, null, null, image); break; case Button.ButtonContentLayout.ImagePosition.Right: SetCompoundDrawablesWithIntrinsicBounds(null, null, image, null); break; default: // Defaults to image on the left SetCompoundDrawablesWithIntrinsicBounds(image, null, null, null); break; } image?.Dispose(); }
internal static bool IsHorizontal(this Button.ButtonContentLayout layout) => layout.Position == Button.ButtonContentLayout.ImagePosition.Left || layout.Position == Button.ButtonContentLayout.ImagePosition.Right;
public static Button ContentLayout(this Button button, Button.ButtonContentLayout layout) { button.ContentLayout = layout; return(button); }