Skip to content

[Label] Certain style attributes not applied when using Label.FormattedText #27828

@MAUIoxo

Description

@MAUIoxo

Description

When I use Label.FormattedText (e.g. to format a string with a localization resource as below), certain style attributes like FontAttributes="Bold"or TextColor="MediumPurple" are ignored when they are used directly on the Label:

<Label FontSize="16" FontAttributes="Bold" TextColor="MediumPurple" Margin="8, 10, 0, 0">
     <Label.FormattedText>
         <FormattedString >
             <Span Text="{loc:Translate SelectItem_LabelHeading}"
             <Span Text=": " />
         </FormattedString>
     </Label.FormattedText>
 </Label>

Image

When they are set on each Span element, e.g. with a style, they are applied, but then an attribute like `FontSize="25" is not applied anymore:

<ContentPage.Resources>
    <ResourceDictionary>
        ...
        <Style x:Key="HeaderLabelStyle" TargetType="Label">
            <Setter Property="FontSize" Value="16"/>
            <Setter Property="TextColor" Value="MediumPurple"/>
        </Style>
    </ResourceDictionary>
</ContentPage.Resources>
...
<Label FontSize="25" Margin="8, 10, 0, 0">
    <Label.FormattedText>
        <FormattedString >
            <!-- Style has to be applied like this on each Span element, otherwise it will be ignored. -->
            <Span Text="Select a value" Style="{StaticResource HeaderLabelStyle}" />
            <Span Text=": " Style="{StaticResource HeaderLabelStyle}" />
        </FormattedString>
    </Label.FormattedText>
</Label>
...

Image


I don't know if this is the intended behavior, but at least from my perspective attributes should be accumulative from top to bottom. So all child elements „inherit“ all the Attributes from their parent first (top to bottom). If a child also sets the same attribute, it should be overwritten for that specific element then.

Therefore, in the snippet above with FontSize="16", all the attributes set on the Label should also be set in the child and therefore be applied to the Span elements.

Steps to Reproduce

  1. Use the attached reproducible example in MainPage.xaml
  2. Define style attributes like FontAttributes="Bold" or TextColor="MediumPurple" on the Label with the Label.FormattedText
  3. See that in this example, an attribute like FontSize="16" is still applied, but other attributes like FontAttributes="Bold" or TextColor="MediumPurple" are ignored on the <Span Text="Select a value" /> and <Span Text=": " /> elements
  4. When styles are applied like in the <Span Text="Select a value" Style="{StaticResource HeaderLabelStyle}" /> and <Span Text=": " Style="{StaticResource HeaderLabelStyle}" /> elements, the style attributes FontAttributes="Bold" or TextColor="MediumPurple" are applied, but then a huge FontSize="25" defined on the Label is ignored

Expected behavior is that in both examples all style attributes should be accumulative and applied down to the child Span elements

Link to public reproduction project repository

FormattedLabelTextIgnoresAttributes_27828

Version with bug

9.0.40 SR4

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

iOS, Android

Affected platform versions

No response

Did you find any workaround?

No response

Relevant log output

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-controls-labelLabel, Spanplatform/androidplatform/ioss/triagedIssue has been revieweds/try-latest-versionPlease try to reproduce the potential issue on the latest public versions/verifiedVerified / Reproducible Issue ready for Engineering Triaget/bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions