垂直和水平对齐文本以及 UITextView 中的 UIImage
Vertically and Horizontally Alignment of Text as well as UIImage inside a UITextView
我正在开发一个应用程序,其中我在 UITextView
中有一个文本和一个 UIImage
,我正在尝试将文本和图像垂直并向左对齐在水平方向上,我怎样才能做到这一点?
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).
你可以使用边缘插入得到类似的东西
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).
结果:
我正在开发一个应用程序,其中我在 UITextView
中有一个文本和一个 UIImage
,我正在尝试将文本和图像垂直并向左对齐在水平方向上,我怎样才能做到这一点?
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).
你可以使用边缘插入得到类似的东西
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).
结果: