Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
Fix Entry issue using TextColor and ClearButton in iOS < 13 (#14566)
Browse files Browse the repository at this point in the history
Co-authored-by: Gerald Versluis <gerald.versluis@microsoft.com>
  • Loading branch information
jsuarezruiz and jfversluis authored Sep 6, 2021
1 parent 3470b92 commit 30d837a
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8" ?>
<local:TestContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="Test 14523" xmlns:local="using:Xamarin.Forms.Controls"
x:Class="Xamarin.Forms.Controls.Issues.Issue14523">
<StackLayout>
<Label
Padding="12"
BackgroundColor="Black"
TextColor="White"
Text="Without exceptions, the test has passed."/>
<Entry
ClearButtonVisibility="WhileEditing"
TextColor="Red" />
</StackLayout>
</local:TestContentPage>
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Input;
using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;

#if UITEST
using Xamarin.UITest;
using NUnit.Framework;
using Xamarin.Forms.Core.UITests;
#endif

namespace Xamarin.Forms.Controls.Issues
{
#if UITEST
[Category(UITestCategories.CarouselView)]
#endif
[Preserve(AllMembers = true)]
[Issue(IssueTracker.Github, 14523,
"[Bug] NullReferenceException in EntryRenderer on iOS versions <14.X with ClearButtonVisibility.WhileEditing and non-default TextColor",
PlatformAffected.iOS)]
public partial class Issue14523 : TestContentPage
{
public Issue14523()
{
#if APP
InitializeComponent();
#endif
}

protected override void Init()
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
<Compile Include="$(MSBuildThisFileDirectory)Issue13551.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue13819.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue13916.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue14523.xaml.cs">
<DependentUpon>Issue14523.xaml</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)Issue14428.xaml.cs">
<DependentUpon>Issue14428.xaml</DependentUpon>
</Compile>
Expand Down Expand Up @@ -2755,6 +2758,10 @@
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue14523.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue14428.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
Expand Down
12 changes: 9 additions & 3 deletions Xamarin.Forms.Platform.iOS/Renderers/EntryRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ void UpdateClearButtonColor()
if(_defaultClearImage == null)
_defaultClearImage = clearButton.ImageForState(UIControlState.Highlighted);

if(Element.TextColor == Color.Default)
if (Element.TextColor == Color.Default)
{
clearButton.SetImage(_defaultClearImage, UIControlState.Normal);
clearButton.SetImage(_defaultClearImage, UIControlState.Highlighted);
Expand All @@ -580,14 +580,20 @@ void UpdateClearButtonColor()
{
var tintedClearImage = GetClearButtonTintImage(_defaultClearImage, Element.TextColor.ToUIColor());

clearButton.SetImage(tintedClearImage, UIControlState.Normal);
clearButton.SetImage(tintedClearImage, UIControlState.Highlighted);
if (tintedClearImage != null)
{
clearButton.SetImage(tintedClearImage, UIControlState.Normal);
clearButton.SetImage(tintedClearImage, UIControlState.Highlighted);
}
}
}
}

UIImage GetClearButtonTintImage(UIImage image, UIColor color)
{
if (image == null)
return null;

var size = image.Size;

UIGraphics.BeginImageContextWithOptions(size, false, UIScreen.MainScreen.Scale);
Expand Down

0 comments on commit 30d837a

Please sign in to comment.