如果你正在嘗試轉換變量包含一個Swift字符串到CFString我認爲@freytag釘他的解釋。
如果任何人想看到一個例子,我想我會包含一個代碼片段,在這裏我將一個Swift字符串(在這種情況下爲「ArialMT」)轉換爲NSString,以便與核心文本中的CTFontCreateWithName函數(這需要一個CFString)。 (注意:從NSString到CFString的轉換是隱含的)。
// Create Core Text font with desired size
let coreTextFont:CTFontRef = CTFontCreateWithName("ArialMT" as NSString, 25.0, nil)
// Center text horizontally
var paragraphStyle: NSMutableParagraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = NSTextAlignment.Center
// Center text vertically
let fontBoundingBox: CGRect = CTFontGetBoundingBox(coreTextFont)
let frameMidpoint = CGRectGetHeight(self.frame)/2
let textBoundingBoxMidpoint = CGRectGetHeight(fontBoundingBox)/2
let verticalOffsetToCenterTextVertically = frameMidpoint - textBoundingBoxMidpoint
// Create text with the following attributes
let attributes = [
NSFontAttributeName : coreTextFont,
NSParagraphStyleAttributeName: paragraphStyle,
kCTForegroundColorAttributeName:UIColor.whiteColor().CGColor
]
var attributedString = NSMutableAttributedString(string:"TextIWantToDisplay", attributes:attributes)
// Draw text (CTFramesetterCreateFrame requires a path).
let textPath: CGMutablePathRef = CGPathCreateMutable()
CGPathAddRect(textPath, nil, CGRectMake(0, verticalOffsetToCenterTextVertically, CGRectGetWidth(self.frame), CGRectGetHeight(fontBoundingBox)))
let framesetter: CTFramesetterRef = CTFramesetterCreateWithAttributedString(attributedString)
let frame: CTFrameRef = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, attributedString.length), textPath, nil)
CTFrameDraw(frame, context)
在這種情況下,爲什麼不直接使用'let url = NSURL.fileURLWithPath(path,isDirectory:false)' –
它們都是橋接的,只是將它們投射到 – Sulthan
@EricD。你什麼意思? – ciccioska