我正在开发一个应用程序,我在UIImage
内有一个文本和一个UITextView
,我试图将文本以及图像垂直和左边对齐水平方向,我该怎样才能做到这一点?
let image = UIImage(named: "Downward Arrow - 01.png") //Step (10) This is where the image is defined.
let attachment = NSTextAttachment() // Step (10) inserting a NSTextAttachment in order to attach the image in a UITextView.
let scaledImage = image?.scaleImageToSize(img: image!, size: CGSize(width: 30, height: 30)) // this line of code is needed in order to resize the size of the image.
attachment.image = scaledImage // Step (10) setting the image to be attached to the scaled one that we just obtained out of the previous code.
//put your NSTextAttachment into and attributedString
let attString = NSAttributedString(attachment: attachment) // Step (10).
testTextView.textStorage.insert(attString, at: testTextView.selectedRange.location) // Step (10).
答案 0 :(得分:0)
你可以使用边缘插入
这样的东西let image = UIImage(named: "Downward Arrow - 01.png") //Step (10) This is where the image is defined.
let attachment = NSTextAttachment() // Step (10) inserting a NSTextAttachment in order to attach the image in a UITextView.
let scaledImage = image?.scaleImageToSize(img: image!, size: CGSize(width: 30, height: 30)) // this line of code is needed in order to resize the size of the image.
let vCenter = ((testTextView.frame.size.height - 30) / 2)
let padding = UIEdgeInsets(top: vCenter, left: 0, bottom: 0, right: 0)
testTextView.textContainerInset = padding
attachment.image = scaledImage // Step (10) setting the image to be attached to the scaled one that we just obtained out of the previous code.
//put your NSTextAttachment into and attributedString
let pic = NSAttributedString(attachment: attachment) // Step (10).
let text = NSAttributedString(string: "YOUR TEXT")
let attString = NSMutableAttributedString()
attString.append(pic)
attString.append(text)
testTextView.textStorage.insert(attString, at: testTextView.selectedRange.location) // Step (10).