-
Notifications
You must be signed in to change notification settings - Fork 139
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
- custom iconFont regist #46
Conversation
### regist your iconFont ```swift // custom iconFont import LGButton public enum MyIconFonts: IconFont { case iconFont public var fontName: String { switch self { case .iconFont: return "iconfont"// the ttf name } } public var icons: [String: String] { switch self { case .iconFont: return ["icon_star": "\u{e620}"] } } } // regist SwiftIconFont.registFont(from: MyIconFonts.iconFont, name: MyIconFonts.iconFont.fontName) // useage btn.leftIconFontName = MyIconFonts.iconFont.fontName btn.leftIconString = "icon_star" ```
@loregr thank you |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the great work on this. I've added some comments that I'd like to see fixed before merging your PR. Most of them are just styling stuff, but there's line LGButton.swift:536
that absolutely needs to be fixed, otherwise, the lib won't work correctly.
Thanks again for the time spent on this! 👍
LGButton/Classes/LGButton.swift
Outdated
break; | ||
} | ||
icon.font = UIFont.icon(from: iconFont, ofSize: fontSize) | ||
icon.text = String.getIcon(from: iconFont, code: iconName.replacingOccurrences(of: "-", with: ".")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
to set icon.text
we need to keep the switch-case statement because some icon identifiers are slightly different from the ones used in the cheat sheet. Some use .
other use _
. If you try to use a FontAwesome icon like bar-chart-o
with this code, it simply won't work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
like this?
fileprivate func setupIcon(icon:UILabel, fontName:String, iconName:String, fontSize:CGFloat, color:UIColor){
defer {
setupBorderAndCorners()
}
if let iconFont = SwiftIconFont.fonts[fontName] {
icon.isHidden = false
icon.textColor = color
icon.font = UIFont.icon(from: iconFont, ofSize: fontSize)
if let iconStr = String.getIcon(from: iconFont, code: iconName) {
icon.text = iconStr
}else{
let joind = ["-", ".", "_"]
for left in joind {
for right in joind {
if left == right { continue }
let code = left.replacingOccurrences(of: left, with: right)
if let iconStr = String.getIcon(from: iconFont, code: code) {
icon.text = iconStr
return
}
}
}
}
}else{
icon.isHidden = true
}
}
1. keep only 4.2 version 2. remove unused code 3. icon font code split with ["-", "_", "."]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Much better, I spotted some issues while testing your code and added suggestions. I think the PR should be ready to merge once fixed. Thanks
case MapIcon// = "map-icons" | ||
case MaterialIcon// = "MaterialIcons-Regular" | ||
|
||
public var fontName: String { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With your new implementation, here you should return the name of the .ttf
files. So it should be:
public var fontName: String {
switch self {
case .FontAwesome:
return "FontAwesome"
case .Iconic:
return "open-iconic"
case .Ionicon:
return "Ionicons"
case .Octicon:
return "octicons"
case .Themify:
return "themify"
case .MapIcon:
return "map-icons"
case .MaterialIcon:
return "MaterialIcons-Regular"
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sorry, there have some problems, if the ttf file name is not the font name.
I will rewrite there. there need fontName
and fileName
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We probably don't need fontName
to be something different from the file name, by doing what I suggested everything works just fine. But if you prefer to polish or change some stuff feel free to do that. 👍
Co-Authored-By: inft <goo.gle@foxmail.com>
Co-Authored-By: inft <goo.gle@foxmail.com>
1. rename iconfont: join fileName 2.generate iconfont keys enum 3.output the ttf's font name
Thanks @INFT for the contribution. There's still some styling that I would like to improve, but I'll do that once merged your PR |
regist your iconFont