Set label font size per platform in xaml - not possible?

MellsonMellson DKUniversity ✭✭

I wan't to set a label's fontsize per platform, and I wish to do it in XAML.
However it's not really working for me.

This doesn't compile:
<Label Text="99 FontSize Problems, but the attribute ain't one" HorizontalOptions="Center" VerticalOptions="Center"> <Label.FontSize> <OnPlatform x:TypeArguments="NamedSize" iOS="Default" Android="Medium" /> </Label.FontSize> </Label>

and when I try this instead:
<Label Text="99 FontSize Problems, but the attribute ain't one" HorizontalOptions="Center" VerticalOptions="Center"> <Label.Font> <OnPlatform x:TypeArguments="Font" iOS="Default, Bold" Android="Medium, Italic" /> </Label.Font> </Label>
It gives me the correct attribute but ignores my size request.
If I set the FontSize on the Label directly I get the correct size.

Does anyone know how to do this the right way?

Best Answer

Answers

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    I highly recommend using Style definitions. That way you set it in one place instead of on 500 individual controls, making changes far easier.
    https://redpillxamarin.com/2016/12/23/203-now-for-the-left-foot/

  • MellsonMellson DKUniversity ✭✭

    Thanks for the reply. However a Style definition does not seem to work with named sizes, at least I can't make it work here.

  • VelocityVelocity NZMember ✭✭✭

    @Mellson Very much possible, recommend using Style as suggested by @ClintStLaurent.

            <Style x:Key="MyLabelStyle" TargetType="Label">
                <Setter Property="TextColor" Value="Black" />
                <Setter Property="FontSize">
                    <OnPlatform x:TypeArguments="x:String" Android="Medium" iOS="Medium" WinPhone="Small" />
                </Setter>
            </Style>
    
  • MellsonMellson DKUniversity ✭✭

    @Velocity thanks for the suggestion!
    However it does not work for me. The TextColor does change, but the FontSize does not.
    Can you get it to work on your system?

    I am using Xamarin.Forms 2.3.3.193 on VS2017 and on VS for Mac Preview 5.

  • VelocityVelocity NZMember ✭✭✭

    Yes, above code is tested working with Xamarin Forms 2.33.193 on iOS, Android and UWP platforms.

  • MellsonMellson DKUniversity ✭✭

    @Velocity that is weird. It does not work here. It works if I use specific numeric sizes, but not the named sizes. Hopefully someone from the Xamarin team will see this post and chime in.

  • VelocityVelocity NZMember ✭✭✭

    If you can post a link to a project showing the problem, someone can take a look.
    In the meantime, I would suggest moving to pixel-based sizing.

  • MellsonMellson DKUniversity ✭✭

    Thanks @Velocity for the suggestion - I have created a very simple project showcasing the problem here - https://github.com/appbureauet/NamedSizeTest

    I will report this at BugZilla and see where it goes.

  • MellsonMellson DKUniversity ✭✭
    Accepted Answer

    This is now recognized as a bug here - https://bugzilla.xamarin.com/show_bug.cgi?id=54416

  • NMackayNMackay GBInsider, University mod
    edited May 2017

    Ran into this as well, totally sucks this bug.

    EDIT:

    Just to clarify this does work with FontSize and numeric so it's not the end of the world at all :smile:

  • +1 also having the bug. Have already tried all the suggested solutions, no change in the problem.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    @CalmBreathsSlowPulse said:
    +1 also having the bug. Have already tried all the suggested solutions, no change in the problem.

    You're replying to a thread over a year and half old - talking about Xam Forms versions that are far out of date etc.
    All of the issues in this thread were fixed long ago.
    If you're having an issue with a CURRENT version of the IDE, Xamarin forms etc. then you probably need to make a new thread.

  • JamesLaveryJamesLavery GBBeta, University ✭✭✭✭✭
    edited October 2018

    @ClintStLaurent said:

    @CalmBreathsSlowPulse said:
    +1 also having the bug. Have already tried all the suggested solutions, no change in the problem.

    You're replying to a thread over a year and half old - talking about Xam Forms versions that are far out of date etc.
    All of the issues in this thread were fixed long ago.
    If you're having an issue with a CURRENT version of the IDE, Xamarin forms etc. then you probably need to make a new thread.

    Like this one? :)

    @CalmBreathsSlowPulse - would have been worth posting a link to your new thread (if it is indeed the same bug) to avoid the wrath of Clint ;) Actually Clint was being quite gentle there...

Sign In or Register to comment.