Center NSTextAttachment image next to single line UILabel

You can use the capHeight of the font. Objective-C NSTextAttachment *icon = [[NSTextAttachment alloc] init]; UIImage *iconImage = [UIImage imageNamed:@”icon.png”]; [icon setBounds:CGRectMake(0, roundf(titleFont.capHeight – iconImage.size.height)/2.f, iconImage.size.width, iconImage.size.height)]; [icon setImage:iconImage]; NSAttributedString *iconString = [NSAttributedString attributedStringWithAttachment:icon]; [titleText appendAttributedString:iconString]; Swift let iconImage = UIImage(named: “icon.png”)! var icon = NSTextAttachment() icon.bounds = CGRect(x: 0, y: (titleFont.capHeight – iconImage.size.height).rounded() / … Read more

tech