gpt4 book ai didi

ios - 如何避免 UIGraphicsGetCurrentContext() 在 Swift 中返回 "nil"?

转载 作者:行者123 更新时间:2023-11-28 11:47:38 27 4
gpt4 key购买 nike

我正在尝试学习如何通过 ( NSAttributedString background color and rounded corners ) 获得圆形背景突出显示...

所以我正在关注 Apple's Core Text Programming Guide并基本上尝试在 Swift 中重新创建所有 Objective-C 代码。

我遇到错误,UIGraphicsGetCurrentContext() 返回 nil

这是我的 ViewController,它会在上一个 View 中点击按钮时被推送:

import UIKit
import CoreGraphics

public class HighlightPractiveViewController: UIViewController {

override public func viewDidLoad() {
drawRect()
}

func drawRect() {

let context: CGContext = UIGraphicsGetCurrentContext()!
context.translateBy(x: 0, y: view.bounds.size.height)
context.scaleBy(x: 1.0, y: -1.0)
context.textMatrix = CGAffineTransform.identity

let path = CGMutablePath()

let bounds = CGRect(x: 10.0, y: 10.0, width: 200.0, height: 200.0)
path.addRect(bounds)


let textString = NSString(string: "Hello, World!") // CF & NS are toll-free bridged.

let attrString = CFAttributedStringCreateMutable(kCFAllocatorDefault, 0)

CFAttributedStringReplaceString(attrString, CFRangeMake(0, 0), textString)

let rgbColorSpace: CGColorSpace = CGColorSpaceCreateDeviceRGB()
let components: [CGFloat] = [1.0, 0.0, 0.0, 0.8]
let red: CGColor = CGColor(colorSpace: rgbColorSpace, components: components)!

CFAttributedStringSetAttribute(attrString, CFRangeMake(0, 12), kCTForegroundColorAttributeName, red)

let framesetter = CTFramesetterCreateWithAttributedString(attrString!)
let frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, nil)

CTFrameDraw(frame, context)
}
}

如何以及在何处正确调用 UIGraphicsGetCurrentContext() 以使其不返回 nil?

最佳答案

您不必在 UIViewController 中实现方法 drawRect()。您必须在自定义 UIView 中执行此操作。

class MyView: UIView {

override func draw(_ rect: CGRect) {
super.draw(rect)

// write what you want to implement here
}
}

然后将您的自定义 MyView 添加到 UIViewControl 的 View 层次结构中。

class HighlightPractiveViewController: UIViewController {

func viewDidLoad() {
super.viewDidLoad() // always call super's methods

let myView = MyView()
view.addSubview(myView)
}
}

关于ios - 如何避免 UIGraphicsGetCurrentContext() 在 Swift 中返回 "nil"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52315907/

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