如何将UILabel文本颜色设置为背景图像

时间:2014-04-02 11:14:25

标签: ios

我想设计一个视图,其中UILabel文字应该保持透明,以便背景图像可以通过文本看到,UILabel的透明文字 移动UILabel时,文字颜色应根据背景而改变  图片 请帮帮我

UIImage *croppedImage=[self crop:self.demo.frame];

 self.demo.textColor=[UIColor colorWithPatternImage:croppedImage];

我正在使用demo(UILabel)的大小裁剪图像并设置其文本颜色 但是图像得到双像素化

1 个答案:

答案 0 :(得分:0)

通过传递确切的LabelSize来裁剪图像;

// get sub image
- (UIImage*) getSubImageFrom: (UIImage*) img WithRect: (CGRect) rect {

    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();

    // translated rectangle for drawing sub image
    CGRect drawRect = CGRectMake(-rect.origin.x, -rect.origin.y, img.size.width, img.size.height);

    // clip to the bounds of the image context
    // not strictly necessary as it will get clipped anyway?
    CGContextClipToRect(context, CGRectMake(0, 0, rect.size.width, rect.size.height));

    // draw image
    [img drawInRect:drawRect];

    // grab image
    UIImage* subImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return subImage;
}