使用自定义渲染器,我可以使 TableSection.Title 以小型混合大小写形式显示吗?

2024-05-12

这是我目前拥有的:

<TableView Intent="Settings">
   <TableRoot>
      <TableSection>
         <TableSection.Title>
            This appears in uppercase
         </TableSection.Title>

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


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

https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/custom-renderer/renderers/ 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=58731另一个问题是文本转换也不起作用https://bugzilla.xamarin.com/show_bug.cgi?id=58732 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>

页面代码

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

    public class CustomTableView : TableView
    {

    }
}

Renderer

[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);
                    }
                }
            }
        }
    }   
}

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用自定义渲染器,我可以使 TableSection.Title 以小型混合大小写形式显示吗? 的相关文章

随机推荐