swift 使用未解析的标识符 'CTFramesetterCreateWithAttributedString'

swift Use of unresolved identifier 'CTFramesetterCreateWithAttributedString'

我想使用 CoreText 做一些事情,但是我对 function:TFramesetterCreateWithAttributedString() 有一些疑问。我应该怎么做?谢谢!

import UIKit

class NLUICoreTextLabel: UIView {

override init(frame: CGRect) {
    super.init(frame: frame)
    initparagraph()
}

func initparagraph(){
    let context = UIGraphicsGetCurrentContext()
    context?.translateBy(x: 0, y: bounds.size.height)
    context?.scaleBy(x: 1.0, y: -1.0)
    context?.textMatrix = .identity
    let path = CGMutablePath()
    let bounds = CGRect(x: 10.0, y: 10.0, width: 200.0, height: 200.0)
    path.addRect(bounds)
    let textString: CFString = "Core Text Core Text Core Text Core Text Core Text Core Text Core Text Core Text Core Text Core Text Core Text Core Text " as CFString
    let attrString = CFAttributedStringCreateMutable(kCFAllocatorDefault, 0)
    CFAttributedStringReplaceString(attrString, CFRangeMake(0, 0), textString)
    let rgbColor = CGColorSpaceCreateDeviceRGB()
    let components: Array<CGFloat> = [1.0, 0.0, 0.0, 0.8]
    let red = CGColor.init(colorSpace: rgbColor, components: components)
    CFAttributedStringSetAttribute(attrString, CFRangeMake(0, 12), NSAttributedString.Key.foregroundColor as CFString, red)
    //There have a trouble        
    let frameSetter = CTFramesetterCreateWithAttributedString(attrString)
}


required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
    }
}

errorMessage: Use of unresolved identifier 'CTFramesetterCreateWithAttributedString'

图片是我的代码

您需要添加

import CoreText

编辑:

下面的代码对我来说编译得很好:

import Foundation
import UIKit

class NLUICoreTextLabel: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        initparagraph()
    }

    func initparagraph(){
        let context = UIGraphicsGetCurrentContext()
        context?.translateBy(x: 0, y: self.bounds.size.height)
        context?.scaleBy(x: 1.0, y: -1.0)
        context?.textMatrix = .identity
        let path = CGMutablePath()
        let bounds = CGRect(x: 10.0, y: 10.0, width: 200.0, height: 200.0)
        path.addRect(bounds)
        let textString: CFString = "Core Text Core Text Core Text Core Text Core Text Core Text Core Text Core Text Core Text Core Text Core Text Core Text " as CFString
        let attrString = CFAttributedStringCreateMutable(kCFAllocatorDefault, 0)!
        CFAttributedStringReplaceString(attrString, CFRangeMake(0, 0), textString)
        let rgbColor = CGColorSpaceCreateDeviceRGB()
        let components: Array<CGFloat> = [1.0, 0.0, 0.0, 0.8]
        let red = CGColor.init(colorSpace: rgbColor, components: components)
        CFAttributedStringSetAttribute(attrString, CFRangeMake(0, 12), NSAttributedString.Key.foregroundColor as CFString, red)
        //There have a trouble
        let frameSetter = CTFramesetterCreateWithAttributedString(attrString)
    }


    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}