如何在Swift 3中使用CFDictionaryCreate?

时间:2016-10-20 06:12:22

标签: core-graphics swift3 cgcontext core-foundation

我想用剪切矩形绘制一个属性字符串,我找到了一个用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;
}

但是我没能在Swift 3中翻译它,任何人都可以帮助我吗?

// 我尝试了下面的代码,它编译并运行,但文本没有剪辑:

    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)

1 个答案:

答案 0 :(得分:2)

构造一个Swift字典,然后将其转换为select field01 ,field02 ,case field03 when 'calc1' then field01 - field02 when 'calc2' then field01 + field02 end as AmountCalc ,case AmountCalc when 0 then 'amount is 0' else 'Greater than 0' end as result from myTable 。例如:

CFDictionary

此处有更多详情:https://bugs.swift.org/browse/SR-2388