簡體   English   中英

如何在Core Text Programming中設置字體大小和名稱?

[英]How to set Font size and name in Core Text Programming?

下面是我的代碼崩潰

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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM