Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/liwuqingxin/magic.icon

An icon class generator for iconfont in avalonia and wpf.
https://github.com/liwuqingxin/magic.icon

Last synced: 16 days ago
JSON representation

An icon class generator for iconfont in avalonia and wpf.

Awesome Lists containing this project

README

        

# About
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.

You can use it in avalonia (and wpf in future), in kinds of ways, just like this:
```xaml

```

## Promotion

![img](img/Brand.svg)

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.

## Using

1. Create a standalone project for iconfont resources.

2. Add the package `Nlnet.Sharp.Iconfont.Generator` from Nuget.

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.

> Note that the *.ttf should be an Avalonia Resource.

4. Add the iconfont.json as AdditionalFiles and give it an IconName property.

```xml



```

That's all. Build your solution and you will get a set of classes for Travel, including:

* `TravelExtention`. This is a MarkupExtension.

* `TravelChars`. This is a static class that has all glyph chars as fields.

* `TravelInfo`. This is a static class that contains information about the iconfont.

* `TravelKeys`. This is an enum that has all glyph names as values.

Also you have two more classes for all iconfonts:

* `IconFamilyExtension`. This is a MarkupExtension to set iconfont FontFamily.
* `IconFamilyKeys`. This is an enum that contains all iconfont's names.

Some using examples:

```xaml

```

## Options

We provide some MsBuild Properties to control the build-behaviors.

### IconNamespace

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.

```xml

```

### UseDefaultXmlnsPrefix

The xmlns prefix in xaml for the classes related to the iconfont can be saved by setting UseDefaultXmlnsPrefix to true.

```xml

```

With this option being enabled, the namespace will be put into the avalonia's default XmlnsDefinition. Then you can use it like this:

```C#

```

### InjectFallbackFont

The FontFamily can be saved by setting InjectFallbackFont to true.

```xml

```

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.

```xaml

```

### AutoSetFontFamily

The FontFamily can be saved by setting AutoSetFontFamily to true.

```xml

```

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.

```xaml

```

Also, without setting MsBuild property AutoSetFontFamily, you can just set the property when you use the TravelExtension like this:

```xaml

```

### ConsiderIIconVisualWhenAutoSetFontFamily

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.

```xaml

```

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.

So the SomeControl should implement the interface `IIconVisual`, which is:

```c#
public interface IIconVisual
{
public string? Icon { get; set; }
public double IconSize { get; set; }
public IBrush? IconBrush { get; set; }
public FontFamily? IconFamily { get; set; }
}
```