コメント
コメントの投稿
※名前とタイトルが入力されていないコメントでは他のコメントとの区別ができません。
入力されていないコメントには返信しませんのであらかじめご了承くださいませ。
<plist version="1.0">
<dict>
<key>UIAppFonts</key>
<array>
<string>Fonts/07YasashisaGothic.ttf</string>
<string>Fonts/JK-Gothic-M.ttf</string>
</array>
</dict>
</plist>
using Android.Graphics;
using Xamarin.Forms.Platform.Android;
using Xamarin.Forms;
[assembly: ExportRenderer(typeof(Xamarin.Forms.Button), typeof(CustomButtonRenderer))]
namespace AppName.Droid.Renderer
{
public class CustomButtonRenderer : ButtonRenderer
{
//Fontを使いまわす為
private static Typeface _font = null;
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Button> e)
{
base.OnElementChanged(e);
try
{
if (_font == null)
{
//Fontが取得できていない場合は取得する
var fontName = e.NewElement?.FontFamily;
if (!string.IsNullOrEmpty(fontName))
{
_font = Typeface.CreateFromAsset(Forms.Context.Assets, fontName);
}
}
if (_font != null)
{
//Fontが取得できていればフォントを設定する
Control.Typeface = _font;
}
}
catch (Exception ex)
{
System.Console.WriteLine(ex.Message + System.Environment.NewLine +
ex.StackTrace);
}
}
}
}
using Android.Graphics;
using Xamarin.Forms.Platform.Android;
using Xamarin.Forms;
[assembly: ExportRenderer(typeof(Label), typeof(CustomLabelRenderer))]
namespace AppName.Droid.Renderer
{
public class CustomLabelRenderer : LabelRenderer
{
//Fontを使いまわす為
private static Typeface _font = null;
protected override async void OnElementChanged(ElementChangedEventArgs<Label> e)
{
base.OnElementChanged(e);
if (Control == null || e.NewElement == null)
{
return;
}
try
{
if (_font == null)
{
//Fontが取得できていない場合は取得する
var fontName = e.NewElement?.FontFamily;
if (!string.IsNullOrEmpty(fontName))
{
_font = Typeface.CreateFromAsset(Forms.Context.Assets, fontName);
}
}
if (_font != null)
{
//Fontが取得できていればフォントを設定する
Control.Typeface = _font;
}
}
catch (Exception ex)
{
System.Console.WriteLine(ex.Message + System.Environment.NewLine +
ex.StackTrace);
}
}
}
}
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
[assembly: ExportRenderer(typeof(Button), typeof(CustomButtonRenderer))]
namespace AppName.iOS.Renderer
{
public class CustomButtonRenderer : ButtonRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
{
base.OnElementChanged(e);
try
{
var fontName = e.NewElement?.FontFamily;
if (!string.IsNullOrEmpty(fontName))
{
Control.TitleLabel.Font = UIFont.FromName(fontName, Control.TitleLabel.Font.PointSize);
}
}
catch (Exception ex)
{
System.Console.WriteLine(ex);
}
}
}
}
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
[assembly: ExportRenderer(typeof(Label), typeof(CustomLabelRenderer))]
namespace AppName.iOS.Renderer
{
public class CustomLabelRenderer : LabelRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
{
base.OnElementChanged(e);
try
{
var fontName = e.NewElement?.FontFamily;
if (!string.IsNullOrEmpty(fontName))
{
Control.Font = UIFont.FromName(fontName, Control.Font.PointSize);
}
}
catch (Exception ex)
{
System.Console.WriteLine(ex);
}
}
}
}
using Xamarin.Forms;
namespace AppName.Controls
{
public class BaseButton : Button
{
public BaseButton() : base()
{
this.FontAttributes = FontAttributes.Bold;
switch (Device.RuntimePlatform)
{
case Device.iOS:
this.FontFamily = "JK-Gothic-M"; //"07YasashisaGothic"; //PostScript名
break;
case Device.Android:
this.FontFamily = "Fonts/JK-Gothic-M.ttf"; //ファイル名
break;
}
}
}
}
using Xamarin.Forms;
namespace AppName.Controls
{
public class BaseLabel : Label
{
public BaseLabel() : base()
{
switch (Device.RuntimePlatform)
{
case Device.iOS:
this.FontFamily = "JK-Gothic-M"; //"07YasashisaGothic"; //PostScript名
break;
case Device.Android:
this.FontFamily = "Fonts/JK-Gothic-M.ttf"; //ファイル名
break;
}
}
}
}
<ContentPage xmlns:app="clr-namespace:ソリューション名.Controls;assembly=アプリ名">
<app:BaseLabel x:Name="lblTest" Text="ラベルだよ"/>
<app:BaseButton x:Name="btnSave" Text="ボタンだよ"/>
</ContentPage>
※名前とタイトルが入力されていないコメントでは他のコメントとの区別ができません。
入力されていないコメントには返信しませんのであらかじめご了承くださいませ。