Giter VIP home page Giter VIP logo

Comments (8)

beloso avatar beloso commented on September 26, 2024 1

from fontawesome.swift.

MatrixSenpai avatar MatrixSenpai commented on September 26, 2024

you still can. just make sure you're using the unicode properly

// make sure to replace the contents of the curly braces with the icon you would like to use
myLabel.text = "\u{f0ac}" 

from fontawesome.swift.

beloso avatar beloso commented on September 26, 2024

from fontawesome.swift.

MatrixSenpai avatar MatrixSenpai commented on September 26, 2024

Is there a particular reason you have to use unicode? The API has two features that may be more useful. Each FontAwesome icon has a rawValue: String property, that matches the CSS codes for each particular icon. The second is UIImage.fontAwesomeIcon(code: String, style: textColor: size:) that allows you to create a FontAwesome image from a CSS code. If you need to store and retrieve these codes, you can do something like this

let toStorage: String = icon.rawValue // icon being an instance of FontAwesome
store(toStorage, "identifier")
...
let fromStorage: String = retrieve("identifier")
let image = UIImage.fontAwesomeIcon(code: fromStorage, style: .regular, textColor: .black, size: CGSize(width: 10, height: 10))

from fontawesome.swift.

beloso avatar beloso commented on September 26, 2024

Well I am using a REST API that I don't own. They are sending the icon as a unicode char.
If you'd ask me I'd prefer using the CSS name. But in this case I don't have an option.

EDIT: To give some more context, they give me the unicode chars and I need to set them on TabBarItems. I was able to work around it by using the unicode chars on the TabBatItem title instead of the image. But that brings some problems with different screen scales. Being possible to go from Unicode char to UIImage would be great. The lack of this feature does not make the lib unusable to me. I just couldn't find a cleaner way to achieve this so far.

from fontawesome.swift.

MatrixSenpai avatar MatrixSenpai commented on September 26, 2024

Since this is coming from an API, this may warrant further investigation. However, for the time being, here's a solution that may work

let icon = resultFromApi.icon
// Here we will leverage the CaseIterable conformity of the FontAwesome enum
// Take note, if ONLY the unicode char is returned, and not the enclosing \u{}, you may need to
// interpolate the icon variable inside of a string, as in "\u{\(icon)}"
// Also note that in case the icon unicode is not found, you can provide a default. 
// The `first` property will be optional
let fa = FontAwesome.allCases.filter { $0.unicode == icon }.first ?? FontAwesome.question

from fontawesome.swift.

beloso avatar beloso commented on September 26, 2024

Thank you, your suggestion does help me out.

One thing though, it's not enough to do \u{\(code)}. The compiler cries.
I had to use something like this:

var fontAwesomeIconCode: String? {
    let num = Int(strtoul(iconCode, nil, 16))
    if let scalar = UnicodeScalar(num) {
        return String(scalar)
    }
    return .none
}

from fontawesome.swift.

MatrixSenpai avatar MatrixSenpai commented on September 26, 2024

I was assuming in this case that what's returned is in Char format (i.e. f290). In that case, to match it against something in the library as it stands, you'd have to use my format. If that's not the case, as it seems from what you're doing, you can either use your solution, which will only convert to String, or you can do something more elaborate like conversion to Char/String with interpolation, then filtering. Either way, this doesn't seem like a common problem, and I'm not sure I really want to tackle this problem just yet. It does seem to be solved though, so I'm closing this issue.
That being said, I am going to put something on the project board to investigate other ways to initialize.

from fontawesome.swift.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.