PrideChung / FontAwesomeKit

Icon font library for iOS. Currently supports Font-Awesome, Foundation icons, Zocial, and ionicons.
MIT License
2.81k stars 308 forks source link

[Question] Aligning icons with the rest of an attributed string #54

Closed cdzombak closed 9 years ago

cdzombak commented 9 years ago

As seen in the attached image, I am trying to use FontAwesomeKit to display icons aligned with text on a button. For simplicity, I'd love to be able to do this with an attributed string rather than an image and string.

But I can't seem to figure out how to get the icons to vertically center-align with the text. I've tried a few different things, but they all seem to affect either nothing or the entire icon + text.

img_1058

My code follows…

FAKIcon *icon = [FAKIonIcons socialFacebookIconWithSize:20.f];
NSString *trailingTitle = NSLocalizedString(@"  Facebook", nil);
UIFont *font = [AppStyle avenirLightFontOfSize:16.f];

[icon addAttribute:NSForegroundColorAttributeName value:foregroundColor];
NSMutableAttributedString *buttonTitle = [icon.attributedString mutableCopy];
[buttonTitle appendAttributedString:[[NSAttributedString alloc] initWithString:trailingTitle
    attributes:@{
        NSForegroundColorAttributeName: foregroundColor,
        NSFontAttributeName: font,
    }]
];
[self.facebookButton setAttributedTitle:[buttonTitle copy] forState:UIControlStateNormal];

I suspect I am missing some NSAttributedString feature; any helpful pointers would be appreciated.

cdzombak commented 9 years ago

Applying NSBaselineOffsetAttributeName to the icon is the key.