NSAttributedString中NSTextAttachment竖直居中

iOS开发中经常需要在富文本(NSAttributedString)中添加图片(NSTextAttachment)。那么如何添加图片呢?代码如下

UIFont *font = [UIFont systemFontOfSize:12];
NSDictionary<NSAttributedStringKey,id> *attributes = @{
    NSFontAttributeName: font,
};
//生成富文本
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:@"我忘记了" attributes:attributes];
//添加图片
[attributedText appendAttributedString:({
    NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
    UIImage *image = [UIImage imageNamed:@"icon"];
    attachment.image = image;
    attachment.bounds = CGRectMake(0, 0, image.size.width, image.size.height);
    [NSAttributedString attributedStringWithAttachment:attachment];
})];

代码写好了,编译运行,看看效果:

运行效果1

这肯定不是设计师需要的效果,如何让文字和图片在竖直方向上居中对齐呢?其实很简单,修改attachment.bounds.origin.y即可:

UIFont *font = [UIFont systemFontOfSize:12];
NSDictionary<NSAttributedStringKey,id> *attributes = @{
    NSFontAttributeName: font,
};
//生成富文本
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:@"我忘记了" attributes:attributes];
//添加图片
[attributedText appendAttributedString:({
    NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
    UIImage *image = [UIImage imageNamed:@"icon"];
    attachment.image = image;

    attachment.bounds = CGRectMake(0, -4.5, image.size.width, image.size.height);
    [NSAttributedString attributedStringWithAttachment:attachment];
})];

设置好attachment.bounds.origin.y了,再重新编译运行看看效果:

运行效果2

效果不错,达到了设计师需要的效果。那么问题来了,为何要设置attachment.bounds.origin.y = -4.5呢?我试了十遍试出来的

每次新添加或者切换字体都试上十遍,肯定不是我们想要的,那有没有更好的方式呢?答案肯定是有的,设置attachment.bounds.origin.y = round(font.capHeight - image.size.height)/2.0即可:

UIFont *font = [UIFont systemFontOfSize:12];
NSDictionary<NSAttributedStringKey,id> *attributes = @{
    NSFontAttributeName: font,
};
//生成富文本
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:@"我忘记了" attributes:attributes];
//添加图片
[attributedText appendAttributedString:({
    NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
    UIImage *image = [UIImage imageNamed:@"icon"];
    attachment.image = image;

    attachment.bounds = CGRectMake(0, round(font.capHeight - image.size.height)/2.0, image.size.width, image.size.height);
    [NSAttributedString attributedStringWithAttachment:attachment];
})];

再次编译运行得到我们想要的效果:


最终效果

参考:https://stackoverflow.com/questions/26105803/center-nstextattachment-image-next-to-single-line-uilabel
关于bound的设置可参考:https://www.jianshu.com/p/964313cfbdaa

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 用到的组件 1、通过CocoaPods安装 2、第三方类库安装 3、第三方服务 友盟社会化分享组件 友盟用户反馈 ...
    SunnyLeong阅读 14,736评论 1 180
  • iOS面试题目100道 1.线程和进程的区别。 进程是系统进行资源分配和调度的一个独立单位,线程是进程的一个实体,...
    有度YouDo阅读 30,018评论 8 137
  • 表情是什么,我认为表情就是表现出来的情绪。表情可以传达很多信息。高兴了当然就笑了,难过就哭了。两者是相互影响密不可...
    Persistenc_6aea阅读 126,264评论 2 7
  • 16宿命:用概率思维提高你的胜算 以前的我是风险厌恶者,不喜欢去冒险,但是人生放弃了冒险,也就放弃了无数的可能。 ...
    yichen大刀阅读 6,122评论 0 4