コメント
コメントの投稿
※名前とタイトルが入力されていないコメントでは他のコメントとの区別ができません。
入力されていないコメントには返信しませんのであらかじめご了承くださいませ。
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
[assembly: ExportRenderer(typeof(NavigationPage), typeof(CustomNavigationRenderer))]
public class CustomNavigationRenderer : NavigationRenderer
{
public override void ViewDidLoad()
{
base.ViewDidLoad();
//ナビゲーションのスタイル変更
//背景色
this.NavigationBar.BarTintColor = UIColor.Blue;
//戻るボタンも含む文字色
this.NavigationBar.TintColor = UIColor.White;
//タイトルフォント
this.NavigationBar.TitleTextAttributes = new UIStringAttributes()
{
Font = UIFont.FromName("フォントのPostScript名", 20),
ForegroundColor = UIColor.White, //タイトルフォントカラー
};
//戻るボタンもフォント変更する場合
UIBarButtonItem.Appearance.SetTitleTextAttributes(new UITextAttributes()
{
Font = UIFont.FromName("フォントのPostScript名", 16),
//TextColor = UIColor.White,
//TextColorはイメージピッカーのような画面のキャンセルボタンまで白色になってしまうので設定しないほうが良い。
}, UIControlState.Normal);
}
}
[Register("AppDelegate")]
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
global::Xamarin.Forms.Forms.Init();
LoadApplication(new App());
//ナビゲーションタイトルの色変更
UINavigationBar.Appearance.BarTintColor= UIColor.Blue;
UINavigationBar.Appearance.TintColor = UIColor.White;
UINavigationBar.Appearance.SetTitleTextAttributes(new UITextAttributes()
{
Font = UIFont.FromName("フォントのPostScript名", 20),
TextColor = UIColor.White,
});
return base.FinishedLaunching(app, options);
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<TextView
style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Toolbar Title"
android:gravity="center_vertical"
android:id="@+id/titleView" />
</android.support.v7.widget.Toolbar>
using System.ComponentModel;
using Android.Widget;
using Android.Graphics;
using Support = Android.Support.V7.Widget;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using Xamarin.Forms.Platform.Android.AppCompat;
[assembly: ExportRenderer(typeof(Xamarin.Forms.NavigationPage), typeof(CustomNavigationPageRenderer))]
public class CustomNavigationPageRenderer : NavigationPageRenderer
{
private Support.Toolbar _toolbar;
public override void OnViewAdded(Android.Views.View child)
{
base.OnViewAdded(child);
if (child.GetType() == typeof(Support.Toolbar))
_toolbar = (Support.Toolbar)child;
}
/// <summary>
/// ツールバータイトルのフォントを変更する
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
this.SetTitle();
}
/// <summary>
/// 画面の回転時のイベント(タイトルを再設定する)
/// </summary>
/// <param name="changed"></param>
/// <param name="l"></param>
/// <param name="t"></param>
/// <param name="r"></param>
/// <param name="b"></param>
protected override void OnLayout(bool changed, int l, int t, int r, int b)
{
base.OnLayout(changed, l, t, r, b);
this.SetTitle();
}
/// <summary>
/// タイトルを設定する
/// </summary>
private void SetTitle()
{
var page = this.Element as NavigationPage;
if (page != null && _toolbar != null)
{
Typeface tf = Typeface.CreateFromAsset(Android.App.Application.Context.Assets, "Fonts/拡張子付きフォント名");
TextView titleView = (TextView)_toolbar.FindViewById(Resource.Id.titleView);
titleView.SetText(page.CurrentPage.Title, TextView.BufferType.Normal);
titleView.SetTypeface(tf, TypefaceStyle.Normal);
}
}
}
※名前とタイトルが入力されていないコメントでは他のコメントとの区別ができません。
入力されていないコメントには返信しませんのであらかじめご了承くださいませ。