Bạn nên thêm đại biểu chạy của mình làm thuộc tính cho một dải ký tự trong chuỗi được phân bổ của bạn. Xem Core Text String Attributes. Khi vẽ, Core Text sẽ gọi callbacks của bạn để nhận kích thước của các ký tự đó.
Cập nhật
Đây là một mẫu mã cho một cái nhìn vẽ một văn bản đơn giản (Lưu ý rằng không có mã quản lý bộ nhớ ở đây).
@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
Kích thước ký tự khoảng trắng giữa "đại biểu" và "khoảng trống" trong văn bản được kiểm soát bởi đại biểu chạy.
Tôi đã làm điều đó nhưng nó không giúp ích gì. Bản thân tài liệu không cung cấp nhiều thông tin về CTRunDelegate. nếu bạn có bất kỳ mã mẫu nào cho CTRunDelegate, điều đó sẽ giúp ích rất nhiều. – tek3
Tôi đã nhận được câu trả lời trước khi tôi nhìn thấy câu trả lời của bạn nhưng bạn đã nỗ lực để cho tôi câu trả lời đúng, vì vậy tiền thưởng này sẽ đến với bạn. Thanx rất nhiều cho phản ứng của bạn. – tek3
Xin chào, tôi cần thêm trợ giúp từ bạn cho đơn đăng ký của tôi. Bạn có muốn giúp tôi không? – tek3