Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cocoa touch 在UITextView中显示首字母过大的文本块_Cocoa Touch_Uitextview_Text Formatting - Fatal编程技术网

Cocoa touch 在UITextView中显示首字母过大的文本块

Cocoa touch 在UITextView中显示首字母过大的文本块,cocoa-touch,uitextview,text-formatting,Cocoa Touch,Uitextview,Text Formatting,有没有办法在UITextView中提供这种效果?如果没有,我怎样才能达到这个效果呢 出于同样的要求,我创建了一个UIView子类,该子类使用一个下拉帽绘制文本。文本是使用核心文本绘制的,正如@CodaFi所建议的,下拉盖是在单独的核心文本框架中绘制的 该类的完整实现: 它的肉看起来像这样: - (void)drawRect:(CGRect)rect { // Create attributed strings NSAttributedString *bodyText = [[NS

有没有办法在
UITextView
中提供这种效果?如果没有,我怎样才能达到这个效果呢


出于同样的要求,我创建了一个
UIView
子类,该子类使用一个下拉帽绘制文本。文本是使用核心文本绘制的,正如@CodaFi所建议的,下拉盖是在单独的核心文本框架中绘制的

该类的完整实现:

它的肉看起来像这样:

- (void)drawRect:(CGRect)rect {
    // Create attributed strings
    NSAttributedString *bodyText = [[NSAttributedString alloc] initWithString:[self.text substringWithRange:NSMakeRange(1, self.text.length -1)] attributes:_bodyAttributes];
    NSAttributedString *capText = [[NSAttributedString alloc] initWithString:[[self.text substringWithRange:NSMakeRange(0, 1)] uppercaseString] attributes:_capAttributes];

    CGRect capFrame = CGRectMake(0, 0, [capText size].width, [capText size].height);

    // Set up graphics context
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetTextMatrix(context, CGAffineTransformIdentity);
    CGContextTranslateCTM(context, 0, self.bounds.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);

    // Create type frames
    CGMutablePathRef bodyPath = CGPathCreateMutable();
    CGAffineTransform transform = CGAffineTransformTranslate(CGAffineTransformMakeScale(1, -1), 0, -self.bounds.size.height);
    CGPathMoveToPoint(bodyPath, &transform, CGRectGetMaxX(capFrame), 0);
    CGPathAddLineToPoint(bodyPath, &transform, self.bounds.size.width, 0);
    CGPathAddLineToPoint(bodyPath, &transform, self.bounds.size.width, self.bounds.size.height);
    CGPathAddLineToPoint(bodyPath, &transform, 0, self.bounds.size.height);
    CGPathAddLineToPoint(bodyPath, &transform, 0, CGRectGetMaxY(capFrame));
    CGPathAddLineToPoint(bodyPath, &transform, CGRectGetMaxX(capFrame), CGRectGetMaxY(capFrame));
    CGPathCloseSubpath(bodyPath);

    CGMutablePathRef capPath = CGPathCreateMutable();
    CGPathAddRect(capPath, &transform, CGRectMake(0, 0, capFrame.size.width+10, capFrame.size.height+10));

    // Draw text
    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef) bodyText);
    CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), bodyPath, NULL);
    CFRelease(framesetter);
    CTFrameDraw(frame, context);
    CFRelease(frame);
    framesetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)capText);
    frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), capPath, NULL);
    CFRelease(framesetter);
    CTFrameDraw(frame, context);
    CFRelease(frame);
}
它的要点是创建两个路径,一个矩形用于包含下拉框,另一个矩形用于删除文本其余部分的槽口。gist的完整实现允许您使用属性控制整个视图的字体、下拉框周围的间距和内容插入

它没有实现
UITextView
的大部分功能(仅实现我需要的功能),因此它可能不是一个完整的解决方案


希望有帮助

大写字母在核心文本中很奇怪,您必须使用额外的CTFrameDraw调用来呈现一个超大字母。@CodaFi Hi,Thanx,谢谢您的回复。你能详细说明一下吗?或者给我提供一些教程或者其他什么吗?@josh caswell:Thanx供你编辑。不幸的是,没有。CoreText是一个非常强大的框架。“我可以带你去火车站。”@CodaFi:没问题。Thanx作为指导,我将研究它并尝试找到解决方案。谢谢你的回答。这是你做出的巨大努力。但我已经用HTML实现了我的这个要求:)。是的,这是一个老问题,但希望它对其他人有用!我最初也使用html,但webkit对于我的应用程序来说速度太慢了。