Skip to content

Commit

Permalink
Add the iOS15 plus work
Browse files Browse the repository at this point in the history
  • Loading branch information
tj-devel709 committed Mar 11, 2024
1 parent 70e730d commit e5aec15
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 61 deletions.
69 changes: 59 additions & 10 deletions src/Controls/src/Core/Platform/iOS/Extensions/ButtonExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,19 +182,13 @@ public static void UpdateContentLayout(this UIButton platformButton, Button butt

platformButton.UpdatePadding(button);

// #pragma warning disable CA1416, CA1422 // TODO: [UnsupportedOSPlatform("ios15.0")]
// if (platformButton.ImageEdgeInsets != imageInsets ||
// platformButton.TitleEdgeInsets != titleInsets)
// {
// platformButton.ImageEdgeInsets = imageInsets;
// platformButton.TitleEdgeInsets = titleInsets;
// platformButton.Superview?.SetNeedsLayout();
// }
// #pragma warning restore CA1416, CA1422

// On iOS 15+, we will resize the image and then use the UIButton.Configuration to adjust the padding
if (OperatingSystem.IsIOSVersionAtLeast(15) && platformButton.Configuration is not null)
{
var config = platformButton.Configuration;
ResizeImage(platformButton, button, image);
var config = platformButton.Configuration;

config.ImagePadding = imageInsets.Right - imageInsets.Left + titleInsets.Left - titleInsets.Right;
platformButton.Configuration = config;
}
Expand All @@ -211,6 +205,61 @@ public static void UpdateContentLayout(this UIButton platformButton, Button butt
}
}

static void ResizeImage(UIButton platformButton, Button button, UIImage image)
{
nfloat buttonSize = 0;

if (!OperatingSystem.IsIOSVersionAtLeast(15))
{
var contentEdgeInsets = platformButton.ContentEdgeInsets;
if (platformButton.Bounds != CGRect.Empty)
{
if (platformButton.Bounds.Height > platformButton.Bounds.Width)
buttonSize = platformButton.Bounds.Width - (contentEdgeInsets.Left + contentEdgeInsets.Right);
else
buttonSize = platformButton.Bounds.Height - (contentEdgeInsets.Top + contentEdgeInsets.Bottom);
}
}
else
{
if (platformButton.Bounds != CGRect.Empty && platformButton.Configuration?.ContentInsets is NSDirectionalEdgeInsets contentInsets)
{
if (platformButton.Bounds.Height > platformButton.Bounds.Width)
buttonSize = platformButton.Bounds.Width - (contentInsets.Leading + contentInsets.Trailing);
else
buttonSize = platformButton.Bounds.Height - (contentInsets.Top + contentInsets.Bottom);
}
}

try
{
if (buttonSize != 0)
image = ResizeImageSource(image, buttonSize, buttonSize);

image = image?.ImageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal);

platformButton.SetImage(image, UIControlState.Normal);
}
catch (Exception)
{
// Handler.MauiContext?.CreateLogger<ButtonHandler>()?.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);
Expand Down
61 changes: 10 additions & 51 deletions src/Core/src/Handlers/Button/ButtonHandler.iOS.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Threading.Tasks;
using CoreGraphics;
using Foundation;
using Microsoft.Extensions.Logging;
using Microsoft.Maui.Graphics;
using UIKit;
Expand Down Expand Up @@ -28,10 +29,16 @@ protected override UIButton CreatePlatformView()
// It is important to note that the configuration will change any set style changes so we will do this right after creating the button.
if (OperatingSystem.IsIOSVersionAtLeast(15))
{
button.Configuration = UIButtonConfiguration.PlainButtonConfiguration;
var config = UIButtonConfiguration.PlainButtonConfiguration;
config.TitleTextAttributesTransformer = (incoming) =>
{
var outgoing = incoming;
outgoing[UIStringAttributeKey.Font] = buttonFont;
return outgoing;
};
button.Configuration = config;
}

button.TitleLabel.Font = buttonFont;
SetControlPropertiesFromProxy(button);
return button;
}
Expand Down Expand Up @@ -217,55 +224,7 @@ public override void SetImageSource(UIImage? platformImage)

nfloat buttonSize = 0;

if (!OperatingSystem.IsIOSVersionAtLeast(15))
{
var contentEdgeInsets = button.ContentEdgeInsets;
if (button.Bounds != CGRect.Empty)
{
if (button.Bounds.Height > button.Bounds.Width)
buttonSize = button.Bounds.Width - (contentEdgeInsets.Left + contentEdgeInsets.Right);
else
buttonSize = button.Bounds.Height - (contentEdgeInsets.Top + contentEdgeInsets.Bottom);
}
}
else
{
if (button.Bounds != CGRect.Empty && button.Configuration?.ContentInsets is NSDirectionalEdgeInsets contentInsets)
{
if (button.Bounds.Height > button.Bounds.Width)
buttonSize = button.Bounds.Width - (contentInsets.Leading + contentInsets.Trailing);
else
buttonSize = button.Bounds.Height - (contentInsets.Top + contentInsets.Bottom);
}
}

try
{
if (buttonSize != 0)
platformImage = ResizeImageSource(platformImage, buttonSize, buttonSize);

platformImage = platformImage?.ImageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal);

button.SetImage(platformImage, UIControlState.Normal);
}
catch (Exception)
{
Handler.MauiContext?.CreateLogger<ButtonHandler>()?.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);
button.SetImage(platformImage, UIControlState.Normal);
}
}
}
Expand Down
13 changes: 13 additions & 0 deletions src/Core/src/Platform/iOS/ButtonExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,19 @@ public static void UpdateCharacterSpacing(this UIButton platformButton, ITextSty
public static void UpdateFont(this UIButton platformButton, ITextStyle textStyle, IFontManager fontManager)
{
platformButton.TitleLabel.UpdateFont(textStyle, fontManager, UIFont.ButtonFontSize);

// If iOS 15+, update the configuration with the new font
if (OperatingSystem.IsIOSVersionAtLeast(15))
{
var config = UIButtonConfiguration.PlainButtonConfiguration;
config.TitleTextAttributesTransformer = (incoming) =>
{
var outgoing = incoming;
outgoing[UIStringAttributeKey.Font] = platformButton.TitleLabel.Font;
return outgoing;
};
platformButton.Configuration = config;
}
}

public static void UpdatePadding(this UIButton platformButton, IButton button, Thickness? defaultPadding = null) =>
Expand Down

0 comments on commit e5aec15

Please sign in to comment.