iOS 7 TextKit - 如何使用文本内嵌插入图像?

And*_*Hin 104 ios ios7 textkit

我试图使用UITextView获得以下效果:

在此输入图像描述

基本上我想在文本之间插入图像.图像可以简单地占用1行空间,因此不需要包装.

我尝试在子视图中添加一个UIView:

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

但它似乎漂浮在文本上并覆盖它.

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

bil*_*tum 175

您需要使用属性字符串并将图像添加为以下实例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];
Run Code Online (Sandbox Code Playgroud)

  • 我如何调整NSTextAttachment的大小? (6认同)
  • 我认为这是迄今为止最接近的答案.是否可以使用UIView而不是UIImage使用相同的技术? (4认同)
  • 在UITextView中使用结果字符串时,这不显示图像 (4认同)
  • `[attributionString insertAttributedString:textAttachment atIndex:4]`比`replaceCharactersInRange更好`` (3认同)
  • @bilobatum,我想在textview中添加多个图像.那我怎么加? (2认同)
  • @jsetting32 `textAttachment.bounds = CGRectMake(0, 0, tAWidth, tAHeight)` 来调整 NSTextAttachment 的大小 (2认同)

Jus*_*ely 25

@ bilobatum的代码转换为Swift,供有需要的人使用:

let attributedString = NSMutableAttributedString(string: "like after")

let textAttachment = NSTextAttachment()

textAttachment.image = UIImage(named: "whatever.png")

let attrStringWithImage = NSAttributedString(attachment: textAttachment)

attributedString.replaceCharacters(in: NSMakeRange(4, 1), with: attrStringWithImage)
Run Code Online (Sandbox Code Playgroud)


Dun*_*ald 20

您可以尝试使用NSAttributedString和NSTextAttachment.有关自定义NSTextAttachment以调整图像大小的更多详细信息,请查看以下链接. http://ossh.com.au/design-and-technology/software-development/implementing-rich-text-with-images-on-os-x-and-ios/

在我的示例中,我调整图像大小以适应宽度,在您的情况下,您可能需要调整图像大小以匹配行高.


Sti*_*and 5

扩展@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;
Run Code Online (Sandbox Code Playgroud)

执行:

@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
Run Code Online (Sandbox Code Playgroud)

.

@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
Run Code Online (Sandbox Code Playgroud)


Pra*_*era 5

简单示例中的问题解决方案是 在此处输入图片说明

let attachment = NSTextAttachment()
attachment.image = UIImage(named: "qrcode")

let iconString = NSAttributedString(attachment: attachment)
let firstString = NSMutableAttributedString(string: "scan the ")
let secondString = NSAttributedString(string: "QR code received on your phone.")

firstString.append(iconString)
firstString.append(secondString)

self.textLabel.attributedText = firstString
Run Code Online (Sandbox Code Playgroud)


归档时间:

查看次数:

55422 次

最近记录:

6 年,7 月 前