Skip to content

Commit

Permalink
Code refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
kubaflo committed Feb 14, 2024
1 parent 0b1c40a commit bcf199e
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/Controls/src/Core/Button/Button.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static void MapText(IButtonHandler handler, Button button)
handler.PlatformView?.UpdateText(button);

// Update all of the attributed text formatting properties
ButtonHandler.UpdateAttributedTitle(handler, button);
handler.PlatformView?.UpdateAttributedTitle(handler, button);
}
}
}
32 changes: 5 additions & 27 deletions src/Core/src/Handlers/Button/ButtonHandler.iOS.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Threading.Tasks;
using Foundation;
using Microsoft.Maui.Graphics;
using UIKit;

Expand Down Expand Up @@ -91,7 +90,7 @@ public static void MapText(IButtonHandler handler, IText button)
handler.PlatformView?.UpdateText(button);

// Any text update requires that we update any attributed string formatting
UpdateAttributedTitle(handler, button);
handler.PlatformView?.UpdateAttributedTitle(handler, button);
}

public static void MapTextColor(IButtonHandler handler, ITextStyle button)
Expand All @@ -110,14 +109,14 @@ public static void MapTextColor(IButtonHandler handler, ITextStyle button)
handler.PlatformView?.UpdateTextColor(button);
}

UpdateAttributedTitle(handler, button!);
handler.PlatformView?.UpdateAttributedTitle(handler, button!);
}

public static void MapCharacterSpacing(IButtonHandler handler, ITextStyle button)
{
handler.PlatformView?.UpdateCharacterSpacing(button);

UpdateAttributedTitle(handler, button);
handler.PlatformView?.UpdateAttributedTitle(handler, button);
}

public static void MapPadding(IButtonHandler handler, IButton button)
Expand All @@ -131,12 +130,12 @@ public static void MapFont(IButtonHandler handler, ITextStyle button)

handler.PlatformView?.UpdateFont(button, fontManager);

UpdateAttributedTitle(handler, button);
handler.PlatformView?.UpdateAttributedTitle(handler, button);
}

public static void MapFormatting(IButtonHandler handler, IText button)
{
UpdateAttributedTitle(handler, button);
handler.PlatformView?.UpdateAttributedTitle(handler, button);
}

public static void MapImageSource(IButtonHandler handler, IImage image) =>
Expand All @@ -157,27 +156,6 @@ static void SetControlPropertiesFromProxy(UIButton platformView)
}
}

internal static void UpdateAttributedTitle(IButtonHandler handler, ITextStyle textStyle)
{
// Any text update requires that we update any attributed string formatting
var fontManager = handler.GetRequiredService<IFontManager>();
var platformButton = handler.PlatformView;
var uiFontAttribute = fontManager.GetFont(textStyle.Font, UIFont.ButtonFontSize);
var attributedString = new NSMutableAttributedString(new NSAttributedString(platformButton.CurrentTitle!));

// Update font family & size
attributedString.AddAttribute(UIStringAttributeKey.Font, uiFontAttribute, new NSRange(0, attributedString.Length));

//Update UpdateCharacterSpacing
attributedString = attributedString.WithCharacterSpacing(textStyle.CharacterSpacing);

//Update Text Color
if (textStyle.TextColor != null)
attributedString = attributedString?.WithTextColor(textStyle.TextColor);

platformButton.SetAttributedTitle(attributedString, UIControlState.Normal);
}

