简体   繁体   English

快速使用未解决的标识符“ CTFramesetterCreateWithAttributedString”

[英]swift Use of unresolved identifier 'CTFramesetterCreateWithAttributedString'

I want to use CoreText do something, however I have some question about the function:TFramesetterCreateWithAttributedString(). 我想使用CoreText做一些事情,但是我对函数有一些疑问:TFramesetterCreateWithAttributedString()。 how should I do? 我应该怎么做? Thanks! 谢谢!

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' 错误消息:使用未解决的标识符“ CTFramesetterCreateWithAttributedString”

The image is my code 图片是我的代码
在此处输入图片说明

You need to add 您需要添加

import CoreText

EDIT: 编辑:

Below code is compiling fine for me: 下面的代码对我来说很好:

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")
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM