Skip to content

Commit e5aec15

Browse files
committed
Add the iOS15 plus work
1 parent 70e730d commit e5aec15

File tree

3 files changed

+82
-61
lines changed

3 files changed

+82
-61
lines changed

src/Controls/src/Core/Platform/iOS/Extensions/ButtonExtensions.cs

Lines changed: 59 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -182,19 +182,13 @@ public static void UpdateContentLayout(this UIButton platformButton, Button butt
182182

183183
platformButton.UpdatePadding(button);
184184

185-
// #pragma warning disable CA1416, CA1422 // TODO: [UnsupportedOSPlatform("ios15.0")]
186-
// if (platformButton.ImageEdgeInsets != imageInsets ||
187-
// platformButton.TitleEdgeInsets != titleInsets)
188-
// {
189-
// platformButton.ImageEdgeInsets = imageInsets;
190-
// platformButton.TitleEdgeInsets = titleInsets;
191-
// platformButton.Superview?.SetNeedsLayout();
192-
// }
193-
// #pragma warning restore CA1416, CA1422
194185

186+
// On iOS 15+, we will resize the image and then use the UIButton.Configuration to adjust the padding
195187
if (OperatingSystem.IsIOSVersionAtLeast(15) && platformButton.Configuration is not null)
196188
{
197-
var config = platformButton.Configuration;
189+
ResizeImage(platformButton, button, image);
190+
var config = platformButton.Configuration;
191+
198192
config.ImagePadding = imageInsets.Right - imageInsets.Left + titleInsets.Left - titleInsets.Right;
199193
platformButton.Configuration = config;
200194
}
@@ -211,6 +205,61 @@ public static void UpdateContentLayout(this UIButton platformButton, Button butt
211205
}
212206
}
213207

208+
static void ResizeImage(UIButton platformButton, Button button, UIImage image)
209+
{
210+
nfloat buttonSize = 0;
211+
212+
if (!OperatingSystem.IsIOSVersionAtLeast(15))
213+
{
214+
var contentEdgeInsets = platformButton.ContentEdgeInsets;
215+
if (platformButton.Bounds != CGRect.Empty)
216+
{
217+
if (platformButton.Bounds.Height > platformButton.Bounds.Width)
218+
buttonSize = platformButton.Bounds.Width - (contentEdgeInsets.Left + contentEdgeInsets.Right);
219+
else
220+
buttonSize = platformButton.Bounds.Height - (contentEdgeInsets.Top + contentEdgeInsets.Bottom);
221+
}
222+
}
223+
else
224+
{
225+
if (platformButton.Bounds != CGRect.Empty && platformButton.Configuration?.ContentInsets is NSDirectionalEdgeInsets contentInsets)
226+
{
227+
if (platformButton.Bounds.Height > platformButton.Bounds.Width)
228+
buttonSize = platformButton.Bounds.Width - (contentInsets.Leading + contentInsets.Trailing);
229+
else
230+
buttonSize = platformButton.Bounds.Height - (contentInsets.Top + contentInsets.Bottom);
231+
}
232+
}
233+
234+
try
235+
{
236+
if (buttonSize != 0)
237+
image = ResizeImageSource(image, buttonSize, buttonSize);
238+
239+
image = image?.ImageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal);
240+
241+
platformButton.SetImage(image, UIControlState.Normal);
242+
}
243+
catch (Exception)
244+
{
245+
// Handler.MauiContext?.CreateLogger<ButtonHandler>()?.LogWarning("Can not load Button ImageSource");
246+
}
247+
}
248+
249+
static UIImage ResizeImageSource(UIImage sourceImage, nfloat maxWidth, nfloat maxHeight)
250+
{
251+
if (sourceImage is null || sourceImage.CGImage is null)
252+
return null;
253+
254+
var sourceSize = sourceImage.Size;
255+
float maxResizeFactor = (float)Math.Min(maxWidth / sourceSize.Width, maxHeight / sourceSize.Height);
256+
257+
if (maxResizeFactor > 1)
258+
return sourceImage;
259+
260+
return UIImage.FromImage(sourceImage.CGImage, sourceImage.CurrentScale / maxResizeFactor, sourceImage.Orientation);
261+
}
262+
214263
public static void UpdateText(this UIButton platformButton, Button button)
215264
{
216265
var text = TextTransformUtilites.GetTransformedText(button.Text, button.TextTransform);

src/Core/src/Handlers/Button/ButtonHandler.iOS.cs

Lines changed: 10 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Threading.Tasks;
33
using CoreGraphics;
4+
using Foundation;
45
using Microsoft.Extensions.Logging;
56
using Microsoft.Maui.Graphics;
67
using UIKit;
@@ -28,10 +29,16 @@ protected override UIButton CreatePlatformView()
2829
// It is important to note that the configuration will change any set style changes so we will do this right after creating the button.
2930
if (OperatingSystem.IsIOSVersionAtLeast(15))
3031
{
31-
button.Configuration = UIButtonConfiguration.PlainButtonConfiguration;
32+
var config = UIButtonConfiguration.PlainButtonConfiguration;
33+
config.TitleTextAttributesTransformer = (incoming) =>
34+
{
35+
var outgoing = incoming;
36+
outgoing[UIStringAttributeKey.Font] = buttonFont;
37+
return outgoing;
38+
};
39+
button.Configuration = config;
3240
}
3341

34-
button.TitleLabel.Font = buttonFont;
3542
SetControlPropertiesFromProxy(button);
3643
return button;
3744
}
@@ -217,55 +224,7 @@ public override void SetImageSource(UIImage? platformImage)
217224

