您應該將您的運行委託添加爲屬性字符串中一系列字符的屬性。請參閱Core Text String Attributes。在繪製時,核心文本會調用你的回調來獲取字符的大小。
更新
這是用於繪製簡單的文本視圖中的示例代碼(注意,沒有內存管理代碼在這裏)。
@implementation View
/* Callbacks */
void MyDeallocationCallback(void* refCon){
}
CGFloat MyGetAscentCallback(void *refCon){
return 10.0;
}
CGFloat MyGetDescentCallback(void *refCon){
return 4.0;
}
CGFloat MyGetWidthCallback(void* refCon){
return 125;
}
- (void)drawRect:(CGRect)rect {
// create an attributed string
NSMutableAttributedString * attrString = [[NSMutableAttributedString alloc] initWithString:@"This is my delegate space"];
// create the delegate
CTRunDelegateCallbacks callbacks;
callbacks.version = kCTRunDelegateVersion1;
callbacks.dealloc = MyDeallocationCallback;
callbacks.getAscent = MyGetAscentCallback;
callbacks.getDescent = MyGetDescentCallback;
callbacks.getWidth = MyGetWidthCallback;
CTRunDelegateRef delegate = CTRunDelegateCreate(&callbacks, NULL);
// set the delegate as an attribute
CFAttributedStringSetAttribute((CFMutableAttributedStringRef)attrString, CFRangeMake(19, 1), kCTRunDelegateAttributeName, delegate);
// create a frame and draw the text
CTFramesetterRef frameSetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attrString);
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, NULL, rect);
CTFrameRef frame = CTFramesetterCreateFrame(frameSetter, CFRangeMake(0, attrString.length), path, NULL);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetTextMatrix(context, CGAffineTransformIdentity);
CGContextSetTextPosition(context, 0.0, 0.0);
CTFrameDraw(frame, context);
}
@end
文本中「委託」和「空間」之間的空格字符大小由運行委託來控制。
我已經這樣做了,但它沒有幫助。文檔本身並沒有提供關於CTRunDelegate的更多信息。如果你有任何CTRunDelegate的示例代碼,那將會很有幫助。 – tek3 2010-07-22 13:23:41
我在看到你的答案之前就已經得到了答案,但是你爲了給我正確的答案而付出了努力,所以這個賞金就給你了。非常感謝您的回覆。 – tek3 2010-07-23 07:09:39
嗨,我需要一些更多的幫助。你有興趣幫助我嗎? – tek3 2010-07-23 07:34:50