c# - Xamarin Forms 使用字体大小缩放组件

标签 c# xamarin.forms xamarin.android

是否可以根据字体大小缩放标签、按钮和框架?我遇到了 accessibility 问题,其中 iOS 有几行可以禁用所有辅助功能缩放,而在谷歌搜索后 android 似乎根本没有它。我有许多内部有文本的组件和具有自定义字体的按钮,这些按钮具有自定义字体的 arrows pauseplay 图标。我需要根据辅助功能中的字体大小是否已更改来缩放整个按钮。

仅适用于 iOS:Accessibility Scaling for Named Font Sizes on iOS ,找不到有关 android 的任何内容。

另一篇文章:Getting Android/iOS Accessibility font sizes ,第一个答案有这行适用于 android Android.Content.Res.Resources.System.Configuration.FontScale 的代码,但我似乎无法访问 Android.Content 任何我尝试使用它的地方。

提前非常感谢。

编辑: Temp Fix 。仍然想知道是否可以使用字体大小进行组件缩放。

enter image description here

引用:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="BugTestMediaElement.Page3">

    <Shell.TitleView>
        <StackLayout HorizontalOptions="FillAndExpand"
                     VerticalOptions="FillAndExpand"
                     BackgroundColor="Red">
            <Frame BackgroundColor="White"
                   VerticalOptions="CenterAndExpand"
                   HorizontalOptions="CenterAndExpand">
                <Label Text="Test"
                       FontSize="{StaticResource ToolbarIconFontSize}"
                       TextColor="Black" />
            </Frame>
        </StackLayout>
    </Shell.TitleView>
</ContentPage>

最佳答案

我使用以下方法。在 App.xaml 文件(应用程序范围声明)中,我做出以下声明:

<Application.Resources>
    <ResourceDictionary>
       <OnPlatform x:Key="ToolbarIconFontSize" x:TypeArguments="x:Double" Android="48" iOS="64" />

    </ResourceDictionary>
</Application.Resources>

然后在页面上(我使用 FontAwesome 的图标),我做出以下声明:

<Shell.TitleView>
    <Grid Style="{StaticResource GridTitle}" RowDefinitions="*" ColumnDefinitions="*, 0.20*, 0.20*">

       <!-- other declarations are skipped -->

       <ImageButton Grid.Row="0" Grid.Column="1" HorizontalOptions="Start" Command="{Binding SomeActionCommand}">
                <ImageButton.Source>
                    <FontImageSource Glyph="{x:Static icons:IconFontGlyphNames.SomeActionSymbol}" 
                                     FontFamily="IconFontFamily" 
                                     Color="White" 
                                     Size="{StaticResource ToolbarIconFontSize}" />                    
                </ImageButton.Source>
       </ImageButton>

       <!-- other declarations are skipped -->

    </Grid>
</Shell.TitleView>

IconFontGlyphNames 类声明如下所示:

public static class IconFontGlyphNames
{
    public const string SomeActionSymbol = "\uf234";

    // other declarations are skipped
}

我使用这种方法设置图标和文本的字体大小,它在 Android 和 iOS 平台上运行良好。


更新#1。解释 `Shell.TitleView` 位置。

Shell.TitleView 位于此处:

<ContentPage>

   <Shell.TitleView>
      <!-- Shell title view elements go here -->
   </Shell.TitleView>

   <ContentPage.Content>
      <!-- Your page body elements go here -->
   </ContentPage.Content>

</ContentPage>

关于c# - Xamarin Forms 使用字体大小缩放组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74443797/

相关文章:

c# - MainPageModel 的构造函数中的 App.Current.MainPage NullReferenceException

c# - 在代码隐藏之外的其他地方捕获事件

c# - 在 C# 中使用弱处理程序引发 PropertyChanged 事件时发生 ExecutionEngineException

c# - SQLite 连接策略

c# - 使用 Serilog 文件接收器维护(单个)稳定文件名

xamarin.android - Xamarin.Forms(UWP、Droid 和 iOS)的 Breeze#?

c# - 如何从后面的代码设置 CarouselView 的项目?

android - TabbedPage 中 ListView 中的 Xamarin Forms Slider 无法正确滑动

android - 使用 NuGet 安装 GooglePlayServices 时出现安装包错误

c# - 自定义 WebView 在模拟器中工作但不在物理设备中工作。赛马林