218225
nfloat buttonSize = 0;
219226

220-
if (!OperatingSystem.IsIOSVersionAtLeast(15))
221-
{
222-
var contentEdgeInsets = button.ContentEdgeInsets;
223-
if (button.Bounds != CGRect.Empty)
224-
{
225-
if (button.Bounds.Height > button.Bounds.Width)
226-
buttonSize = button.Bounds.Width - (contentEdgeInsets.Left + contentEdgeInsets.Right);
227-
else
228-
buttonSize = button.Bounds.Height - (contentEdgeInsets.Top + contentEdgeInsets.Bottom);
229-
}
230-
}
231-
else
232-
{
233-
if (button.Bounds != CGRect.Empty && button.Configuration?.ContentInsets is NSDirectionalEdgeInsets contentInsets)
234-
{
235-
if (button.Bounds.Height > button.Bounds.Width)
236-
buttonSize = button.Bounds.Width - (contentInsets.Leading + contentInsets.Trailing);
237-
else
238-
buttonSize = button.Bounds.Height - (contentInsets.Top + contentInsets.Bottom);
239-
}
240-
}
241-
242-
try
243-
{
244-
if (buttonSize != 0)
245-
platformImage = ResizeImageSource(platformImage, buttonSize, buttonSize);
246-
247-
platformImage = platformImage?.ImageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal);
248-
249-
button.SetImage(platformImage, UIControlState.Normal);
250-
}
251-
catch (Exception)
252-
{
253-
Handler.MauiContext?.CreateLogger<ButtonHandler>()?.LogWarning("Can not load Button ImageSource");
254-
}
255-
}
256-
257-
static UIImage? ResizeImageSource(UIImage? sourceImage, nfloat maxWidth, nfloat maxHeight)
258-
{
259-
if (sourceImage is null || sourceImage.CGImage is null)
260-
return null;
261-
262-
var sourceSize = sourceImage.Size;
263-
float maxResizeFactor = (float)Math.Min(maxWidth / sourceSize.Width, maxHeight / sourceSize.Height);
264-
265-
if (maxResizeFactor > 1)
266-
return sourceImage;
267-
268-
return UIImage.FromImage(sourceImage.CGImage, sourceImage.CurrentScale / maxResizeFactor, sourceImage.Orientation);
227+
button.SetImage(platformImage, UIControlState.Normal);
269228
}
270229
}
271230
}

src/Core/src/Platform/iOS/ButtonExtensions.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,19 @@ public static void UpdateCharacterSpacing(this UIButton platformButton, ITextSty
5454
public static void UpdateFont(this UIButton platformButton, ITextStyle textStyle, IFontManager fontManager)
5555
{
5656
platformButton.TitleLabel.UpdateFont(textStyle, fontManager, UIFont.ButtonFontSize);
57+
58+
// If iOS 15+, update the configuration with the new font
59+
if (OperatingSystem.IsIOSVersionAtLeast(15))
60+
{
61+
var config = UIButtonConfiguration.PlainButtonConfiguration;
62+
config.TitleTextAttributesTransformer = (incoming) =>
63+
{
64+
var outgoing = incoming;
65+
outgoing[UIStringAttributeKey.Font] = platformButton.TitleLabel.Font;
66+
return outgoing;
67+
};
68+
platformButton.Configuration = config;
69+
}
5770
}
5871

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

0 commit comments

Comments
 (0)