objective-c - DrawRect 添加文字阴影

标签 objective-c ios drawrect dropshadow

我第一次在自定义 UITableViewCell 中使用 -drawRect,我想知道如何将 1px dropShadows 添加到所有正在绘制的文本和图像( self.image).

提前致谢。

- (void) drawRect:(CGRect) rect {

    CGContextRef context = UIGraphicsGetCurrentContext();
    [[UIColor clearColor] set];
    CGContextFillRect(context, rect);

    if (shouldDrawImage == YES) {
        CGContextDrawImage(context, CGRectMake(10, 10, 40, 40), self.image.CGImage);
    }

    CGContextDrawImage(context, CGRectMake(self.frame.size.width - 16, 0, 16, self.frame.size.height), [UIImage imageNamed:@"right_bar_including_holes"].CGImage);      
    NSString *authorName = [[self.info objectForKey:@"user"] objectForKey:@"full_name"];

    [RGB(219, 240, 73) set];

    CGSize maximumLabelSize = CGSizeMake(self.frame.size.width - 10 - 55 - 16,9999);
    CGSize authorsize = [authorName sizeWithFont:[UIFont boldSystemFontOfSize:15]
                                     constrainedToSize:maximumLabelSize 
                                         lineBreakMode:UILineBreakModeWordWrap]; 
    [authorName drawInRect:CGRectMake(60, 10, self.frame.size.width - 60, authorsize.height) withFont:[UIFont boldSystemFontOfSize:15]];

    [RGB(249,249,249) set];

    NSString *description = [self.info objectForKey:@"description"];
    CGSize descriptionSize = [description sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:maximumLabelSize lineBreakMode:UILineBreakModeWordWrap];

    [description drawInRect:CGRectMake(60, authorsize.height + 15, descriptionSize.width, descriptionSize.height) withFont:[UIFont systemFontOfSize:14]];
}

最佳答案

使用:

CGContextSaveGState(context);
CGContextSetShadow(context, CGSizeMake(1,1),1);
//draw text here
CGContextRestoreGState(context);

第一个参数是context,第二个参数是offset,第三个参数是blur。

Quartz2d Docs on Shadows

关于objective-c - DrawRect 添加文字阴影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11058167/

相关文章:

ios - 我真的需要在自定义 UIView 中使用 drawRect() 吗?

ios - 创建后如何更改IBAction事件类型?

objective-c - 通过有线通信将网络摄像头类型的摄像机传输到 iPad2

ios - 从动态创建的 UITextFields 访问文本值。 iOS

objective-c - 如何使用 NSDate 从今天的日期中查找工作日?

ios - 如何在 iOS 项目中旋转没有任何 subview 的 View ?

IOS 警报未启动/单击警报转到下一页 SWIFT

iphone - drawRect 在 iPhone(模拟器)中反转 imageView

ios - 我到底应该在哪里调用 [FBSettings publishInstall :appId]

ios - CGContextDrawLinearGradient 与 cagradientlayer?