From a0e0b0e1d5e90a816d30f67ff7667917268ddba1 Mon Sep 17 00:00:00 2001 From: tj-devel709 Date: Thu, 21 Mar 2024 17:27:48 -0500 Subject: [PATCH] Add the resizing code for iOS 15 plus --- .../iOS/Extensions/ButtonExtensions.cs | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/src/Controls/src/Core/Platform/iOS/Extensions/ButtonExtensions.cs b/src/Controls/src/Core/Platform/iOS/Extensions/ButtonExtensions.cs index 8f3a667eb022..82fc64569d47 100644 --- a/src/Controls/src/Core/Platform/iOS/Extensions/ButtonExtensions.cs +++ b/src/Controls/src/Core/Platform/iOS/Extensions/ButtonExtensions.cs @@ -2,6 +2,7 @@ using System; using CoreGraphics; using Foundation; +using Microsoft.Extensions.Logging; using Microsoft.Maui.Controls.Internals; using UIKit; using static Microsoft.Maui.Controls.Button; @@ -229,6 +230,11 @@ public static void UpdateContentLayout(this UIButton platformButton, Button butt else platformButton.TitleLabel.Layer.Hidden = true; + if (config is UIButtonConfiguration) + { + ResizeImageIfNecessary(platformButton, button, image); + } + if (config is UIButtonConfiguration) { // If there is an image above or below the Title, the button will need to be redrawn the first time. @@ -258,6 +264,71 @@ public static void UpdateContentLayout(this UIButton platformButton, Button butt } } + static void ResizeImageIfNecessary(UIButton platformButton, Button button, UIImage image) + { + nfloat availableHeight = 0; + nfloat availableWidth = 0; + + if (platformButton.Bounds != CGRect.Empty + && (button.Height != double.NaN || button.Width != double.NaN) + && platformButton.Configuration is UIButtonConfiguration config + && config.ContentInsets is NSDirectionalEdgeInsets contentInsets) + { + // Case where the image is on top or bottom of the Title text. + if (config.ImagePlacement == NSDirectionalRectEdge.Top || config.ImagePlacement == NSDirectionalRectEdge.Bottom) + { + availableHeight = platformButton.Bounds.Height - (contentInsets.Top + contentInsets.Bottom + config.ImagePadding + platformButton.GetTitleBoundingRect().Height); + availableWidth = platformButton.Bounds.Width - (contentInsets.Leading + contentInsets.Trailing); + } + + // Case where the image is on the left or right of the Title text. + else + { + availableHeight = platformButton.Bounds.Height - (contentInsets.Top + contentInsets.Bottom); + availableWidth = platformButton.Bounds.Width - (contentInsets.Leading + contentInsets.Trailing + config.ImagePadding + platformButton.GetTitleBoundingRect().Width); + } + } + + availableHeight = (nfloat)Math.Max(availableHeight, 0); + availableWidth = (nfloat)Math.Max(availableWidth, 0); + + try + { + if (image.Size.Height > availableHeight || image.Size.Width > availableWidth) + { + image = ResizeImageSource(image, availableWidth, availableHeight); + } + else + { + return; + } + + image = image?.ImageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal); + + platformButton.SetImage(image, UIControlState.Normal); + + platformButton.SetNeedsLayout(); + } + catch (Exception) + { + button.Handler.MauiContext?.CreateLogger()?.LogWarning("Can not load Button ImageSource"); + } + } + + static UIImage ResizeImageSource(UIImage sourceImage, nfloat maxWidth, nfloat maxHeight) + { + if (sourceImage is null || sourceImage.CGImage is null) + return null; + + var sourceSize = sourceImage.Size; + float maxResizeFactor = (float)Math.Min(maxWidth / sourceSize.Width, maxHeight / sourceSize.Height); + + if (maxResizeFactor > 1) + return sourceImage; + + return UIImage.FromImage(sourceImage.CGImage, sourceImage.CurrentScale / maxResizeFactor, sourceImage.Orientation); + } + public static void UpdateText(this UIButton platformButton, Button button) { var text = TextTransformUtilites.GetTransformedText(button.Text, button.TextTransform);