-
Notifications
You must be signed in to change notification settings - Fork 16
/
CustomFontNavigationPageRenderer.cs
135 lines (111 loc) · 3.84 KB
/
CustomFontNavigationPageRenderer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
using System;
using System.Collections.Generic;
using Foundation;
using ObjCRuntime;
using Xamarin.Forms;
using ToolbarCustomFont.iOS;
using Xamarin.Forms.Platform.iOS;
using UIKit;
[assembly: ExportRenderer(typeof (NavigationPage), typeof (CustomFontNavigationPageRenderer))]
namespace ToolbarCustomFont.iOS
{
// https://blog.xamarin.com/custom-fonts-in-ios/
public class CustomFontNavigationPageRenderer : NavigationRenderer
{
public CustomFontNavigationPageRenderer()
{
}
const string customFontName = "fontawesome";
nfloat customFontSize = 16;
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
if (this.NavigationBar == null) return;
SetNavBarStyle();
// SetNavBarTitle();
SetNavBarItems();
}
public override void ViewDidAppear(bool animated)
{
base.ViewDidAppear(animated);
if (this.NavigationBar == null) return;
var navPage = this.Element;
if (navPage == null) return;
}
private void SetNavBarStyle()
{
NavigationBar.ShadowImage = new UIImage();
NavigationBar.SetBackgroundImage(new UIImage(), UIBarMetrics.Default);
UINavigationBar.Appearance.ShadowImage = new UIImage();
UINavigationBar.Appearance.SetBackgroundImage(new UIImage(), UIBarMetrics.Default);
}
private void SetNavBarItems()
{
var navPage = this.Element as NavigationPage;
if (navPage == null) return;
var textAttributes = new UITextAttributes()
{
Font = UIFont.FromName(customFontName, customFontSize)
};
var textAttributesHighlighted = new UITextAttributes()
{
TextColor = Color.Black.ToUIColor(),
Font = UIFont.FromName(customFontName, customFontSize)
};
var theFont = UIFont.FromName(customFontName, customFontSize);
var nsDictSegmentFont = GetFontNSDictionaryReversed(theFont);
var segmentTextAttributes = new MyUITextAttributes(nsDictSegmentFont);
UISegmentedControl.Appearance.SetTitleTextAttributes(segmentTextAttributes, UIControlState.Normal);
UISegmentedControl.Appearance.SetTitleTextAttributes(segmentTextAttributes, UIControlState.Selected);
UIBarButtonItem.Appearance.SetTitleTextAttributes(textAttributes,
UIControlState.Normal);
UIBarButtonItem.Appearance.SetTitleTextAttributes(textAttributesHighlighted,
UIControlState.Highlighted);
}
// assembles a reversed dictionary to workaround bug in iOS
public NSDictionary GetFontNSDictionaryReversed(UIFont font)
{
var handle = Dlfcn.dlopen("/System/Library/Frameworks/UIKit.framework/UIKit", 0);
var nsfontattributename = "NSFontAttributeName";
//var nsfontattributename = "UITextAttributeFont"; // deprecated
NSString UITextAttributeFont = Dlfcn.GetStringConstant(handle, nsfontattributename);
var valuekeys = new List<NSObject>();
if (font != null)
{
valuekeys.Add(font);
valuekeys.Add(UITextAttributeFont);
}
return new NSDictionary(valuekeys[0], valuekeys[0]);
}
// private void SetNavBarTitle()
// {
// var navPage = this.Element as NavigationPage;
//
// if (navPage == null) return;
//
// this.NavigationBar.TitleTextAttributes = new UIStringAttributes
// {
// Font = UIFont.FromName(customFontName, customFontSize),
// };
// }
}
public class MyUITextAttributes : UITextAttributes
{
private NSDictionary myDictionary;
public MyUITextAttributes(NSDictionary newDictionary)
{
this.myDictionary = newDictionary;
}
public new NSDictionary Dictionary
{
get
{
return ToDictionary();
}
}
public new NSDictionary ToDictionary()
{
return myDictionary;
}
}
}