6

我在 UITextView 上试过它可以工作。但是当我使用 CATextLayer 时图像留空。

有人试过吗?

这是我在 UIView drawRect 中尝试的代码

NSTextAttachment* attachment = [NSTextAttachment new];
attachment.image = [UIImage imageNamed:@"rainbow.png"];
NSAttributedString* imageString = [NSAttributedString attributedStringWithAttachment:attachment];
NSDictionary * attribute = @{NSFontAttributeName: [UIFont fontWithName:@"Helvetica" size:12]};
NSMutableAttributedString *speechString = [[NSMutableAttributedString alloc] initWithString: @"hi" attributes:attribute];
[speechString insertAttributedString:imageString atIndex:1];
[speechString appendAttributedString:imageString];

然后添加到 CATextLayer

CATextLayer * speechTextLayer = [CATextLayer layer];
[speechTextLayer setString:speechString];
[speechTextLayer setFrame:self.bounds];
[speechTextLayer setBackgroundColor:[UIColor whiteColor].CGColor];
[self.layer addSublayer:speechTextLayer];
4

0 回答 0