Xamarin.forms: [Bug] Changing text on a Label on macOS resets font color to Black

Created on 26 Mar 2020  路  15Comments  路  Source: xamarin/Xamarin.Forms

Description

The Issue started appearing in XF 4.5, 4.4 is OK

Steps to Reproduce

  1. Define a Label on a page with some text in it
  2. Define Label style in app.xaml
        <!-- FONTS -->
        <OnPlatform x:TypeArguments="Font" x:Key="FontNormal">
            <On Platform="macOS" Value="IBM Plex Mono, 15" />
            <On Platform="iOS" Value="IBM Plex Mono, 15" />
            <On Platform="WPF" Value="./Fonts/IBMPlexMono-Regular.ttf#IBM Plex Mono, 15" />
            <On Platform="Android, iOS" Value="IBMPlexMono-Regular.ttf#IBMPlexMono-Regular, 15" />
        </OnPlatform>
        <!-- LABELS -->
        <Style TargetType="Label" ApplyToDerivedTypes="True">
            <Setter Property="Font" Value="{StaticResource FontNormal}" />
            <Setter Property="TextColor" Value="Red" />
        </Style>
  1. Run the app, see the color of text is applied
  2. Change the text of the Label in code behind (e.g. on button click)

Expected Behavior

The text color stays as it is defined in Style

Actual Behavior

The color is set to black

4.5.0 regression in-progress macOS bug

Most helpful comment

sorry to jump in uninvited, i still have this problem and im on 4.8.0.1451.

All 15 comments

There's a problem with Button as well. If Text is updated in code behind the font get's corrupted.
macOS Button

Any news? XF 4.5 and up is barely useful for macOS with even basic UI being corrupted.

i'm also still having this issue in 4.6.0.800. It seems that a Label with a static text defined works fine with a TextColor but once the value of the Label comes from Code-Behind or DataBinding it's defaulting the color to black

Would be nice to see this having a higher prio - 4.6.0 is completely unusable with black labels only! @samhouts

here is a repro:
https://github.com/nor0x/repro-XF-10789

related / duplicate #9526 ? https://github.com/xamarin/Xamarin.Forms/pull/10120

I'm also waiting for the fix with the fingers crossed.

Seems that there was commit https://github.com/xamarin/Xamarin.Forms/pull/8206/files
Motivation was to AddCharacterSpacing by using AttributedStringValue.
For maintaining this attribute UpdateTextColor() was modified to change color in AttributedStringValue but not in NSLabel.TextColor.
But UpdateTextPlainText() is not using attributed string, thus it sets usual StringValue with default value in NSLabel.TextColor.

Workaround is to implement own LabelRenderer and override there OnElementPropertyChanged

[assembly: ExportRenderer(typeof(Label), typeof(MacLabelRenderer))]
namespace MyProject.Renderers
{
    public class MacLabelRenderer : LabelRenderer
    {
        protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == Label.TextProperty.PropertyName)
            {
                Control.TextColor = Element.TextColor.ToNSColor();
            }

            base.OnElementPropertyChanged(sender, e);
        }
    }
}

@nor0x would you by any chance be able to test the NuGets resulting from #10120 on your reproduction? See here how to do that.

@maxim-saplin could you maybe add a reproduction for the button one? Thank you :)

@jfversluis https://github.com/maxim-saplin/TimeSeries-Xamarin - that's the repo with button text bug. Still reproducible on XF 4.6.0.800. The steps are quite straightforward, changing button text in code behind breaks button font:

            if (collectionTypeButton.Text == showingRandom)
                collectionTypeButton.Text = showingParabola;
            else collectionTypeButton.Text = showingRandom;

I only see you setting a custom font on a label in a custom control, right?

Could you please point me where you are setting some custom things on your button? Or even better, lift that code to a separate repro project

Right, custom font is only used with Labels. Buttons get font jumps without any customizations, simple changes of Text value in code behind leads to buttons' font size increase.

Yep definitely see that happening!

@jfversluis just tested the repro with the NuGets from #10120 and color changing is working as expected

Hi @jfversluis 馃憢 do you have an update on this or do you need more repro info?

@maxim-saplin according to @nor0x this is fixed as of 4.7.0.1239 would you be able to confirm?

If so, #10120 can be closed and we can move on 馃帀

@jfversluis sorry, can't test it. I've removed VS and Xamarin from my laptop.

sorry to jump in uninvited, i still have this problem and im on 4.8.0.1451.

Was this page helpful?
0 / 5 - 0 ratings