gpt4 book ai didi

ios - 包裹的 CATextLayer 中的字符被部分截断

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:49:53 25 4
gpt4 key购买 nike

我有创建 CATextLayer 并将其绘制到 UIView 上的代码:

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
CGRect viewRect = CGRectMake(50.0f, 50.0f, 345.0f, 120.0f);

CATextLayer *textLayer = [CATextLayer layer];
textLayer.contentsScale = [[UIScreen mainScreen] scale];
textLayer.wrapped = YES;
textLayer.truncationMode = kCATruncationNone;
UIFont *font = [UIFont fontWithName:@"SnellRoundhand" size:20.0f];
textLayer.string = @"Lorem Lipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.";
textLayer.alignmentMode = kCAAlignmentLeft;
textLayer.fontSize = font.pointSize;
CTFontRef fontRef = CTFontCreateWithName((__bridge CFStringRef)font.fontName, font.pointSize, nil);
textLayer.font = fontRef;
CFRelease(fontRef);

textLayer.backgroundColor = [[UIColor lightGrayColor] CGColor];
textLayer.foregroundColor = [[UIColor blackColor] CGColor];
textLayer.frame = viewRect;


UIGraphicsBeginImageContextWithOptions(textLayer.frame.size, NO, 0);
[textLayer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *textImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

UIImageView *textImageView = [[UIImageView alloc] initWithImage:textImage];
textImageView.frame = viewRect;
[self.view addSubview:textImageView];
}

不幸的是,一些字符在渲染 View 时被截断了。在我发布的示例中,开头的“L”缺少最左边的像素,第二个末尾的“d”缺少最右边的像素。

有没有办法在不改变文本换行方式的情况下防止这种截断的发生?

最佳答案

我没有使用 CATextLayer 的默认行为,而是编写了一个从 CATextLayer 获取数据并手动将其绘制到 CGContext 中的函数。我为填充添加了一个参数来解决截止问题。

-(UIImage *)imageFromCATextLayer:(CATextLayer *)layer andPaddingSize:(CGSize)paddingSize
{
CGFloat paddingWidth = paddingSize.width;
CGFloat paddingHeight = paddingSize.height;
CGRect textBounds = layer.frame;
CGRect paddedImageBounds = CGRectMake(0.0f, 0.0f, textBounds.size.width + 2 * paddingWidth, textBounds.size.height + 2 * paddingHeight);
UIGraphicsBeginImageContextWithOptions(paddedImageBounds.size, NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();

CGContextTranslateCTM(context, 0.0f, paddedImageBounds.size.height);
CGContextScaleCTM(context, 1, -1);

CGContextSetFillColorWithColor(context, layer.backgroundColor);
CGContextFillRect(context, paddedImageBounds);

CTTextAlignment alignment;
if ([layer.alignmentMode isEqualToString: kCAAlignmentLeft]) {
alignment = kCTLeftTextAlignment;
}
else if ([layer.alignmentMode isEqualToString: kCAAlignmentCenter]) {
alignment = kCTCenterTextAlignment;
}
else if ([layer.alignmentMode isEqualToString: kCAAlignmentRight]) {
alignment = kCTRightTextAlignment;
}
else {
alignment = kCTLeftTextAlignment;
}

CTParagraphStyleSetting paragraphSettings = {kCTParagraphStyleSpecifierAlignment, sizeof(alignment), &alignment};
CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(&paragraphSettings, 1);

NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:layer.font, (NSString*)kCTFontAttributeName, paragraphStyle, kCTParagraphStyleAttributeName, layer.foregroundColor, kCTForegroundColorAttributeName, nil];
NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:layer.string attributes:attributes];
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge_retained CFAttributedStringRef)attrString);
CGMutablePathRef p = CGPathCreateMutable();
CGPathAddRect(p, NULL, CGRectMake(10.0f, 2 * paddingHeight - 10.0f, textBounds.size.width, textBounds.size.height));
CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0,0), p, NULL);
CTFrameDraw(frame, context);

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return image;
}

关于ios - 包裹的 CATextLayer 中的字符被部分截断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11765613/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com