【问题标题】:Drawing text over Core Graphics in Swift 3在 Swift 3 中的 Core Graphics 上绘制文本
【发布时间】:2017-07-18 19:58:05
【问题描述】:

背景:我正在使用 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()
    }
}

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

【问题讨论】:

  • 能否在 CATextLayer 中创建文本并将其添加到图像视图中?
  • “我正在使用 Core Graphics 在 UIImageView 上绘图”是什么意思,您在哪个类下运行您的代码?
  • 我添加了一些上下文代码,希望对您有所帮助。我认为答案将在于核心文本,我将尝试自己找到解决方案。 link 之类的东西看起来很有希望

标签: ios swift swift3 core-graphics


【解决方案1】:

使用 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
]

【讨论】:

  • swift 4.2 NSAttributedString.Key.xxx
  • 我能问一下画文本而不是使用UILabel有什么好处吗?抱歉,我是 Swift 新手
  • 如果您不是自定义视图,则不能使用 UIlabel。
【解决方案2】:

Swift 3.0:绘制文本

   func drawMyText(myText:String,textColor:UIColor, FontName:String, FontSize:CGFloat, inRect:CGRect){

     let textFont = UIFont(name: FontName, size: FontSize)!
    let textFontAttributes = [
        NSFontAttributeName: textFont,
        NSForegroundColorAttributeName: textColor,
        ] as [String : Any]

    myText.draw(in: inRect, withAttributes: textFontAttributes)
}

调用这个函数,下面是例子

let _rect = CGRect(x: 40, y: 100, width: 40, height: 40)

 drawMyText(myText: "Hello", textColor: UIColor.black, FontName: "Helvetica Bold", FontSize: 11 , inRect:_rect)

【讨论】:

  • 这与我的代码不同之处在于您删除了 UIGraphicsBeginImageContext(self.imageView.frame.size) 。在我的情况下,您的代码没有绘制任何文本。
  • 将drawMyText函数放到uiview子类中并从drawrect函数中调用,如果需要进一步帮助请告诉我
  • 我的问题是绘制文本会擦除我之前绘制的内容。在我上面的示例中,文本会出现,但它会完全擦除我之前绘制的线条。据我所知,您唯一的建议是我在矩形中而不是在某个点上绘制文本,这对我没有任何作用。
  • 小文本。我想将文本绘制为图像大小
猜你喜欢
  • 1970-01-01
  • 2012-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-23
  • 1970-01-01
相关资源
最近更新 更多