gpt4 book ai didi

ios - 在 Swift 3 中通过 Core Graphics 绘制文本

转载 作者:IT王子 更新时间:2023-10-29 05:17:10 28 4
gpt4 key购买 nike

背景:我正在使用 Core Graphics 在 UIImageView 上绘图。我想最终在核心图形绘图上绘制一个文本字符串。

This (hackingwithswift.com)我用来将文本绘制到图像上的方法,将我的 imageView 设置为该图像,然后使用核心图形进行绘制。但是我想让文字覆盖核心图形绘制。

现在我已经转移到这个https://stackoverflow.com/a/35574171/6337391但它仍然不允许我在我的绘图上写字。

class MapViewController: UIViewController {

@IBOutlet var imageView: UIImageView!

func drawScale() {
// establish 6 points to draw a little bar with
let length = CGFloat(80.0)
let bottom = imageView.bounds.size.height - 15.0
let left = CGFloat(20.0)
let top = bottom - 20.0
let middle = top + 10.0
let right = left + length
let topLeft = CGPoint(x: left, y: top)
let topRight = CGPoint(x: right, y: top)
let bottomRight = CGPoint(x: right, y: bottom)
let bottomLeft = CGPoint(x: left, y: bottom)
let middleLeft = CGPoint(x: left, y: middle)
let middleRight = CGPoint(x: right, y: middle)

// draw the bar
drawLineFrom(fromPoint: topLeft, toPoint: bottomLeft, color: UIColor.red.cgColor, width: 1.0, alias: false)
drawLineFrom(fromPoint: topRight, toPoint: bottomRight, color: UIColor.red.cgColor, width: 1.0, alias: false)
drawLineFrom(fromPoint: middleLeft, toPoint: middleRight, color: UIColor.red.cgColor, width: 1.0, alias: false)

// draw a text string
UIGraphicsBeginImageContext(self.imageView.frame.size)

let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .center

let attrs = [
NSFontAttributeName: UIFont.systemFont(ofSize: 12),
NSParagraphStyleAttributeName: paragraphStyle,
NSForegroundColorAttributeName: UIColor.red]

let scaleString = "45 feet"
scaleString.draw(at: CGPoint(x: 0, y: 50), withAttributes: attrs)

let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
}

/* Stroke a line between two CGPoints. Color, width, anti-alias options. */
func drawLineFrom(fromPoint: CGPoint, toPoint: CGPoint, color: CGColor, width: CGFloat, alias: Bool) {
// 1
UIGraphicsBeginImageContext(self.imageView.frame.size)
let context = UIGraphicsGetCurrentContext()!
context.setShouldAntialias(alias)
self.imageView.image?.draw(in: CGRect(x: 0, y: 0, width: self.imageView.frame.size.width, height: self.imageView.frame.size.height))

// 2
context.move(to: fromPoint)
context.addLine(to: toPoint)

// 3
context.setLineWidth(width)
context.setStrokeColor(color)

// 4
context.strokePath()

// 5
self.imageView.image = UIGraphicsGetImageFromCurrentImageContext()
self.imageView.alpha = 1.0
UIGraphicsEndImageContext()
}
}

不起作用。绘制文本字符串会完全删除该条。如果将条形图代码移到字符串绘制代码下方,那么两者都是可见的,但条形图当然在字符串上方。

最佳答案

使用 swift 4:

let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .center

let attributes = [
NSAttributedStringKey.paragraphStyle: paragraphStyle,
NSAttributedStringKey.font: UIFont.systemFont(ofSize: 12.0),
NSAttributedStringKey.foregroundColor: UIColor.blue
]

let myText = "HELLO"
let attributedString = NSAttributedString(string: myText, attributes: attributes)

let stringRect = CGRect(x: W/2 - 50, y: border, width: 100, height: H)
attributedString.draw(in: stringRect)

对于 Swift 4.2,将上面的 attributes 更改为:

let attributes: [NSAttributedString.Key : Any] = [
.paragraphStyle: paragraphStyle,
.font: UIFont.systemFont(ofSize: 12.0),
.foregroundColor: UIColor.blue
]

关于ios - 在 Swift 3 中通过 Core Graphics 绘制文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42498721/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com