class ButtonEventProxy
{
WeakReference<IButton>? _virtualView;
Expand Down
21 changes: 21 additions & 0 deletions src/Core/src/Platform/iOS/ButtonExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Foundation;
using UIKit;

namespace Microsoft.Maui.Platform
Expand Down Expand Up @@ -83,5 +84,25 @@ public static void UpdatePadding(this UIButton platformButton, Thickness padding
#pragma warning restore CA1422 // Validate platform compatibility
#pragma warning restore CA1416
}

public static void UpdateAttributedTitle(this UIButton platformButton, IButtonHandler handler, ITextStyle textStyle)
{
// Any text update requires that we update any attributed string formatting
var fontManager = handler.GetRequiredService<IFontManager>();
var uiFontAttribute = fontManager.GetFont(textStyle.Font, UIFont.ButtonFontSize);
var attributedString = new NSMutableAttributedString(new NSAttributedString(platformButton.CurrentTitle!));

// Update font family & size
attributedString.AddAttribute(UIStringAttributeKey.Font, uiFontAttribute, new NSRange(0, attributedString.Length));

//Update UpdateCharacterSpacing
attributedString = attributedString.WithCharacterSpacing(textStyle.CharacterSpacing);

//Update Text Color
if (textStyle.TextColor != null)
attributedString = attributedString?.WithTextColor(textStyle.TextColor);

platformButton.SetAttributedTitle(attributedString, UIControlState.Normal);
}
}
}
1 change: 1 addition & 0 deletions src/Core/src/PublicAPI/net-ios/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ static Microsoft.Maui.Layouts.FlexBasis.operator !=(Microsoft.Maui.Layouts.FlexB
static Microsoft.Maui.Layouts.FlexBasis.operator ==(Microsoft.Maui.Layouts.FlexBasis left, Microsoft.Maui.Layouts.FlexBasis right) -> bool
static Microsoft.Maui.Layouts.LayoutExtensions.ArrangeContentUnbounded(this Microsoft.Maui.IContentView! contentView, Microsoft.Maui.Graphics.Rect bounds) -> Microsoft.Maui.Graphics.Size
static Microsoft.Maui.Handlers.SearchBarHandler.MapKeyboard(Microsoft.Maui.Handlers.ISearchBarHandler! handler, Microsoft.Maui.ISearchBar! searchBar) -> void
static Microsoft.Maui.Platform.ButtonExtensions.UpdateAttributedTitle(this UIKit.UIButton! platformButton, Microsoft.Maui.Handlers.IButtonHandler! handler, Microsoft.Maui.ITextStyle! textStyle) -> void
static Microsoft.Maui.Platform.KeyboardAutoManagerScroll.Connect() -> void
static Microsoft.Maui.Platform.KeyboardAutoManagerScroll.Disconnect() -> void
static Microsoft.Maui.Platform.SearchBarExtensions.UpdateIsSpellCheckEnabled(this UIKit.UISearchBar! uiSearchBar, Microsoft.Maui.ISearchBar! searchBar, UIKit.UITextField? textField = null) -> void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ static Microsoft.Maui.Hosting.MauiHandlersCollectionExtensions.AddHandler<TType>
static Microsoft.Maui.Hosting.MauiHandlersCollectionExtensions.TryAddHandler<TType>(this Microsoft.Maui.Hosting.IMauiHandlersCollection! handlersCollection, System.Func<System.IServiceProvider!, Microsoft.Maui.IElementHandler!>! handlerImplementationFactory) -> Microsoft.Maui.Hosting.IMauiHandlersCollection!
static Microsoft.Maui.Layouts.FlexBasis.operator !=(Microsoft.Maui.Layouts.FlexBasis left, Microsoft.Maui.Layouts.FlexBasis right) -> bool
static Microsoft.Maui.Layouts.FlexBasis.operator ==(Microsoft.Maui.Layouts.FlexBasis left, Microsoft.Maui.Layouts.FlexBasis right) -> bool
static Microsoft.Maui.Platform.ButtonExtensions.UpdateAttributedTitle(this UIKit.UIButton! platformButton, Microsoft.Maui.Handlers.IButtonHandler! handler, Microsoft.Maui.ITextStyle! textStyle) -> void
static Microsoft.Maui.Layouts.LayoutExtensions.ArrangeContentUnbounded(this Microsoft.Maui.IContentView! contentView, Microsoft.Maui.Graphics.Rect bounds) -> Microsoft.Maui.Graphics.Size
static Microsoft.Maui.Handlers.SearchBarHandler.MapKeyboard(Microsoft.Maui.Handlers.ISearchBarHandler! handler, Microsoft.Maui.ISearchBar! searchBar) -> void
static Microsoft.Maui.Platform.SearchBarExtensions.UpdateIsSpellCheckEnabled(this UIKit.UISearchBar! uiSearchBar, Microsoft.Maui.ISearchBar! searchBar, UIKit.UITextField? textField = null) -> void
Expand Down

0 comments on commit bcf199e

Please sign in to comment.