Sie sollten Ihren Ausführungsdelegaten als Attribut für einen Bereich von Zeichen in der attributierten Zeichenfolge hinzufügen. Siehe Core Text String Attributes. Beim Zeichnen ruft Core Text Ihre Callbacks auf, um die Größe dieser Zeichen zu ermitteln.
aktualisieren
Dies ist ein Beispielcode für eine Ansicht, die einen einfachen Text-Zeichnung (Beachten Sie, dass kein Memory-Management-Code gibt es hier).
@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
Die Größe des Leerzeichen zwischen "Delegat" und "Leerzeichen" im Text werden von der Run-Delegate gesteuert.
Ich habe das schon gemacht, aber es hilft nicht. Die Dokumentation selbst gibt nicht viele Informationen über CTRunDelegate. Wenn Sie einen Beispielcode für CTRunDelegate haben, wird das eine große Hilfe sein. – tek3
Ich habe die Antwort bekommen, bevor ich deine Antwort gesehen habe, aber du hast versucht, mir die richtige Antwort zu geben, also geht diese Prämie an dich. Vielen Dank für Ihre Antwort. – tek3
Hallo Mo, ich brauche noch etwas Hilfe von dir für meine Bewerbung. Wirst du daran interessiert sein, mir zu helfen? – tek3