FontsUtil.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. 
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Windows;
  6. using System.Windows.Media;
  7. namespace FWindSoft.Wpf
  8. {
  9. /// <summary>
  10. /// wpf下字体相关命令
  11. /// </summary>
  12. public class FontsUtil
  13. {//zh-cn
  14. public static List<string> FontNames { get; private set; }
  15. static FontsUtil()
  16. {
  17. FontNames = GetAllFontNames();
  18. //FontFamily不能加载我安装的字体,通过样式却可以
  19. //try
  20. //{
  21. // Awesome = new FontFamily(new Uri("pack://application:,,,/FWindSoft.Wpf;.component/Resources"), "FontAwesome");
  22. //}
  23. //catch (Exception ex)
  24. //{
  25. // throw;
  26. //}
  27. AwesomeKey= new ComponentResourceKey(typeof(FontsUtil), "FontAwesome");
  28. }
  29. private static List<string> GetAllFontNames()
  30. {
  31. List<string> names=new List<string>();
  32. var collection = Fonts.SystemFontFamilies;
  33. foreach (var fontFamily in collection)
  34. {
  35. var tempFontName = fontFamily.FamilyNames;
  36. var chName = tempFontName.FirstOrDefault(c => c.Key.IetfLanguageTag == "zh-cn");
  37. if (chName.Key == null)
  38. {
  39. chName = tempFontName.FirstOrDefault();
  40. }
  41. if (chName.Key == null)
  42. continue;
  43. names.Add(chName.Value);
  44. }
  45. return names;
  46. }
  47. public static ResourceKey AwesomeKey { get; private set; }
  48. //public static FontFamily Awesome { get; set; }
  49. }
  50. }