■ FontImageSource 클래스를 사용해 IONICONS 폰트 아이콘을 표시하는 방법을 보여준다.
▶ FontModel.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 |
namespace TestProject; /// <summary> /// 폰트 모델 /// </summary> public class FontModel { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 아이콘 - Icon /// <summary> /// 아이콘 /// </summary> public ImageSource Icon { get; set; } #endregion #region 글리프 - Glyph /// <summary> /// 글리프 /// </summary> public string Glyph { get; set; } #endregion } |
▶ MainPage.xaml
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 |
<?xml version="1.0" encoding="utf-8" ?> <ContentPage x:Class="TestProject.MainPage" xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:TestProject"> <CollectionView x:Name="collectionView" Margin="10"> <CollectionView.ItemTemplate> <DataTemplate> <HorizontalStackLayout Margin="10"> <Image VerticalOptions="Center" WidthRequest="32" HeightRequest="32" Source="{Binding Icon}" /> <Label Margin="10,0,0,0" VerticalOptions="Center" Text="{Binding Glyph}" /> </HorizontalStackLayout> </DataTemplate> </CollectionView.ItemTemplate> </CollectionView> </ContentPage> |
▶ MainPage.xaml.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 |
using System.Globalization; using System.Text.RegularExpressions; namespace TestProject; /// <summary> /// 메인 페이지 /// </summary> public partial class MainPage : ContentPage { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainPage() /// <summary> /// 생성자 /// </summary> public MainPage() { InitializeComponent(); List<FontModel> list = new List<FontModel>(); for(int i = 0xf0ff ; i < 0xf3aa; i++) { string escapedUnicode = "\\u" + i.ToString("x4"); string nonEscapedUnicode = GetNonEscapedUnicodeString(escapedUnicode); FontImageSource fontImageSource = new FontImageSource(); fontImageSource.Color = Colors.Blue; fontImageSource.FontFamily = "ionicons"; fontImageSource.Glyph = nonEscapedUnicode; list.Add(new FontModel { Icon = fontImageSource, Glyph = escapedUnicode }); } this.collectionView.ItemsSource = list; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 비 이스케이프 유니코드 문자열 구하기 - GetNonEscapedUnicodeString(sourceEscapedUnicodeString) /// <summary> /// 비 이스케이프 유니코드 문자열 구하기 /// </summary> /// <param name="sourceEscapedUnicodeString">소스 이스케이프 유니코드 문자열</param> /// <returns>비 이스케이프 유니코드 문자열</returns> private string GetNonEscapedUnicodeString(string sourceEscapedUnicodeString) { return Regex.Replace ( sourceEscapedUnicodeString, @"\\u(?<Value>[a-zA-Z0-9]{4})", m => { return ((char)int.Parse(m.Groups["Value"].Value, NumberStyles.HexNumber)).ToString(); } ); } #endregion } |