文字横に画像
試行錯誤の結果次のようになった。
+ (NSAttributedString *)backSymbol { UIImage *image = [UIImage imageNamed:@"backbutton"]; NSTextAttachment *attachment = [[NSTextAttachment alloc] init]; attachment.image = image; NSMutableAttributedString *ret = [[NSMutableAttributedString alloc] init]; UIFont *font = [UIFont systemFontOfSize:17.0]; CGSize size = CGSizeMake(font.lineHeight * image.size.width/image.size.height, font.lineHeight); CGFloat y = round(font.capHeight - size.height)/2; [attachment setBounds:CGRectMake(-size.width*1/4, y, size.width, size.height)]; NSAttributedString *back = [NSMutableAttributedString attributedStringWithAttachment:attachment]; [ret appendAttributedString:back]; // NSMutableAttributedString *back = [[NSMutableAttributedString alloc] initWithString:@"<"]; [ret addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:21.0] range:NSMakeRange(0,back.length)]; return ret; }
参考
Swift5 UILabelに画像(UIImage)を表示する NSAttributedString NSTextAttachment - Qiita