Skip to content

Commit 8935e2f

Browse files
committed
* Readme;
1 parent 26e9fb7 commit 8935e2f

9 files changed

Lines changed: 157 additions & 5 deletions

File tree

README.md

Lines changed: 154 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,154 @@
1-
# Iconfont.Generator
2-
A generator for iconfont.
1+
# About
2+
This library provides an ability to generate icon classes from iconfont.json which comes from [https://www.iconfont.cn/](https://www.iconfont.cn/). Some other tools those can provides iconfont will be supported soon if you file an issue to us.
3+
4+
You can use it in avalonia (and wpf in future), in kinds of ways, just like this:
5+
```xaml
6+
<TextBlock Text="{Fruit Orange}" />
7+
<TextBlock>
8+
<Run Text="{Fruit Apple}" FontFamily="{IconFamily Fruit}"/>
9+
</TextBlock>
10+
```
11+
12+
## Promotion
13+
14+
![img](img/DevTools.png)
15+
16+
We have created a powerful DevTools for Avalonia. A free trial plan is available now. Visit our [website](https://www.devtools.nlnet.net) to learn more about it.
17+
18+
![img](img/layout.gif)
19+
20+
![img](img/tree.gif)
21+
22+
![img](img/preview.gif)
23+
24+
![img](img/property.gif)
25+
26+
## Using
27+
28+
1. Create a standalone project for iconfont resources.
29+
30+
2. Add the package `Nlnet.Sharp.Iconfont.Generator` from Nuget.
31+
32+
3. Download an iconfont from [https://www.iconfont.cn/](https://www.iconfont.cn/) and you can have the two files of iconfont.json (or other name like *.json) and iconfont.ttf (or other name like *.ttf). Put them into the resource project.
33+
34+
> Note that the *.ttf should be an Avalonia Resource.
35+
36+
4. Add the iconfont.json as AdditionalFiles and give it an IconName property.
37+
38+
```xml
39+
<ItemGroup>
40+
<AdditionalFiles Include="Assets\iconfont.json" IconName="Travel" />
41+
</ItemGroup>
42+
```
43+
44+
That's all. Build your solution and you will get a set of classes for Travel, including:
45+
46+
* `TravelExtention`. This is a MarkupExtension.
47+
48+
* `TravelChars`. This is a static class that has all glyph chars as fields.
49+
50+
* `TravelInfo`. This is a static class that contains information about the iconfont.
51+
52+
* `TravelKeys`. This is an enum that has all glyph names as values.
53+
54+
Also you have two more classes for all iconfonts:
55+
56+
* `IconFamilyExtension`. This is a MarkupExtension to set iconfont FontFamily.
57+
* `IconFamilyKeys`. This is an enum that contains all iconfont's names.
58+
59+
Some using examples:
60+
61+
```xaml
62+
<TextBlock Text="{x:Static TravelChars.See}" FontFamily="{IconFamily Travel}" />
63+
<TextBlock Text="{Travel Sea}" FontFamily="{IconFamily Travel}" />
64+
<TextBlock Text="{Travel Sea}" />
65+
<TextBlock Text="{Travel Sea, AutoSetFontFamily=true}" />
66+
<TextBlock Text="{Travel Sea, Prefix='This text is before the icon sea.', Suffix='This text is after the icon sea.'}" />
67+
```
68+
69+
## Options
70+
71+
We provide some MsBuild Properties to control the build-behaviors.
72+
73+
### IconNamespace
74+
75+
You can control the namespace of the classes related to the iconfont by setting IconNamespace. The default namespace of the project will be used if it is not set.
76+
77+
```xml
78+
<AdditionalFiles Include="Assets\iconfont.json" IconName="Travel"
79+
IconNamespace="Nlnet.Sharp.Icon.Travel" />
80+
```
81+
82+
### UseDefaultXmlnsPrefix
83+
84+
The xmlns prefix in xaml for the classes related to the iconfont can be saved by setting UseDefaultXmlnsPrefix to true.
85+
86+
```xml
87+
<AdditionalFiles Include="Assets\iconfont.json" IconName="Travel" IconNamespace="Nlnet.Sharp.Icon.Travel"
88+
UseDefaultXmlnsPrefix="true" />
89+
```
90+
91+
With this option being enabled, the namespace will be put into the avalonia's default XmlnsDefinition. Then you can use it like this:
92+
93+
```C#
94+
<TextBlock Text="{Travel Sea}" FontFamily="{IconFamily Travel}"/>
95+
```
96+
97+
### InjectFallbackFont
98+
99+
The FontFamily can be saved by setting InjectFallbackFont to true.
100+
101+
```xml
102+
<AdditionalFiles Include="Assets\iconfont.json" IconName="Travel"
103+
InjectFallbackFont="true" />
104+
```
105+
106+
With this option being enabled, the font family of the iconfont will be put into the FontManager to be a fallback FontFamily. Note that only one iconfont can be built with this option.
107+
108+
```xaml
109+
<TextBlock Text="{Travel Sea}" />
110+
```
111+
112+
### AutoSetFontFamily
113+
114+
The FontFamily can be saved by setting AutoSetFontFamily to true.
115+
116+
```xml
117+
<AdditionalFiles Include="Assets\iconfont.json" IconName="Travel"
118+
AutoSetFontFamily="true" />
119+
```
120+
121+
Then the value of the TravelExtension's property AutoSetFontFamily will be true by default. The FontFamily will be auto set when TravelExtension is used in markup.
122+
123+
```xaml
124+
<TextBlock Text="{Travel Sea}" />
125+
```
126+
127+
Also, without setting MsBuild property AutoSetFontFamily, you can just set the property when you use the TravelExtension like this:
128+
129+
```xaml
130+
<TextBlock Text="{Travel Sea, AutoSetFontFamily=True}" />
131+
```
132+
133+
### ConsiderIIconVisualWhenAutoSetFontFamily
134+
135+
If the AutoSetFontFamily is true, the iconfont FontFamily will be set to the control that used the TravelExtension, which means the whole text of the control will use the iconfont FontFamily. It is wrong.
136+
137+
```xaml
138+
<SomeControl SomeProperty="{Travel Sea, Suffix='Text after the icon.'}" FontFamily="{IconFamily Travel}" />
139+
```
140+
141+
The option ConsiderIIconVisualWhenAutoSetFontFamily provides a mechanism to do right thing. With that option, the TravelExtension will set `global:Nlnet.Sharp.IIconVisual.FontFamily` instead of the FontFamily of the Control.
142+
143+
So the SomeControl should implement the interface `IIconVisual`, which is:
144+
145+
```c#
146+
public interface IIconVisual
147+
{
148+
public string? Icon { get; set; }
149+
public double IconSize { get; set; }
150+
public IBrush? IconBrush { get; set; }
151+
public FontFamily? IconFamily { get; set; }
152+
}
153+
```
154+

img/DevTools.png

7.04 KB
Loading

img/layout.gif

3.35 MB
Loading

img/preview.gif

4.97 MB
Loading

img/property.gif

2.52 MB
Loading

img/tree.gif

4.69 MB
Loading

src/Nlnet.Sharp.Iconfont.Generator/IconfontGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ private static void BuildMarkup(IconfontContext ctx)
446446
if (ctx.ConsiderIIconVisualWhenAutoSetFontFamily)
447447
{
448448
magicIconLogical = $@"
449-
if (targetProvider?.TargetObject is global::Nlnet.Sharp.IMagicIconHost host)
449+
if (targetProvider?.TargetObject is global::Nlnet.Sharp.IIconVisual host)
450450
{{
451451
host.IconFamily = family;
452452
}}

src/Nlnet.Sharp.Magic.Icon/IIconVisual.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Nlnet.Sharp
44
{
5-
public interface IMagicIconHost
5+
public interface IIconVisual
66
{
77
public string? Icon { get; set; }
88

src/Nlnet.Sharp.Magic.Icon/IconTextBlock.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace Nlnet.Sharp
77
{
8-
public class IconTextBlock : TextBlock, IMagicIconHost
8+
public class IconTextBlock : TextBlock, IIconVisual
99
{
1010
public string? Icon
1111
{

0 commit comments

Comments
 (0)