我刚刚尝试使用本机NSAtrributedstring和NStextAttachment在textview中添加图像,从本文here获得一些帮助
但是,我无法做到。我正在使用nativescript-mediafilepicker库从照片库添加图像,然后使用其内置方法之一将PH图像转换为UIImage。但是textview不会随图像更新。但是,我可以通过NSattributedstring而不是image添加更多的字符串。
这是我的代码。
//creating and initilizing new NSMutableAttributedString
var attributedString = NSMutableAttributedString.alloc().initWithString(textview.ios.text);
textview.ios.attributedText = attributedString;
//value is an UIImage object what the convertPHImageToUIImage method returns
var image = value;
console.log(image);
//the above log prints<UIImage: 0x2817ac4d0> size {4032, 3024} orientation 0 scale 1.000000
let oldWidth = image.size.width;
// console.log(oldWidth);
let scaleFactor = oldWidth / (textview.ios.frame.size.width - 10);
//console.log(scaleFactor);
let orientation="up";
//create NStextAttachment
let textAttachment = NSTextAttachment.alloc().init();
//adding UIImage object to NSTextAttachment
textAttachment.image = UIImage.imageWithCGImageScaleOrientation(image.CGImage ,scaleFactor , orientation)
// console.dir(textAttachment);
//creating a new NSAttributedString
let attrStringWithImage = NSAttributedString.alloc().init();
//console.dir(attrStringWithImage);
attrStringWithImage.attachment = textAttachment;
console.dir(attrStringWithImage)
//appenind the NSAttributedString to the mutable string..
attributedString.appendAttributedString(attrStringWithImage);
//console.log(attributedString.containsAttachmentsInRange(textview.ios.selectedRange));
textview.ios.attributedText = attributedString;
//textview.ios.textStorage.insertAttributedStringAtIndex(attrStringWithImage,textview.ios.selectedRange.location)
//this doesn't work either
答案 0 :(得分:1)
如果您使用的是TypeScript,请安装tns-platform-declarations,这将使您在访问本机api时的生活更加轻松。
UIImage.imageWithCGImageScaleOrientation(cgImage, scale, orientation);
此docs将帮助您了解将Objective C转换为JavaScript / TypeScript的情况。