instagram

Fonts, Font-sizes and the MacOS

So, one of the questions I’ve occasionally been getting from Mac users is that they would really like the ability to shift the font and font sizes of the programs’ interface.  If you’ve used the Windows version of MarcEdit, this has been available for some time, but I’ve not put it into the Mac version in part, because, I didn’t know how.  The Mac UI is definitely different from what I’m use to, and the way that the AppKit exposes controls and the way controls are structures as a collection of Views and Subviews complicates some of the sizing and layout options.  But I’ve been wanting to provide something because on really high resolution screens, the application was definitely getting hard to read.

Anyway, I’m not sure if this is the best way to do it, but this is what I’ve come up with.  Essentially, it’s a function that can determine if an element has text, an image, and perform the font scaling, control resizing and ultimately, windows sizing to take advantage of Apples Autolayout features.  Code is below.

 

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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
public void SizeLabels(NSWindow objW, NSControl custom_control = null)
		{
			string test_string = "THIS IS MY TEST STRING";
 
			string val = string.Empty;
			string font_name = "";
			string font_size = "";
			NSStringAttributes myattribute = new NSStringAttributes();
 
 
			cxmlini.GetSettings(XMLPath(), "settings", "mac_font_name", "", ref font_name);
			cxmlini.GetSettings(XMLPath(), "settings", "mac_font_size", "", ref font_size);
 
 
 
			if (string.IsNullOrEmpty(font_name) && string.IsNullOrEmpty(font_size))
			{
				return;
			}
 
			NSFont myfont = null;
			if (String.IsNullOrEmpty(font_name))
			{
				myfont = NSFont.UserFontOfSize((nfloat)System.Convert.ToInt32(font_size));
 
			}
			else if (String.IsNullOrEmpty(font_size))
			{
				font_size = "13";
				myfont = NSFont.FromFontName(font_name, (nfloat)System.Convert.ToInt32(font_size));
			}
			else {
				myfont = NSFont.FromFontName(font_name, (nfloat)System.Convert.ToInt32(font_size));
			}
 
 
 
 
			if (custom_control == null)
			{
 
 
				CoreGraphics.CGSize original_size = NSStringDrawing.StringSize(test_string, myattribute);
 
				myattribute.Font = myfont;
				CoreGraphics.CGSize new_size = NSStringDrawing.StringSize(test_string, myattribute);
 
				CoreGraphics.CGRect frame = objW.Frame;
				frame.Size = ResizeWindow(original_size, new_size, frame.Size);
				objW.MinSize = frame.Size;
				objW.SetFrame(frame, true);
				objW.ContentView.UpdateConstraints();
				//objW.ContentView.UpdateTrackingAreas();
 
 
				//MessageBox(objW, objW.Frame.Size.Width.ToString() + ":" + objW.Frame.Size.Height.ToString());
 
				foreach (NSView v in objW.ContentView.Subviews)
				{
					if (v.IsKindOfClass(new ObjCRuntime.Class("NSControl")))
					{
						NSControl mycontrol = ((NSControl)v);
						switch (mycontrol.GetType().ToString())
						{
 
							case "AppKit.NSTextField":
							case "AppKit.NSButtonCell":
							case "AppKit.NSBox":
							case "AppKit.NSButton":
 
								if (mycontrol.GetType().ToString() == "AppKit.NSButton")
								{
									if (((NSButton)mycontrol).Image != null)
									{
										break;
									}
								}
 
								mycontrol.Font = myfont;
								//if (!string.IsNullOrEmpty(mycontrol.StringValue))
								//{
								//	mycontrol.SizeToFit();
								//}
								mycontrol.UpdateConstraints();
								break;
 
						}
 
						if (mycontrol.Subviews.Length > 0)
						{
							SizeLabels(objW, mycontrol);
						}
					}
					else if (v.IsKindOfClass(new ObjCRuntime.Class("NSTabView")))
					{
						NSTabView mytabview = ((NSTabView)v);
						foreach (NSTabViewItem ti in mytabview.Items)
						{
							foreach (NSView tv in ti.View.Subviews)
							{
								if (tv.IsKindOfClass(new ObjCRuntime.Class("NSControl")))
								{
									SizeLabels(objW, (NSControl)tv);
								}
							}
						}
					}
				}
			}
			else {
				if (custom_control.Subviews.Length == 0)
				{
					if (custom_control.GetType().ToString() != "AppKit.NSButton" ||
						(custom_control.GetType().ToString() == "AppKit.NSButton" &&
						 ((NSButton)custom_control).Image == null))
					{
						custom_control.Font = myfont;
						custom_control.UpdateConstraints();
					}
				}
				else {
					foreach (NSView v in custom_control.Subviews)
					{
 
						NSControl mycontrol = ((NSControl)v);
						switch (mycontrol.GetType().ToString())
						{
 
							case "AppKit.NSTextField":
							case "AppKit.NSButtonCell":
							case "AppKit.NSBox":
							case "AppKit.NSButton":
								if (mycontrol.GetType().ToString() == "AppKit.NSButton")
								{
									if (((NSButton)mycontrol).Image != null)
									{
										break;
									}
								}
								mycontrol.Font = myfont;
								//if (!string.IsNullOrEmpty(mycontrol.StringValue))
								//{
								//	mycontrol.SizeToFit();
								//}
								mycontrol.UpdateConstraints();
								break;
							default:
								if (mycontrol.Subviews.Length > 0)
								{
									SizeLabels(objW, mycontrol);
								}
								break;
						}
 
 
 
					}
				}
 
			}
 
		}

And that was it. I’m sure there might be better ways, but this is (crossing my fingers) working for me right now.

0 Comments

Leave Your Reply

Your email address will not be published. Required fields are marked *

*

*