我有一个表格视图,每个单元格都是一个自定义的表格单元格视图,有一个子视图,子视图使用CoreText绘制文本。子视图的类名是 CCoreTextView,下面是 CCoreTextView 的一些代码
@interface CoreTextView()
{
CTFrameRef _frame;
}
@end
- (void) initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
UILongPressGestureRecognizer *myLongPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(myLongPressHandle:)];
[self addGestureRecognizer:myLongPressRecognizer];
[myLongPressRecognizer release];
}
}
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
[self drawText:context];
}
- (void)drawText:(CGContextRef)context
{
CTFontRef font = CTFontCreateWithName((CFStringRef)self.font.fontName, self.font.pointSize, NULL);
NSDictionary *attribs = [NSDictionary dictionaryWithObjectsAndKeys:(id)font, kCTFontAttributeName, nil];
NSMutableAttributedString *attribString = [[NSMutableAttributedString alloc] initWithString:self.text attributes:attribs];
CFRelease(font);
CGContextSetTextMatrix(context, CGAffineTransformIdentity);
CGContextTranslateCTM(context, 0, self.bounds.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGFloat headIdent = CORETEXTVIEW_HEAD_IDENT;
CGFloat tailIdent = CORETEXTVIEW_TAIL_IDENT;
CTParagraphStyleSetting settings[] = {
{kCTParagraphStyleSpecifierAlignment, sizeof(_textAlignment), &_textAlignment },
{kCTParagraphStyleSpecifierLineSpacing, sizeof(CGFloat), &_leading},
{kCTParagraphStyleSpecifierFirstLineHeadIndent, sizeof(CGFloat), &_firstLineHeadIndent},
{kCTParagraphStyleSpecifierHeadIndent, sizeof(CGFloat), &headIdent},
{kCTParagraphStyleSpecifierTailIndent, sizeof(CGFloat), &tailIdent}
};
CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(settings, sizeof(settings) / sizeof(settings[0]));
CFAttributedStringSetAttribute((CFMutableAttributedStringRef)attribString,
CFRangeMake(0, [self.text length]),
kCTParagraphStyleAttributeName,
paragraphStyle);
CFRelease(paragraphStyle);
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attribString);
CGRect columnFrame = CGRectMake(0,
0,
self.bounds.size.width,
self.bounds.size.height);
columnFrame = UIEdgeInsetsInsetRect(columnFrame, UIEdgeInsetsMake(0, CORETEXTVIEW_EDGE_INSET_LEFT, 0, CORETEXTVIEW_EDGE_INSET_RIGHT));
CGMutablePathRef framePath = CGPathCreateMutable();
CGPathAddRect(framePath, &CGAffineTransformIdentity, columnFrame);
CFRange textRange = CFRangeMake(0, 0);
if (NULL != _frame) {
CFRelease(_frame);
_frame = NULL;
}
_frame = CTFramesetterCreateFrame(framesetter, textRange, framePath, NULL);
CTFrameDraw(_frame, context);
CFRelease(framePath);
[attribString release];
CFRelease(framesetter);
}
- (void)myLongPressHandle:(UILongPressGestureRecognizer *)gestureRecognizer
{
CGPoint point = [gestureRecognizer locationInView:self];
NSArray *lineArr = (NSArray*)CTFrameGetLines(_frame);
NSInteger lineCount = [lineArr count];
NSLog(@"lineCount=%d, frame=%@, lineArr=%@", lineCount, _frame, lineArr);
}
问题是当我按下表格视图的一个单元格(名为 CellA)时,不要抬起手指,将手指移动到另一个单元格(名为 CellB),我发现 longPressHandle 仍然使用 CellA 的子视图。 NSLog
打印 CellA 的子视图的内容。