ios - Swift:如何在字符串的特定索引处插入 UIImage?

标签 ios swift uiimageview uiimage

我正在尝试将一个 UIImageView 插入到一个 textView 中。它可以工作,但是我的图像的位置总是在 textView 的一角。 这是我的代码:

if(parseur.textStrings.containsString(SymboleList[i])){
   let image = UIImageView(image: UIImage(named: imagesSymboleList[i]))
   let path = UIBezierPath(rect: CGRectMake(0,0, image.frame.width,       image.frame.height))
   parseur.textStrings.stringByReplacingOccurrencesOfString(SymboleList[i], withString: "")             
   boiteTexte.textContainer.exclusionPaths = [path]
   boiteTexte.addSubview(image)
}

SymboleList 是一个列表,其中包含我想用图像替换的字符串(图像在 imagesSymboleList 中) 我必须找到要替换的字符串的位置,然后在该索引处插入我的图像。 我该怎么做?

最佳答案

下面是如何实现类似表情符号的图像插入

@IBOutlet var textView: UITextView!

var SymboleList: [String] = ["ipsum", "incididunt", "Excepteur"];
var imagesSymboleList: [String] = ["img.png", "img.png", "img.png"]

override func viewDidLoad() {
    super.viewDidLoad()
    self.insertImages();
}

func insertImages() {

    let attrString: NSMutableAttributedString = NSMutableAttributedString(attributedString: textView.attributedText)
    let style = NSMutableParagraphStyle()
    style.maximumLineHeight = (textView.font?.lineHeight)!
    attrString.addAttribute(
        NSParagraphStyleAttributeName, value: style, range: NSMakeRange(0, attrString.length))

    for var i = 0; i < SymboleList.count; i++ {

        // get attachment image
        var image = UIImage(named: imagesSymboleList[i])
        let scaleFactor = (textView.font?.lineHeight)! / (image?.size.height)!
        let newSize = CGSizeMake((image?.size.width)! * scaleFactor, (image?.size.height)! * scaleFactor)
        image = self.scaleImage(image!, newSize: newSize)
        let attachment: NSTextAttachment = NSTextAttachment()
        attachment.image = image;

        // build attachment string
        let attachmentString: NSMutableAttributedString = NSMutableAttributedString(attributedString: NSAttributedString(attachment: attachment))
        attachmentString.addAttribute(
            NSBaselineOffsetAttributeName,
            value: -(textView.font?.lineHeight)! / 4,
            range: NSMakeRange(0, attachmentString.length))

        // replace text with images
        let range = (attrString.string as NSString).rangeOfString(SymboleList[i])
        attrString.replaceCharactersInRange(range, withAttributedString: attachmentString)

    }

    textView.attributedText = attrString

}

func scaleImage(image: UIImage, newSize: CGSize) -> UIImage {
    //UIGraphicsBeginImageContext(newSize);
    // In next line, pass 0.0 to use the current device's pixel scaling factor (and thus account for Retina resolution).
    // Pass 1.0 to force exact pixel size.
    UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0);
    image.drawInRect(CGRectMake(0, 0, newSize.width, newSize.height))
    let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

结果:
Result

关于ios - Swift:如何在字符串的特定索引处插入 UIImage?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36249312/

相关文章:

ios - 如何使类中的初始化方法等待 Firestore 在 Swift 4 中完成?

ios - UIImageView.图像 setter

iphone - 使用 Quartz 时如何去掉 UIView 的锯齿?

swift - TableViewController 的第二个 segue 创建与第一个相关的 SIGABRT 错误

swift - XMPPFramework (Swift) 问题

objective-c - 如何让 imageview 做一个 segue

iphone - UITapGestureRecognizer 不会在以编程方式创建的 UIImageView 上触发

ios - Realm 迁移枚举无法执行

iphone - 使用整数范围填充选择器 View 的最有效方法?

ios - 子类 PFObject 并覆盖 init