如何使用 UIKit 在图像下添加一些文本
How to add some text under the image using UIKit
我有这样一张图片:
但是我想在这张图片下面添加一些文字,并把它当作一个新的 UIImage,例如:
据我所知,我应该创建一个新图像。然后将我的图像放入其中并添加文本。
我该怎么做?
我尝试使用类似的东西,但无法将我的文字放在图片下方
func textToImage(drawText text: String, inImage image: UIImage, atPoint point: CGPoint) -> UIImage {
let textColor = UIColor.white
let textFont = UIFont(name: "Helvetica Bold", size: 12)!
let scale = UIScreen.main.scale
UIGraphicsBeginImageContextWithOptions(image.size, false, scale)
let textFontAttributes = [
NSAttributedStringKey.font: textFont,
NSAttributedStringKey.foregroundColor: textColor,
] as [NSAttributedStringKey : Any]
image.draw(in: CGRect(origin: CGPoint.zero, size: image.size))
let rect = CGRect(origin: point, size: image.size)
text.draw(in: rect, withAttributes: textFontAttributes)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage!
}
PS 我的图片背景很清晰。红色是另一个视图的背景。
一般来说,您应该总是展示一些您到目前为止尝试过的代码。但这是一种普通的视图,看起来应该类似于:
class ViewWithImage: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
setup()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
fatalError("storyboards no thanks")
}
private func setup() {
backgroundColor = .red
let imageView = UIImageView()
imageView.image = UIImage(named: "garbagecan")
imageView.contentMode = .scaleAspectFit
imageView.tintColor = .white
let deleteLabel = UILabel()
deleteLabel.text = "Delete"
deleteLabel.textColor = .white
let vStack = UIStackView()
vStack.axis = .vertical
vStack.spacing = 8
addSubview(vStack)
NSLayoutConstraint.activate([
vStack.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 16),
vStack.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -16),
vStack.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: -16),
vStack.topAnchor.constraint(equalTo: self.topAnchor, constant: 16)
])
vStack.addArrangedSubview(imageView)
vStack.addArrangedSubview(deleteLabel)
}
}
我有这样一张图片:
但是我想在这张图片下面添加一些文字,并把它当作一个新的 UIImage,例如:
据我所知,我应该创建一个新图像。然后将我的图像放入其中并添加文本。 我该怎么做?
我尝试使用类似的东西,但无法将我的文字放在图片下方
func textToImage(drawText text: String, inImage image: UIImage, atPoint point: CGPoint) -> UIImage {
let textColor = UIColor.white
let textFont = UIFont(name: "Helvetica Bold", size: 12)!
let scale = UIScreen.main.scale
UIGraphicsBeginImageContextWithOptions(image.size, false, scale)
let textFontAttributes = [
NSAttributedStringKey.font: textFont,
NSAttributedStringKey.foregroundColor: textColor,
] as [NSAttributedStringKey : Any]
image.draw(in: CGRect(origin: CGPoint.zero, size: image.size))
let rect = CGRect(origin: point, size: image.size)
text.draw(in: rect, withAttributes: textFontAttributes)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage!
}
PS 我的图片背景很清晰。红色是另一个视图的背景。
一般来说,您应该总是展示一些您到目前为止尝试过的代码。但这是一种普通的视图,看起来应该类似于:
class ViewWithImage: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
setup()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
fatalError("storyboards no thanks")
}
private func setup() {
backgroundColor = .red
let imageView = UIImageView()
imageView.image = UIImage(named: "garbagecan")
imageView.contentMode = .scaleAspectFit
imageView.tintColor = .white
let deleteLabel = UILabel()
deleteLabel.text = "Delete"
deleteLabel.textColor = .white
let vStack = UIStackView()
vStack.axis = .vertical
vStack.spacing = 8
addSubview(vStack)
NSLayoutConstraint.activate([
vStack.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 16),
vStack.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -16),
vStack.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: -16),
vStack.topAnchor.constraint(equalTo: self.topAnchor, constant: 16)
])
vStack.addArrangedSubview(imageView)
vStack.addArrangedSubview(deleteLabel)
}
}