使用自定义渲染器,我可以使TableSection.Title以小写混合大小写出现吗? [英] With a custom renderer can I make a TableSection.Title appear in small mixed case?

查看:91
本文介绍了使用自定义渲染器,我可以使TableSection.Title以小写混合大小写出现吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我目前拥有的:

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

iOS自定义渲染器是否可以将显示的字体转换为大小写混合的字体并减小字体大小,例如在设置">控制中心"中看到Apple用户?

Is there a way perhaps with an iOS custom renderer that I could convert the font that displays to a mixed upper and lower case and make the font size smaller such as I see Apple user in Settings > Control Center ?

推荐答案

对于iOS,您需要具有UITableView原生控件的XF TableView TableViewRenderer.此处更多:

For iOS you need for XF TableView TableViewRenderer with native control of UITableView. More here:

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

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

Below is the solution. The code in renderer of function Draw should be done in OnElementChanged but unfortunately it seems like Xamarin has a bug https://bugzilla.xamarin.com/show_bug.cgi?id=58731 Another problem that text conversion doesn't work either https://bugzilla.xamarin.com/show_bug.cgi?id=58732

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

One more small optimisation - to avoid doing text conversion in renderer every time control drawn textDecapitalized was added. Answering another question how to change text size I added hv.TextLabel.Font set (commented out but working).

因此,解决以下两个错误:

so, working around these 2 bugs:

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
    {

    }
}

渲染器

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

最终结果,带有较小的区分大小写的标头

Final result with small font case sensitive header

这篇关于使用自定义渲染器,我可以使TableSection.Title以小写混合大小写出现吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