欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

macOS-NSTextView显示图片

程序员文章站 2022-06-30 14:18:17
...

//NSTextView显示图片

NSTextAttachment *atacchment = [[NSTextAttachment alloc]init];
//用户的图片路径
atacchment.image = [NSImage imageNamed:@"a.png"] ;
//图片在textview中显示的大小
atacchment.bounds = CGRectMake(0, 0, 20, 20);
NSAttributedString *attributedStr = [[NSAttributedString alloc]initWithString:@""];
attributedStr = [NSAttributedString attributedStringWithAttachment:atacchment];
//textStorage装载内容的容器,继承自NSMutableAttributedString,其里面保存的东西决定了textview的富文本显示方式
[self.textView.textStorage appendAttributedString:attributedStr];