gpt4 book ai didi

iphone - 如何在 Core Text Programming 中设置字体大小和名称?

转载 作者:可可西里 更新时间:2023-11-01 05:23:30 26 4
gpt4 key购买 nike

下面是我的代码崩溃了

CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(currentText);

我正在尝试设置字体,请帮帮我,谢谢。

+(void)drawText:(NSString*)textToDraw inFrame:(CGRect)frameRect
{

// CFStringRef stringRef = (__bridge CFStringRef)textToDraw;
// Prepare the text using a Core Text Framesetter

NSAttributedString *stringRef=[[[NSAttributedString alloc]initWithString:textToDraw attributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont boldSystemFontOfSize:12.0],(NSString*)kCTFontAttributeName, nil]]autorelease];

CFAttributedStringRef currentText =(__bridge CFAttributedStringRef)stringRef;
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(currentText);// at this Line it giving me EXC_BAD_ACCESS

CGMutablePathRef framePath = CGPathCreateMutable();
CGPathAddRect(framePath, NULL, frameRect);
CFRange currentRange = CFRangeMake(0, 0);
CTFrameRef frameRef = CTFramesetterCreateFrame(framesetter, currentRange, framePath, NULL);
CGPathRelease(framePath);
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGContextSetTextMatrix(currentContext, CGAffineTransformIdentity);
CGContextTranslateCTM(currentContext, 0, frameRect.origin.y*2);
CGContextScaleCTM(currentContext, 1.0, -1.0);
CTFrameDraw(frameRef, currentContext);
CGContextScaleCTM(currentContext, 1.0, -1.0);
CGContextTranslateCTM(currentContext, 0, (-1)*frameRect.origin.y*2);
CFRelease(frameRef);
//CFRelease(currentText);
CFRelease(framesetter);
//CFRelease(stringRef);
}

最佳答案

试试这个

CTFontRef font = CTFontCreateWithName((CFStringRef)@"Helvetica", 16.0f, nil);
CFAttributedStringSetAttribute(currentText,CFRangeMake(0, _stringLength-1),kCTFontAttributeName,font);

关于iphone - 如何在 Core Text Programming 中设置字体大小和名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12577706/

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