[英]swift Use of unresolved identifier 'CTFramesetterCreateWithAttributedString'
我想使用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")
}
}
錯誤消息:使用未解決的標識符“ 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")
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.