Vaberer / Font-Awesome-Swift

Font Awesome swift library for iOS.
MIT License
743 stars 125 forks source link

Bottom part of text cut off #20

Closed squeeish closed 8 years ago

squeeish commented 8 years ago

I'm new to iOS programming. I've noticed that if I have a prefix containing the letters "g", "p", etc, the bottom part of the text is cut off when I use font awesome icons.

Imgur

Is there a workaround to that? That's Swiftybutton by the way.

wherewelive commented 8 years ago

FYI, had the same issue and you can fix it using NSAttributedString and setAttributedTitle for buttons or labels. Here is how to split the icon and text up into an NSAttributedString

let iconPart = NSMutableAttributedString(string: "\(String.fontAwesomeIconWithName(.User))", attributes: [NSFontAttributeName:UIFont.fontAwesomeOfSize(12)])

let textPart = NSMutableAttributedString(string: " whatever your label is here", attributes: [NSFontAttributeName:UIFont.systemFontOfSize(12)])

Then you need to append the textPart to the iconPart (notice the space left in the textPart string)

iconPart.appendAttributedString(textPart)

iconPart is now an NSAttributedString with 2 font types that will not cutoff the text :)

screen shot 2016-02-17 at 2 12 25 pm
RanjitKadam commented 8 years ago

hey I am also facing the same issue, but this lib, does not contain "fontAwesomeIconWithName"

wherewelive commented 8 years ago

I was using a different lib (https://github.com/thii/FontAwesome.swift) but idea would be the same here since the icon is just a string

Vaberer commented 8 years ago

Thank you guys for a hint how to fix it. New version of Font Awesome works great!

cschar commented 6 years ago

If anyones wondering how to get that Attributed String into a BarButton Item (which doesnt accept multi attributed strings....) You can override the customView with a label and set it from there:

        let iconPart = NSMutableAttributedString(string: "\(String.fontAwesomeIcon( name: .videoCamera))", attributes: [NSAttributedStringKey.font:UIFont.fontAwesome(ofSize: 24)])

        let textPart = NSMutableAttributedString(string: "Video", attributes: [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 16)])

        iconPart.append(textPart)
        let label = UILabel()
        label.attributedText = iconPart
        label.sizeToFit()
        label.textColor = self.view.tintColor

        let tap = UITapGestureRecognizer(target: self, action: #selector(MyViewController.tapFunction))
        label.isUserInteractionEnabled = true
        label.addGestureRecognizer(tap)
        MyBarButton.customView = label

//elsewhere in your view Controller:
    @objc
    func tapFunction(sender:UITapGestureRecognizer) {
        print("tap working")
    }