[Question] Aligning icons with the rest of an attributed string
cdzombak opened this issue · 1 comments
cdzombak commented
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.
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
Applying NSBaselineOffsetAttributeName
to the icon is the key.