iOS 7的TextKit – 如何插入图像内嵌文本?
我想使用UITextView获得以下效果:
基本上我想插入文字之间的图像。 图像可以简单地占用1行的空间,所以没有必要的包装。
我试着只是添加一个UIView的子视图:
UIView *pictureView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 25, 25)]; [pictureView setBackgroundColor:[UIColor redColor]]; [self.textView addSubview:pictureView];
但它似乎浮出了文本,并覆盖它。
我在排除path上做了一些阅读,这似乎是实现这一点的一种方式。 但是,我不想绝对定位图像 – 相反,它应该与文本一样(类似于<span>
在HTML中的行为)。
你将需要使用一个属性string,并添加图像作为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];
你可以尝试使用NSAttributedString和NSTextAttachment。 看看下面的链接,了解更多关于自定义NSTextAttachment的细节,以便调整图像大小。 http://ossh.com.au/design-and-technology/software-development/implementing-rich-text-with-images-on-os-x-and-ios/
在我的例子中,我调整图像的大小以适应宽度,在您的情况下,您可能需要调整图像的大小以匹配行高。
@ bilobatum的代码转换为Swift为有需要的人:
var attributedString = NSMutableAttributedString(string: "like after") var textAttachment = NSTextAttachment() textAttachment.image = UIImage(named: "whatever.png") var attrStringWithImage = NSAttributedString.attributedStringWithAttachment(textAttachment) attributedString.replaceCharactersInRange(NSMakeRange(4, 1), withAttributedString: attrStringWithImage)
扩展@ bilobatum的答案 ,并从另一个问题使用这个类别 。 我做了这个:
用法:
UILabel *labelWithImage = [UILabel new]; labelWithImage.text = @"Tap [new-button] to make a new thing!"; NSAttributedString *stringWithImage = [labelWithImage.attributedText attributedStringByReplacingOccurancesOfString:@"[new-button]" withImage:[UIImage imageNamed:@"MyNewThingButtonImage"] scale:0]; labelWithImage.attributedText = stringWithImage;
执行:
@interface NSMutableAttributedString (InlineImage) - (void)replaceCharactersInRange:(NSRange)range withInlineImage:(UIImage *)inlineImage scale:(CGFloat)inlineImageScale; @end @interface NSAttributedString (InlineImages) - (NSAttributedString *)attributedStringByReplacingOccurancesOfString:(NSString *)string withInlineImage:(UIImage *)inlineImage scale:(CGFloat)inlineImageScale; @end
。
@implementation NSMutableAttributedString (InlineImages) - (void)replaceCharactersInRange:(NSRange)range withInlineImage:(UIImage *)inlineImage scale:(CGFloat)inlineImageScale { if (floorf(inlineImageScale) == 0) inlineImageScale = 1.0f; // Create resized, tinted image matching font size and (text) color UIImage *imageMatchingFont = [inlineImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; { // Font size NSDictionary *attributesForRange = [self attributesAtIndex:range.location effectiveRange:nil]; UIFont *fontForRange = [attributesForRange valueForKey:NSFontAttributeName]; CGSize imageSizeMatchingFontSize = CGSizeMake(inlineImage.size.width * (fontForRange.capHeight / inlineImage.size.height), fontForRange.capHeight); // Some scaling for prettiness CGFloat defaultScale = 1.4f; imageSizeMatchingFontSize = CGSizeMake(imageSizeMatchingFontSize.width * defaultScale, imageSizeMatchingFontSize.height * defaultScale); imageSizeMatchingFontSize = CGSizeMake(imageSizeMatchingFontSize.width * inlineImageScale, imageSizeMatchingFontSize.height * inlineImageScale); imageSizeMatchingFontSize = CGSizeMake(ceilf(imageSizeMatchingFontSize.width), ceilf(imageSizeMatchingFontSize.height)); // Text color UIColor *textColorForRange = [attributesForRange valueForKey:NSForegroundColorAttributeName]; // Make the matching image UIGraphicsBeginImageContextWithOptions(imageSizeMatchingFontSize, NO, 0.0f); [textColorForRange set]; [inlineImage drawInRect:CGRectMake(0 , 0, imageSizeMatchingFontSize.width, imageSizeMatchingFontSize.height)]; imageMatchingFont = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); } // Text attachment with image NSTextAttachment *textAttachment = [NSTextAttachment new]; textAttachment.image = imageMatchingFont; NSAttributedString *imageString = [NSAttributedString attributedStringWithAttachment:textAttachment]; [self replaceCharactersInRange:range withAttributedString:imageString]; } @end @implementation NSAttributedString (InlineImages) - (NSAttributedString *)attributedStringByReplacingOccurancesOfString:(NSString *)string withInlineImage:(UIImage *)inlineImage scale:(CGFloat)inlineImageScale { NSMutableAttributedString *attributedStringWithImages = [self mutableCopy]; [attributedStringWithImages.string enumerateOccurancesOfString:string usingBlock:^(NSRange substringRange, BOOL *stop) { [attributedStringWithImages replaceCharactersInRange:substringRange withInlineImage:inlineImage scale:inlineImageScale]; }]; return [attributedStringWithImages copy]; } @end