FontAwesomeKitはそれ自体に組み込まれてないアイコンフォントを使う場合にも役立つ

例えば、 https://github.com/erikflowers/weather-icons を使いたいって時の例。

  • weather-icons.cssweathericons-regular-webfont.ttf とかをリポジトリに入れておく。
  • コードジェネレータを準備(CodeGenerator.rb, gen.rb)
  • FAKWeatherIcons.fakgen.[h|m]を生成(ruby gen.rb)
$ tree WeatherIcons
WeatherIcons
├── FAKWeatherIcons.fakgen.h
├── FAKWeatherIcons.fakgen.m
├── code_generator.rb
├── gen.rb
├── weather-icons.css
└── weathericons-regular-webfont.ttf
  • weathericons-regular-webfont.ttfをリソースに追加
  • FAKIconを継承したTQWeatherIconsを作る
  • TQWeatherIconsFAKWeatherIcons.fakgen.[h|m] をコピペして、singleton生成のためのクラスメソッド書く。

※ コードジェネレータを準備のとこで、提供されてるCSSに合わせたgen.rbを適当に書く

require './CodeGenerator.rb' # https://github.com/PrideChung/FontAwesomeKit/blob/master/CodeGenerators/CodeGenerator.rb

names = []
codes = []

_lines = File.read("./weather-icons.css").lines

lines = []
_lines.each_with_index do |l, index|
  case l
  when /^\.wi-.+{/
    lines << l + _lines[index + 1] + _lines[index + 2]
  end
end

lines.each do |line|
  name = ''
  line.gsub(/(?<=wi-).*(?=:before)/i) { |match| name = match }
  nameParts = name.split('-')
  nameParts = nameParts.each_with_index.map do |p, i|
    if i < 1
      p
    else
      p = p.capitalize
    end
  end
  name = nameParts.join
  names.push name

  code = ''
  line.gsub(/".*"/) { |match| code = match[2..(match.length-2)] }
  codes.push "\\u#{code}"
end

generator = CodeGenerator.new('WeatherIcons', names, codes)
generator.generate

こういう風に元々FontAwesomeKitに入ってないフォントでも、UIImage化したりとかFAKIconが提供している便利機能を使えて良い。