我想使用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'
答案 0 :(得分: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")
}
}