2016-07-20 9 views
2

앱을 사용 중입니다.하지만 문제가 발생했습니다.UITextView에서 이미지를 추가하려면 어떻게해야합니까?

에 이미지를 넣고 싶습니다. UITextView. 나는 UITextView의 이미지를 넣어 NSAttributedString은을 사용하지만 난 imagepicker에서 이미지를 넣을 때, 이미지의 크기가 나는 사과처럼 만들고 싶어이

image

같은 너무 큰 응용 프로그램

노트

image2

어떻게 나타낼 수 있습니까?

let images = selectedImage 
    let attachment = NSTextAttachment() 

    attachment.image = images 

    let attString = NSAttributedString(attachment: attachment) 

    textView.textStorage.insertAttributedString(attString, atIndex: textView.selectedRange.location) 

답변

2

let textView = UITextView(frame: CGRectMake(50, 50, 200, 300)) 
let attributedString = NSMutableAttributedString(string: "before after") 
let textAttachment = NSTextAttachment() 
textAttachment.image = UIImage(named: "sample_image.jpg")! 

let oldWidth = textAttachment.image!.size.width; 

let scaleFactor = oldWidth/(textView.frame.size.width - 10); //for the padding inside the textView 
textAttachment.image = UIImage(CGImage: textAttachment.image!.CGImage, scale: scaleFactor, orientation: .Up) 
var attrStringWithImage = NSAttributedString(attachment: textAttachment) 
attributedString.replaceCharactersInRange(NSMakeRange(6, 1), withAttributedString: attrStringWithImage) 
textView.attributedText = attributedString; 
self.view.addSubview(textView) 
+0

덕분에 당신에게 도움이 될 수 있습니다! 그것은 작동! 하지만 ** NSAttributedString **을 ** NSUserDefaults **에 저장하면 이미지 크기가 다시 커집니다. – Daniel

+0

@ 대니얼 .. 나는 또한 같은 문제에 직면하고있다. 해결책은 무엇입니까? – iOS

+0

답변보기 http://stackoverflow.com/a/30417310/2564702 –

관련 문제