Skip to content
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

Merged
merged 5 commits into from
Dec 22, 2018
Merged

- custom iconFont regist #46

merged 5 commits into from
Dec 22, 2018

Conversation

cdoky
Copy link
Contributor

@cdoky cdoky commented Dec 18, 2018

regist your iconFont

// 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"

### 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"
```
@cdoky
Copy link
Contributor Author

cdoky commented Dec 18, 2018

@loregr thank you

Copy link
Owner

@loregr loregr left a 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 Show resolved Hide resolved
LGButton/Classes/LGButton.swift Outdated Show resolved Hide resolved
LGButton/Classes/LGButton.swift Outdated Show resolved Hide resolved
break;
}
icon.font = UIFont.icon(from: iconFont, ofSize: fontSize)
icon.text = String.getIcon(from: iconFont, code: iconName.replacingOccurrences(of: "-", with: "."))
Copy link
Owner

@loregr loregr Dec 18, 2018

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.

Copy link
Contributor Author

@cdoky cdoky Dec 19, 2018

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
        }
    }

LGButton/Classes/LGButton.swift Outdated Show resolved Hide resolved
LGButton/Classes/SwiftIconFont/SwiftIconFont.swift Outdated Show resolved Hide resolved
LGButton/Classes/SwiftIconFont/SwiftIconFont.swift Outdated Show resolved Hide resolved
1. keep only 4.2 version
2. remove unused code
3. icon font code split with ["-", "_", "."]
Copy link
Owner

@loregr loregr left a 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

LGButton/Classes/LGButton.swift Outdated Show resolved Hide resolved
LGButton/Classes/LGButton.swift Outdated Show resolved Hide resolved
case MapIcon// = "map-icons"
case MaterialIcon// = "MaterialIcons-Regular"

public var fontName: String {
Copy link
Owner

@loregr loregr Dec 19, 2018

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"
        }
    }

Copy link
Contributor Author

@cdoky cdoky Dec 19, 2018

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

Copy link
Owner

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. 👍

loregr and others added 3 commits December 19, 2018 19:18
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
@loregr
Copy link
Owner

loregr commented Dec 22, 2018

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

@loregr loregr merged commit bf0053b into loregr:develop Dec 22, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants