コメント
コメントの投稿
※名前とタイトルが入力されていないコメントでは他のコメントとの区別ができません。
入力されていないコメントには返信しませんのであらかじめご了承くださいませ。
using Xamarin.Forms;
namespace AppName.Controls
{
public class BaseImageCell : ImageCell
{
public BaseImageCell() : base()
{
}
public static readonly BindableProperty FontFamilyProperty =
BindableProperty.Create("FontFamily",
typeof(string),
typeof(BaseImageCell),
Font.Default.FontFamily);
public string FontFamily
{
get { return (string)GetValue(FontFamilyProperty); }
set { SetValue(FontFamilyProperty, value); }
}
}
}
using System;
using Android.Content;
using Android.Views;
using Android.Widget;
using Android.Graphics;
using Xamarin.Forms.Platform.Android;
using Xamarin.Forms;
using AppName.Droid.Renderer;
using AppName.Controls;
[assembly: ExportRenderer(typeof(BaseImageCell), typeof(CustomImageCellRenderer))]
namespace AppName.Droid.Renderer
{
public class CustomImageCellRenderer : ImageCellRenderer
{
//Fontを使いまわす為
private static Typeface _font = null;
protected override global::Android.Views.View GetCellCore(Cell item, global::Android.Views.View convertView, ViewGroup parent, Context context)
{
var cell = (BaseCellView)base.GetCellCore(item, convertView, parent, context);
if (cell != null)
{
//ImageCellの内部のTextViewを取得する
var linear = (LinearLayout)cell.GetChildAt(1);
var label = (TextView)linear.GetChildAt(0);
//BaseImageCellのカスタムプロパティに設定されたフォント名を取得する
var imageCell = (BaseImageCell)item;
string fontName = imageCell.FontFamily;
//フォントを設定する
if (_font == null)
{
_font = Typeface.CreateFromAsset(Android.App.Application.Context.Assets, fontName);
}
label.SetText(imageCell.Text, TextView.BufferType.Normal);
label.SetTypeface(_font, TypefaceStyle.Normal);
}
return cell;
}
}
}
using System;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
using AppName.iOS.Renderer;
using AppName.Library.Utility;
using AppName.Controls;
[assembly: ExportRenderer(typeof(BaseImageCell), typeof(CustomImageCellRenderer))]
namespace AppName.iOS.Renderer
{
public class CustomImageCellRenderer : ImageCellRenderer
{
public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv)
{
var cell = base.GetCell(item, reusableCell, tv);
if (cell != null)
{
//BaseImageCellのカスタムプロパティに設定されたフォント名を取得する
BaseImageCell imageCell = (BaseImageCell)item;
string fontName = imageCell.FontFamily;
//フォントを設定する
UILabel label1 = cell.DetailTextLabel;
label1.Font = UIFont.FromName(fontName, label1.Font.PointSize);
UILabel label2 = cell.TextLabel;
label2.Font = UIFont.FromName(fontName, label2.Font.PointSize);
UpdateBackground(cell, item);
}
return cell;
}
}
}
using AppName.Controls;
using Xamarin.Forms;
namespace AppName.Controls
{
public class MenuListView : ListView
{
public MenuListView()
{
this.Initialize();
}
public MenuListView(ListViewCachingStrategy strategy) : base(strategy)
{
this.Initialize();
}
public void Initialize()
{
// ListView の設定
List<MenuItem> data = new MenuListData();
ItemsSource = data;
VerticalOptions = LayoutOptions.FillAndExpand;
BackgroundColor = Color.Transparent;
// ImageCellをバインド
var cell = new DataTemplate(typeof(BaseImageCell));
cell.SetBinding(TextCell.TextProperty, "Title");
cell.SetBinding(ImageCell.ImageSourceProperty, "IconSource");
cell.SetBinding(BaseImageCell.FontFamilyProperty, "FontFamily"); //今回作成した派生コントロールのプロパティです。
ItemTemplate = cell;
}
}
/// <summary>
/// ListView のデータ用のクラスです。
/// </summary>
public class MenuItem
{
public string Title { get; set; }
public string IconSource { get; set; }
public string FontFamily
{
get
{
switch (Device.RuntimePlatform)
{
case Device.iOS:
return "Georgia-BoldItalic"; //PostScript名
case Device.Android:
return "Fonts/Georgia-BoldItalic.ttf"; //ファイル名
default:
throw new NotImplementedException("想定していないプラットフォームです。");
}
}
}
}
}
※名前とタイトルが入力されていないコメントでは他のコメントとの区別ができません。
入力されていないコメントには返信しませんのであらかじめご了承くださいませ。