Jeśli próbujesz konwertować zmienną który zawiera ciąg Swift do CFString Myślę, że @freytag przybity to z jego wyjaśnieniem.
Jeśli ktoś chciał zobaczyć przykład, pomyślałem, że dołączę fragment kodu, w którym rzucam ciąg Swift ("ArialMT" w tym przypadku) do NSString, aby użyć go z funkcją CTFontCreateWithName z Core Text (który wymaga CFString). (Uwaga: rzutowanie z NSString na CFString jest niejawne).
// 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)
W tym przypadku, dlaczego nie wystarczy użyć 'let url = NSURL.fileURLWithPath (path, isDirectory: false)' –
wszystkie są zmostkowane, po prostu obsadź je – Sulthan
@EricD. co masz na myśli? – ciccioska