NSTextAttachment不会显示在CATextLayer上

时间:2014-03-06 10:43:43

标签: ios objective-c calayer nstextattachment

我在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];

0 个答案:

没有答案