gpt4 book ai didi

iOS 7 TextKit - 如何插入与文本内联的图像?

转载 作者:IT王子 更新时间:2023-10-29 07:28:59 25 4
gpt4 key购买 nike

我正在尝试使用 UITextView 获得以下效果:

enter image description here

基本上我想在文本之间插入图像。图片只需占用 1 行空间,因此无需换行。

我试着只添加一个 UIView 到 subview :

UIView *pictureView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 25, 25)];
[pictureView setBackgroundColor:[UIColor redColor]];
[self.textView addSubview:pictureView];

但它似乎漂浮在文字上并覆盖了它。

我阅读了一些关于排除路径 的文章,这似乎是实现这一点的一种方式。但是,我不想绝对定位图像 - 相反,它应该与文本一起流动(类似于 <span> 在 HTML 中的行为方式)。

最佳答案

您需要使用属性字符串并将图像添加为 NSTextAttachment 的实例:

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"like after"];

NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
textAttachment.image = [UIImage imageNamed:@"whatever.png"];

NSAttributedString *attrStringWithImage = [NSAttributedString attributedStringWithAttachment:textAttachment];

[attributedString replaceCharactersInRange:NSMakeRange(4, 1) withAttributedString:attrStringWithImage];

关于iOS 7 TextKit - 如何插入与文本内联的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20930462/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com