Objective c CTFrame剪切文本的第一行
我在使用核心文本时遇到了一个问题,我在Objective c CTFrame剪切文本的第一行,objective-c,ios,core-text,ctframe,Objective C,Ios,Core Text,Ctframe,我在使用核心文本时遇到了一个问题,我在CTFrame中显示的文本的第一行在顶部被截断,如下面的屏幕截图所示,带有字符“B”: 在CTFrame中设置前导时,我认为我做错了什么。我的代码如下: - (void)drawRect:(CGRect)rect { [super drawRect:rect]; CGContextRef context = UIGraphicsGetCurrentContext(); NSAttributedString *myString;
CTFrame
中显示的文本的第一行在顶部被截断,如下面的屏幕截图所示,带有字符“B”:
在CTFrame
中设置前导时,我认为我做错了什么。我的代码如下:
- (void)drawRect:(CGRect)rect
{
[super drawRect:rect];
CGContextRef context = UIGraphicsGetCurrentContext();
NSAttributedString *myString;
//Create the rectangle into which we'll draw the text
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, NULL, self.bounds);
//Flip the coordinate system
CGContextSetTextMatrix(context, CGAffineTransformIdentity);
CGContextTranslateCTM(context, 0, self.bounds.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
//Setup leading (line height)
CGFloat lineHeight = 25;
CTParagraphStyleSetting settings[] = {
{ kCTParagraphStyleSpecifierMinimumLineHeight, sizeof(CGFloat), &lineHeight },
{ kCTParagraphStyleSpecifierMaximumLineHeight, sizeof(CGFloat), &lineHeight },
};
CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(settings, sizeof(settings) / sizeof(settings[0]));
NSDictionary * attrs = [[NSDictionary alloc] initWithObjectsAndKeys:
(__bridge id)CTFontCreateWithName((__bridge CFStringRef) font.fontName, font.pointSize, NULL) ,
(NSString*)kCTFontAttributeName,
(id)textColor.CGColor,
(NSString*)kCTForegroundColorAttributeName,
(__bridge id) paragraphStyle,
kCTParagraphStyleAttributeName,
nil];
myString = [[NSAttributedString alloc] initWithString:text attributes:attrs];
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)myString);
CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0,[myString length]), path, NULL);
CTFrameDraw(frame, context);
CFRelease(frame);
CFRelease(framesetter);
CFRelease(path);
}
其他的SO帖子(和)并没有真正的帮助
如何防止CTFrame
剪裁第一行
--编辑--
将线宽减少到20:
lineheight
从第二行开始考虑,但第一行文本的基线低于顶部20
此处CGFloat lineHeight=25
我确信(从您的屏幕截图)它小于您当前使用的字体大小(font.pointSize
)。
这是剪切文本顶部的唯一原因,因为文本大小无法容纳25个高度点
您有两个选择:
1) 删除段落样式及其相关代码。用以下代码替换您的NSDictionary*attrs=…
NSDictionary * attrs = [[NSDictionary alloc] initWithObjectsAndKeys:
(__bridge id)CTFontCreateWithName((__bridge CFStringRef) font.fontName, font.pointSize, NULL) ,
(NSString*)kCTFontAttributeName,
(id)textColor.CGColor, (NSString*)kCTForegroundColorAttributeName,
nil];
CTFrameDraw()
将自动考虑线条高度
2) 或始终提供大于最大字体大小的线宽。比如lineHeight=font.pointSize+somemargine
要理解上述解释,请尝试以下方法:
在当前代码中设置lineHeight=20代码>;您可以看到更多的文本将从顶行剪切,第二行文本将更接近顶行的底部。我设法解决了这个问题。最后,它变成了我翻转坐标系的位置,通过在代码前面翻转它,问题就解决了。我最初认为,但从顶部到基线的测量结果表明,第一条线远小于规定的线高
,而第二条线则完全符合规定。请参阅问题中的编辑。第二行和第三行使用两行之间的标准空格。如果你低于线高度太多(如10),你可以看到他们的剪辑也。做一件事,注释掉(uu bridge id)paragraphStyle,kCTParagraphStyleAttributeName,尝试不使用行距和paragraphStyle。我注意到您多年前就得到了解决方案。但是你能帮我更详细地解释一下你说的“早点翻页”是什么意思吗?因为我在问题部分通读了您的代码,一旦创建了CGMutablePathRef
,它就会直接进行翻转。