0

我使用此代码使用队列中的核心图形在 UILabel(我已经对其进行了子类化)上绘制 NSString,但屏幕上没有显示任何内容。我没有错误。

我从队列中调用这样的函数。

[label1 createtext];

-(void)createtext
{          
    UIFont *font=[UIFont fontWithName:@"Times New Roman" size:15.0];
    //Core Text (Create Attributed String)

    NSMutableArray *Text=[globaltextarray Text];
    CGSize size=[[Text objectAtIndex:0] sizeWithFont:font];

    if (UIGraphicsBeginImageContextWithOptions != NULL)
        UIGraphicsBeginImageContextWithOptions(size,NO,0.0);
    else
        // iOS is < 4.0
        UIGraphicsBeginImageContext(size);

    CGContextRef ctx = UIGraphicsGetCurrentContext();

    if (ctx != NULL)
        NSLog(@"not null context");
    UIColor *textColor = [UIColor blackColor];
    CGColorRef color = textColor.CGColor;

    CTFontRef font = CTFontCreateWithName((CFStringRef) @"TimesNewRomanPSMT", 15.0, NULL);

    CTTextAlignment theAlignment = kCTLeftTextAlignment;

    CFIndex theNumberOfSettings = 1;
    CTParagraphStyleSetting theSettings[1] = {
        { kCTParagraphStyleSpecifierAlignment, sizeof(CTTextAlignment),
            &theAlignment }
    };

    CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(theSettings, theNumberOfSettings);

    NSDictionary *attributesDict = [NSDictionary dictionaryWithObjectsAndKeys:(id)font, (NSString *)kCTFontAttributeName,color, (NSString *)kCTForegroundColorAttributeName,paragraphStyle, (NSString *) kCTParagraphStyleAttributeName,nil];

    NSAttributedString *stringToDraw = [[NSAttributedString alloc] initWithString:[NSString stringWithCString:[[Text objectAtIndex:indexpath1.row] cStringUsingEncoding:NSISOLatin1StringEncoding] encoding:NSUTF8StringEncoding] attributes:attributesDict];

    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)stringToDraw);

    //Prepare our view for drawing

    CGContextSetTextMatrix(ctx, CGAffineTransformIdentity);
    CGContextTranslateCTM(ctx, 0, ([self bounds]).size.height );
    CGContextScaleCTM(ctx, 1.0, -1.0);

    //Create Frame
    CGMutablePathRef path = CGPathCreateMutable();
    CGRect rect  = self.frame;
    CGPathAddRect(path, NULL, rect);

    CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, NULL);

    //Draw Frame
    CTFrameDraw(frame, ctx);

    UIGraphicsEndImageContext();
    //Release all retained objects
    CFRelease(framesetter);
    CFRelease(path);
    [stringToDraw release];
}

任何帮助表示赞赏!

4

2 回答 2

1

您正在创建一个临时图形上下文,将其绘制到其中,然后将其丢弃。你永远不会告诉 UIKit 在屏幕上放任何东西。

您还需要:

例如:

// In your class's declaration, define an image view to show your drawing.
// Make sure that this view is created (either as part of a storyboard/nib, or with explicit code)
@property (nonatomic) UIImageView *imageView;

- (void)createText
{
    CGSize size = /* you decide */;
    UIGraphicsBeginImageContext(size);

    // draw into the graphics context
    // after you're done:

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

    dispatch_async(dispatch_get_main_queue(), ^{
        self.imageView.image = image;
    });
}
于 2013-06-29T03:49:14.167 回答
-2

您不是在使用 CoreGraphics 框架进行绘制,而是在使用 UIKit 进行绘制,而这在后台线程中是不允许的。您只能从主线程调用 UIKit 方法。例如,您正在调用 UIGraphicsBeginImageContext() 但这是 UIKit 的一部分。无论如何,您不太可能从在后台线程中渲染小部件代码中获得任何好处,除非您正在执行一些真正断开连接的离线渲染(例如创建 PDF 或其他东西),那么您应该只在主线程中渲染您的小部件。

于 2013-06-29T03:11:00.953 回答