使用自定义渲染器,我可以使TableSection.Title以小混合的形式出现吗?

Ala*_*an2 9 xamarin xamarin.forms

这是我现在拥有的:

<TableView Intent="Settings">
   <TableRoot>
      <TableSection>
         <TableSection.Title>
            This appears in uppercase
         </TableSection.Title>
Run Code Online (Sandbox Code Playgroud)

是否有一种方法可能与iOS自定义渲染器,我可以将显示的字体转换为混合的大写和小写,并使字体大小更小,如我在设置>控制中心看到Apple用户?

Yur*_*i S 4

对于 iOS,您需要具有 UITableView 原生控制的 XF TableView TableViewRenderer。更多这里:

https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/custom-renderer/renderers/

下面是解决方案。函数 Draw 的渲染器中的代码应该在 OnElementChanged 中完成,但不幸的是,Xamarin 似乎有一个错误https://bugzilla.xamarin.com/show_bug.cgi?id=58731另一个问题是文本转换不起作用https: //bugzilla.xamarin.com/show_bug.cgi?id=58732

还有一个小的优化 - 避免每次添加控件绘制的 textDecapitalized 时都在渲染器中进行文本转换。回答另一个问题如何更改文本大小我添加了 hv.TextLabel.Font 集(已注释掉但有效)。

因此,解决这两个错误:

XML

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:ButtonRendererDemo;assembly=ButtonRendererDemo"
             x:Class="ButtonRendererDemo.CustomTablePage">

    <ContentPage.Content>
        <local:CustomTableView Intent="Settings">
            <TableRoot>
                <TableSection Title="First Case Sensitive Header">
                    <SwitchCell Text="New Voice Mail" />
                </TableSection>
                <TableSection Title="Second Case Sensitive Header">
                    <SwitchCell Text="New Mail" On="true" />
                </TableSection>
            </TableRoot>
        </local:CustomTableView>
    </ContentPage.Content>
</ContentPage>
Run Code Online (Sandbox Code Playgroud)

页面代码

namespace ButtonRendererDemo
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class CustomTablePage : ContentPage
    {
        public CustomTablePage()
        {
            InitializeComponent();
        }
    }

    public class CustomTableView : TableView
    {

    }
}
Run Code Online (Sandbox Code Playgroud)

渲染器

[assembly: ExportRenderer(typeof(CustomTableView), typeof(CustomTableViewRenderer))]
namespace ButtonRendererDemo.iOS
{
    public class CustomTableViewRenderer : TableViewRenderer
    {
        bool textDecapitalized = false;

        public override void Draw(CGRect rect)
        {
            base.Draw(rect);

            if (!textDecapitalized)
            {
                textDecapitalized = true;

                var tableView = Control as UITableView;
                var numSections = tableView.NumberOfSections();
                for (nint s = 0; s < numSections; s++)
                {
                    var hv = tableView.GetHeaderView(s);
                    if (hv != null) //always null in OnElementChanged. Bug reported
                    {
                        //unfortunately TextInfo doesn't work. Bug reported
                        //TextInfo textInfo = new CultureInfo("en-US", false).TextInfo;
                        // OR
                        //TextInfo textInfo = Thread.CurrentThread.CurrentCulture.TextInfo;

                        if (hv.TextLabel.Text.ToUpper().StartsWith("FIR"))
                            hv.TextLabel.Text = "First Case Sensitive Header";
                        else if (hv.TextLabel.Text.ToUpper().StartsWith("SEC"))
                            hv.TextLabel.Text = "Second Case Sensitive Header";

                        //hv.TextLabel.Font = UIFont.FromName(hv.TextLabel.Font.Name, 5f);
                    }
                }
            }
        }
    }   
}
Run Code Online (Sandbox Code Playgroud)

最终结果具有小字体大小写敏感标题

在此输入图像描述