struct InlineView: View {
private var text = "This is a multi-line text. It has multiple lines. It has multiple lines. It has multiple lines. It has multiple lines. "
private var image = "info" // Replace with your image name
private let font: Font = .system(size: 17)
var body: some View {
Text(text)
.font(font)
+ Text("\(getCustomImage())")
}
private func getCustomImage(color: Color = .gray, newSize: CGSize = CGSize(width: 17, height: 17)) -> Text {
if let image = UIImage(named: image),
let newImage = convertImageToNewFrame(image: image, newFrameSize: newSize) {
return Text(
Image(uiImage: newImage)
.renderingMode(.template)
)
.baselineOffset(-1.5)
.foregroundStyle(color)
}
return Text(Image(systemName: "heart.fill"))
}
func convertImageToNewFrame(image: UIImage, newFrameSize: CGSize) -> UIImage? {
UIGraphicsBeginImageContextWithOptions(newFrameSize, false, 0.0)
image.draw(in: CGRect(origin: .zero, size: newFrameSize))
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage
}
}
1条答案
按热度按时间wmtdaxz31#
我找到了一个解决方案,并写了一篇简短的文章。我愿意探索其他方法。
Inline Text and Image
这是让事情为我工作的代码: