对于 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);
}
}
}
}
}
}
最终结果具有小字体大小写敏感标题