Use your Icomoon fonts with Swift - auto-generates type safe enums for each icon
The generated framework extends UIFont
, UIImage
and String
and generates an enum Icon
. Cases are automatically created based on the name on Icomoon. (Example: my-search
becomes .MySearch
)
import Icomoon
//UILabel
searchIcon.text = String.iconWithName(.MySearch)
searchIcon.font = UIFont.iconOfSize(30)
//UIButton
buttonChat.titleLabel?.font = UIFont.iconOfSize(30)
buttonChat.setTitle(String.iconWithName(.Chat), for: .normal)
//UIImage
myImage.image = UIImage.icomoonIcon(name: .Chat, textColor: UIColor.black, size: CGSize(width:myImage.frame.width, height:myImage.frame.height))
- Download or clone this repo.
- Run
make
. That's it. (You can runmake uninstall
to uninstall.)
- Download your font file from Icomoon (usally called
icomoon.zip
) - Run
icomoon-swift icomoon.zip
- Add the generated
Icomoon.framework
to your Xcode project by following the import instructions below.
Copy to root of Project
Project -> Target -> General -> Linked Frameworks and Libraries
Add Icomoon.framework
Project -> Target -> Build Phases
Add a New Section ("New Copy Files Phase"), titled "Embed Frameworks" (if not exist)
Add Icomoon.framework
In order to submit to App Store, you'll need to automatically strip unsupported architectures (i386 simulator). Add this "Run Script" to you Build Phases, AFTER Embed Frameworks, or as the final entry.
APP_PATH="${TARGET_BUILD_DIR}/${WRAPPER_NAME}"
# This script loops through the frameworks embedded in the application and
# removes unused architectures.
find "$APP_PATH" -name '*.framework' -type d | while read -r FRAMEWORK
do
FRAMEWORK_EXECUTABLE_NAME=$(defaults read "$FRAMEWORK/Info.plist" CFBundleExecutable)
FRAMEWORK_EXECUTABLE_PATH="$FRAMEWORK/$FRAMEWORK_EXECUTABLE_NAME"
echo "Executable is $FRAMEWORK_EXECUTABLE_PATH"
EXTRACTED_ARCHS=()
for ARCH in $ARCHS
do
echo "Extracting $ARCH from $FRAMEWORK_EXECUTABLE_NAME"
lipo -extract "$ARCH" "$FRAMEWORK_EXECUTABLE_PATH" -o "$FRAMEWORK_EXECUTABLE_PATH-$ARCH"
EXTRACTED_ARCHS+=("$FRAMEWORK_EXECUTABLE_PATH-$ARCH")
done
echo "Merging extracted architectures: ${ARCHS}"
lipo -o "$FRAMEWORK_EXECUTABLE_PATH-merged" -create "${EXTRACTED_ARCHS[@]}"
rm "${EXTRACTED_ARCHS[@]}"
echo "Replacing original executable with thinned version"
rm "$FRAMEWORK_EXECUTABLE_PATH"
mv "$FRAMEWORK_EXECUTABLE_PATH-merged" "$FRAMEWORK_EXECUTABLE_PATH"
done