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

问题描述 投票:1回答:1

我想使用CoreText做一些事情,但是我对这个函数有一些疑问: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:使用未解析的标识符'CTFramesetterCreateWithAttributedString'

图像是我的代码 enter image description here

swift core-text
1个回答
0
投票

你需要添加

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")
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.