forked from unoplatform/uno
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUIStringAttributesHelper.iOS.cs
More file actions
100 lines (91 loc) · 2.87 KB
/
Copy pathUIStringAttributesHelper.iOS.cs
File metadata and controls
100 lines (91 loc) · 2.87 KB
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
using System;
using Foundation;
using Windows.UI;
using Windows.UI.Text;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Documents;
using UIKit;
using Uno.Extensions;
namespace Uno.UI
{
internal static class UIStringAttributesHelper
{
private static Func<
(
FontWeight fontWeight,
FontStyle fontStyle,
FontFamily fontFamily,
Brush foreground,
double fontSize,
int characterSpacing,
BaseLineAlignment baseLineAlignment,
TextDecorations textDecorations,
float? preferredBodyFontSize
),
UIStringAttributes
> _getAttributes;
static UIStringAttributesHelper()
{
_getAttributes = InternalGetAttributes;
_getAttributes = _getAttributes.AsMemoized();
}
internal static UIStringAttributes GetAttributes(
FontWeight fontWeight,
FontStyle fontStyle,
FontFamily fontFamily,
Brush foreground,
double fontSize,
int characterSpacing,
BaseLineAlignment baseLineAlignment,
TextDecorations textDecorations
)
{
return _getAttributes((fontWeight, fontStyle, fontFamily, foreground, fontSize, characterSpacing, baseLineAlignment, textDecorations, UIFont.PreferredBody.FontDescriptor.FontAttributes.Size));
}
private static UIStringAttributes InternalGetAttributes((
FontWeight fontWeight,
FontStyle fontStyle,
FontFamily fontFamily,
Brush foreground,
double fontSize,
int characterSpacing,
BaseLineAlignment baseLineAlignment,
TextDecorations textDecorations,
float? preferredBodyFontSize) tuple
)
{
float? GetBaselineOffset()
{
switch (tuple.baseLineAlignment)
{
case BaseLineAlignment.Superscript:
// We assume that the fontsize will be set according to the 0.56 ratio from the non-superscript text.
return (float)tuple.fontSize * 0.56f;
case BaseLineAlignment.Baseline:
return null;
default:
throw new NotSupportedException("The baseline alignment {0} is not supported".InvariantCultureFormat(tuple.baseLineAlignment));
}
}
var font = UIFontHelper.TryGetFont((float)tuple.fontSize, tuple.fontWeight, tuple.fontStyle, tuple.fontFamily, tuple.preferredBodyFontSize);
var attributes = new UIStringAttributes()
{
ForegroundColor = (tuple.foreground as SolidColorBrush)?.ColorWithOpacity,
Font = font,
BaselineOffset = GetBaselineOffset(),
UnderlineStyle = (tuple.textDecorations & TextDecorations.Underline) == TextDecorations.Underline
? NSUnderlineStyle.Single
: NSUnderlineStyle.None,
StrikethroughStyle = (tuple.textDecorations & TextDecorations.Strikethrough) == TextDecorations.Strikethrough
? NSUnderlineStyle.Single
: NSUnderlineStyle.None,
};
if (tuple.characterSpacing != 0f)
{
//CharacterSpacing is in 1/1000 of an em, iOS KerningAdjustment is in points. 1 em = 12 points
attributes.KerningAdjustment = (tuple.characterSpacing / 1000f) * 12;
}
return attributes;
}
}
}