标签: drawinrect

drawInRect:withAttributes vs drawInRect:withFont:lineBreakMode:alignment

我正在开发我的应用程序的新版本,并且我正在尝试替换已弃用的消息,但我无法通过这个消息.

我无法弄清楚为什么drawInRect:withAttributes不起作用.drawInRect:withFont:lineBreakMode:alignment发送消息时代码正确显示,但drawInRect:withAttributes发送时不起作用.

我使用相同的矩形和字体,我相信是相同的文本样式.常量只是将rect定位在图像下方,但我对两个调用使用相同的rect,所以我确定矩形是正确的.

(注意下面使用的bs.name是一个NSString对象)

        CGRect textRect = CGRectMake(fCol*kRVCiPadAlbumColumnWidth,
                                     kRVCiPadAlbumColumnWidth-kRVCiPadTextLabelYOffset,
                                     kRVCiPadAlbumColumnWidth,
                                     kRVCiPadTextLabelHeight);
        NSMutableParagraphStyle *textStyle = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];
        textStyle.lineBreakMode = NSLineBreakByWordWrapping;
        textStyle.alignment = NSTextAlignmentCenter;
        UIFont *textFont = [UIFont systemFontOfSize:16];
Run Code Online (Sandbox Code Playgroud)

使用上面的变量,这不起作用(屏幕上没有任何内容)

        [bs.name drawInRect:textRect
             withAttributes:@{NSFontAttributeName:textFont,
                              NSParagraphStyleAttributeName:textStyle}];
Run Code Online (Sandbox Code Playgroud)

这可以使用上面相同的变量工作(在屏幕上正确绘制字符串)

        [bs.name drawInRect:textRect
                   withFont:textFont
              lineBreakMode:NSLineBreakByWordWrapping
                  alignment:NSTextAlignmentCenter];
Run Code Online (Sandbox Code Playgroud)

任何援助都会很棒.谢谢.

objective-c nsstring ios drawinrect

30
推荐指数
2
解决办法
3万
查看次数

使用drawInRect时如何设置文本颜色?

我一直在使用PaintCode在我的应用程序中绘制一些图形,一些绘制的元素具有使用"drawInRect"绘制的文本.无论我做什么尝试并设置颜色,文本总是显示为黑色.

这是创建文本的代码,我试图设置颜色:

       //// Rounded Rectangle Drawing
    UIBezierPath* roundedRectanglePath = [UIBezierPath bezierPathWithRoundedRect: CGRectMake(0, 0, 66, 66) cornerRadius: 15];
    [[NPSColor NPSPrimaryColor] setFill];
    [roundedRectanglePath fill];

    UIFont *helveticaFont = [UIFont fontWithName:@"AvenirNextCondensed-Bold" size:50];

    NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
    /// Set line break mode
    paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
    /// Set text alignment
    paragraphStyle.alignment = NSTextAlignmentCenter;

    NSDictionary *attributes = @{NSFontAttributeName:helveticaFont,
                                 NSParagraphStyleAttributeName:paragraphStyle};


    //// Text Drawing
    CGRect textRect = CGRectMake(-4, -2.5, 74, 71.5);
    [[NPSColor whiteColor] set];
    [textContent drawInRect: CGRectInset(textRect, 0, 6) withAttributes:attributes];

}
Run Code Online (Sandbox Code Playgroud)

"文本绘图"中的白色是不能正常工作的.谢谢你的帮助.

ios cgrect drawinrect paintcode

6
推荐指数
1
解决办法
6592
查看次数

将CGImageRef(不是UIImage)绘制到图像上下文中

以下是如何在上下文中绘制UIImage:

UIGraphicsBeginImageContextWithOptions(size, NO, 0.0f);
[someUIImage drawInRect:CGRectMake(...)];
[someOtherUIImage drawInRect:CGRectMake(...)];
[someOtherUIImage drawInRect:CGRectMake(...)];
UIImage *yourResult = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Run Code Online (Sandbox Code Playgroud)

没问题.但是,假设我有一个以某种方式构建的CGImageRef.

CGImageRef happyImageRef = CGImageCreateWithImageInRect(blah blah);
Run Code Online (Sandbox Code Playgroud)

我想把它绘制到上下文中.

UIGraphicsBeginImageContextWithOptions(size, NO, 0.0f);
[ happyImageRef .. somehowDrawInRect:CGRectMake(...)];
UIImage *yourResult = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Run Code Online (Sandbox Code Playgroud)

你是怎样做的?

要清楚,显然你可以将它转换为UIImage

UIImage *wasteful = [UIImage imageWithCGImage:happyImageRef];
Run Code Online (Sandbox Code Playgroud)

但这看起来非常低效.怎么做?

我确定我很困惑或遗漏了一些简单的东西,谢谢.

cgimageref uigraphicscontext drawinrect

3
推荐指数
1
解决办法
2186
查看次数

iOS drawInRect内存泄漏 - ARC

我在互联网上搜索过,我发现很多人和我有同样的问题而没有解决方案......

如果我有像NSTimer这样的东西并让它反复循环而我将这段代码粘在其中由于某种原因我得到大量的内存泄漏并且应用程序在大约100个左右循环后崩溃.

但我启用了ARC.

drawInRect根据乐器,记忆问题肯定是胜利.

-(void)nstimerTick {

    UIGraphicsBeginImageContextWithOptions(testView.frame.size, NO, 0.0);

    [[testView image] drawInRect:testView.bounds];

    testView.image = UIGraphicsGetImageFromCurrentImageContext();

}
Run Code Online (Sandbox Code Playgroud)

memory objective-c ios automatic-ref-counting drawinrect

1
推荐指数
1
解决办法
1126
查看次数

drawInRect:withFont:iOS 7无效

我使用的是一个库(SGInfoAlert),它使用了弃用的代码drawInRect:r withFont:.我尝试更改一些代码以在iOS 7中修复它,但文本没有显示.谁知道为什么会这样?

// Changed this
//[info_ drawInRect:r withFont:[UIFont systemFontOfSize:kSGInfoAlert_fontSize]];

// To this
NSDictionary *textAttributes = @{NSFontAttributeName: [UIFont systemFontOfSize:kSGInfoAlert_fontSize]};

[info_ drawInRect:r withAttributes:textAttributes];
Run Code Online (Sandbox Code Playgroud)

这是git存储库https://github.com/sagiwei/SGInfoAlert

deprecated ios7 drawinrect

1
推荐指数
1
解决办法
2421
查看次数