Reputation: 31
I want to draw an attributed string with clipping rect, and I found a function written in Objective-C,
CFDictionaryRef PINCHFrameAttributesCreateWithClippingRect(CGRect clippingRect, CGAffineTransform transform)
{
CGPathRef clipPath = CGPathCreateWithRect(clippingRect, &transform);
CFDictionaryRef options;
CFStringRef keys[] = {kCTFramePathClippingPathAttributeName};
CFTypeRef values[] = {clipPath};
CFDictionaryRef clippingPathDict = CFDictionaryCreate(NULL,
(const void **)&keys, (const void **)&values,
sizeof(keys) / sizeof(keys[0]),
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
CFTypeRef clippingArrayValues[] = { clippingPathDict };
CFArrayRef clippingPaths = CFArrayCreate(NULL, (const void **)clippingArrayValues, sizeof(clippingArrayValues) / sizeof(clippingArrayValues[0]), &kCFTypeArrayCallBacks);
CFStringRef optionsKeys[] = {kCTFrameClippingPathsAttributeName};
CFTypeRef optionsValues[] = {clippingPaths};
options = CFDictionaryCreate(NULL, (const void **)&optionsKeys, (const void **)&optionsValues,
sizeof(optionsKeys) / sizeof(optionsKeys[0]),
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
CFRelease(clippingPathDict);
CFRelease(clippingPaths);
CGPathRelease(clipPath);
return options;
}
But I failed to translate it in Swift 3, anybody can help me?
// I tried belowing codes, it compiles and run, but text doesn't do clip:
let keys: [CFString] = [kCTFrameClippingPathsAttributeName]
let values: [CFTypeRef] = [clipPath]
let keysPointer = UnsafeMutablePointer<UnsafeRawPointer?>.allocate(capacity: keys.count)
keysPointer.initialize(to: keys)
let valuesPointer = UnsafeMutablePointer<UnsafeRawPointer?>.allocate(capacity: values.count)
valuesPointer.initialize(to: values)
let options = CFDictionaryCreate(kCFAllocatorDefault, keysPointer, valuesPointer, 1, nil, nil)
let framesetter = CTFramesetterCreateWithAttributedString(attributedSring)
let frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, attributedSring.length), rectPath.cgPath, options)
CTFrameDraw(frame, context)
Upvotes: 3
Views: 2195
Reputation: 6247
Construct a Swift dictionary and then cast it to CFDictionary
. Example:
let d = [
kCGImageSourceShouldAllowFloat as String : true as NSNumber,
kCGImageSourceCreateThumbnailWithTransform as String : true as NSNumber,
kCGImageSourceCreateThumbnailFromImageAlways as String : true as NSNumber,
kCGImageSourceThumbnailMaxPixelSize as String : w as NSNumber
] as CFDictionary
More details here: https://bugs.swift.org/browse/SR-2388
Upvotes: 2