ひ!サブビューのクリッピング機能を備えたテキストビューを作成しています。アイデアは、すべてのテキストがすべてのサブビューの周りに描画されるようにすることです。問題は、コンテンツの高さを取得することです。
ドキュメントが不足しているため、CTFramesetterSuggestFrameSizeWithConstraints の属性ディクショナリは CTFramesetterCreateFrame と同じであると判断しました。
これが私のクリッピングパスコードです:
-(CFDictionaryRef)clippingPathsDictionary{
if(self.subviews.count==0)return NULL;
NSMutableArray *pathsArray = [[NSMutableArray alloc] init];
CGAffineTransform transform = CGAffineTransformIdentity;
transform = CGAffineTransformScale(transform, 1, -1);
transform = CGAffineTransformTranslate(transform, 0, -self.bounds.size.height);
for (int i=0; i<self.subviews.count; i++) {
UIView *clippingView = self.subviews[i];
CGPathRef clipPath = CGPathCreateWithRect(clippingView.frame, &transform);
NSDictionary *clippingPathDictionary = [NSDictionary dictionaryWithObject:(__bridge id)(clipPath) forKey:(__bridge NSString *)kCTFramePathClippingPathAttributeName];
[pathsArray addObject:clippingPathDictionary];
CFRelease(clipPath);
}
int eFrameWidth=0;
CFNumberRef frameWidth = CFNumberCreate(NULL, kCFNumberNSIntegerType, &eFrameWidth);
int eFillRule = kCTFramePathFillEvenOdd;
CFNumberRef fillRule = CFNumberCreate(NULL, kCFNumberNSIntegerType, &eFillRule);
int eProgression = kCTFrameProgressionTopToBottom;
CFNumberRef progression = CFNumberCreate(NULL, kCFNumberNSIntegerType, &eProgression);
CFStringRef keys[] = { kCTFrameClippingPathsAttributeName, kCTFramePathFillRuleAttributeName, kCTFrameProgressionAttributeName, kCTFramePathWidthAttributeName};
CFTypeRef values[] = { (__bridge CFTypeRef)(pathsArray), fillRule, progression, frameWidth};
CFDictionaryRef clippingPathsDictionary = CFDictionaryCreate(NULL,
(const void **)&keys, (const void **)&values,
sizeof(keys) / sizeof(keys[0]),
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
return clippingPathsDictionary;}
テキストの描画に使用していますが、問題なく動作します。ここに私の描画コードがあります:
- (void)drawRect:(CGRect)rect{
CGAffineTransform transform = CGAffineTransformIdentity;
transform = CGAffineTransformScale(transform, 1, -1);
transform = CGAffineTransformTranslate(transform, 0, -rect.size.height);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextConcatCTM(context, transform);
CFAttributedStringRef attributedString = (__bridge CFAttributedStringRef)self.attributedString;
CTFramesetterRef frameSetter = CTFramesetterCreateWithAttributedString(attributedString);
CFDictionaryRef attributesDictionary = [self clippingPathsDictionary];
CGPathRef path = CGPathCreateWithRect(rect, &transform);
CTFrameRef frame = CTFramesetterCreateFrame(frameSetter, CFRangeMake(0, self.attributedString.length), path, attributesDictionary);
CFRelease(path);
CFRelease(attributesDictionary);
CTFrameDraw(frame, context);
CFRelease(frameSetter);
CFRelease(frame);
CGSize contentSize = [self contentSizeForWidth:self.bounds.size.width];
CGPathRef highlightPath = CGPathCreateWithRect((CGRect){CGPointZero, contentSize}, &transform);
CGContextSetFillColorWithColor(context, [UIColor colorWithRed:.0 green:1 blue:.0 alpha:.3].CGColor);
CGContextAddPath(context, highlightPath);
CGContextDrawPath(context, kCGPathFill);
CFRelease(highlightPath);
}
それが結果です:
それはまさに私が期待していたものです!
最後に、高さを確認するコードは次のとおりです。
-(CGSize)contentSizeForWidth:(float)width{
CFAttributedStringRef attributedString = (__bridge CFAttributedStringRef)self.attributedString;
CTFramesetterRef frameSetter = CTFramesetterCreateWithAttributedString(attributedString);
CFDictionaryRef attributesDictionary = [self clippingPathsDictionary];
CGSize size = CTFramesetterSuggestFrameSizeWithConstraints(frameSetter, CFRangeMake(0, self.attributedString.length), attributesDictionary, CGSizeMake(width, CGFLOAT_MAX), NULL);
NSLog(@"%s: size = %@",__PRETTY_FUNCTION__, NSStringFromCGSize(size));
CFRelease(attributesDictionary);
CFRelease(frameSetter);
return size;
}
出力は次のようになります。
Core Text Demo[2222:a0b] -[DATextView contentSizeForWidth:]: size = {729.71484, 0}
誰かが解決策を持っていますか?ご清聴ありがとうございました:)