我有 NSAttributedString 之类的(只有一个符号 - s 和属性):
s{
CTForegroundColor = "<CGColor 0x8878330> [<CGColorSpace 0x717da80> (kCGColorSpaceDeviceGray)] ( 0 0 )";
CTRunDelegate = "<CTRunDelegate 0x8878270 [0x16e14d8]>";
NSFont = "CTFont <name: HelveticaNeue-Light, size: 15.000000, matrix: 0x0>\nCTFontDescriptor <attributes: <CFBasicHash 0x88720f0 [0x16e14d8]>{type = mutable dict, count = 1,\nentries =>\n\t1 : <CFString 0x2a9838 [0x16e14d8]>{contents = \"NSFontNameAttribute\"} = <CFString 0x886f5f0 [0x16e14d8]>{contents = \"HelveticaNeue-Light\"}\n}\n>";
我创建了框架设置器和框架(它们不是零)并想要获取行:
NSArray *lines = (__bridge NSArray *)CTFrameGetLines(frame);
但是这个数组是空的。我不明白为什么。
提前致谢。
UPD:我的框架是
CTFrame: visible string range = (0, 0)<CGPath 0x75a63f0><CFArray 0x759a5d0 [0x16e14d8]>{type = mutable-small, count = 0, values = ()}
似乎核心文本没有看到属性字符串
UPD:添加了运行委托的代码
CTFontRef fontRef = CTFontCreateWithName((__bridge CFStringRef)kSBTextFontAttribute, kSBFontSize, NULL);
CTRunDelegateCallbacks callbacks;
callbacks.version = kCTRunDelegateVersion1;
callbacks.getAscent = ascentCallback;
callbacks.getDescent = descentCallback;
callbacks.getWidth = widthCallback;
callbacks.dealloc = 0;
NSDictionary *imgAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
@kSmileWidth, kSBCallbackWidth,
@kSmileHeight, kSBCallbackHeight,
nil];
CTRunDelegateRef delegate = CTRunDelegateCreate(&callbacks, CFBridgingRetain(imgAttributes));
NSDictionary *attrDictionaryDelegate = [NSDictionary dictionaryWithObjectsAndKeys:
//set the delegate
(__bridge id)delegate, (NSString*)kCTRunDelegateAttributeName,
(id)kSmileFontColor, kCTForegroundColorAttributeName,
(__bridge id)fontRef, kCTFontAttributeName,
nil];
/////////
static void deallocCallback( void* ref ){
ref = nil;
}
static CGFloat ascentCallback( void *ref ){
return [(NSString*)[(__bridge NSDictionary*)ref objectForKey:kSBCallbackHeight] floatValue];
}
static CGFloat descentCallback( void *ref ){
return [(NSString*)[(__bridge NSDictionary*)ref objectForKey:kSBCallbackDescending] floatValue];
}
static CGFloat widthCallback( void* ref ){
return [(NSString*)[(__bridge NSDictionary*)ref objectForKey:kSBCallbackWidth] floatValue];
}