我有它,用户可以在屏幕上移动图像和 uilabel。假设它是黑色图像和黑色文本。我希望与图像重叠的文本部分变得透明,以便背景显示出来,并且您仍然可以阅读文本,即使它们最初是相同的颜色。希望这是有道理的。
我找到了一些有关触摸删除的链接。我不知道如何使用重叠的文本部分来自动执行此操作。
也许是这样的“用图像掩盖图像”? https://developer.apple.com/library/mac/#documentation/graphicsimaging/conceptual/drawingwithquartz2d/dq_images/dq_images.html
这是我移动图像(以及文本)的方法
-(void)graphicMoved:(UIPanGestureRecognizer *)recognizer
{
CGPoint translation = [recognizer translationInView:self.view];
recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
recognizer.view.center.y + translation.y);
[recognizer setTranslation:CGPointMake(0, 0) inView:self.view];
}
添加代码 在将文本和图像部分重叠放置后,我在单击按钮时调用 creatAlphaClippedImage。图像为 imageMerge,标签为 labelTest。
我所做的就是在开始时分配它们,然后在最后,获取输出图像并将其分配给 imageMerge.image ,该图像被添加到我的 View 中,角落里的界面生成器只是为了测试。我不知道 nslog 应该显示什么,但它不为空。
-(void)createAlphaClippedImage {
UIImageView *imageView = nil;//this will b your imageView, property that u are holding to
UILabel *label = nil;//this will be your label, property that u are holding to
imageView=imageMerge;
label=labelTest;
if (CGRectIntersectsRect(imageView.frame, label.frame)) {
UIImage *bottomImage = imageView.image;
UIImage *image = nil;
UIGraphicsBeginImageContextWithOptions(bottomImage.size, NO, 0.0);
CGRect imageRect = CGRectMake(0.0f, 0.0f, bottomImage.size.width, bottomImage.size.height);
[bottomImage drawInRect:imageRect];
//the color that u want the text to have
[[UIColor clearColor] set];
//change this textRect to change the position of text on image
CGRect textRect = CGRectMake(CGRectGetMinX(label.frame) - CGRectGetMinX(imageView.frame), CGRectGetMinY(label.frame) - CGRectGetMinY(imageView.frame), CGRectGetWidth(label.frame), CGRectGetHeight(label.frame));
[label.text drawInRect:textRect withFont:label.font];
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
imageView.image = image;
imageMerge.image=image;
NSLog(@"ImageMerge %@\n",imageMerge.image);
}
}
最佳答案
假设您已经完成了在屏幕上移动 imageView 的操作。让我继续讨论该做什么。
- 当您触摸 imageView 时。使用我将提供的代码更改其中的图像。
- 完成移动 imageView 后,用旧图像替换新图像
new image = one with black image + black text; old image = one with black image + transparent text;
-(UIImage *)customImage:(NSString *)cellImageName withText:(NSString *)badgeText textColor:(UIColor*)color {
UIImage *bottomImage = [UIImage imageNamed:cellImageName];
//Change the font size to change text size
CGSize stringSize = [badgeText sizeWithFont:[UIFont systemFontOfSize:[UIFont systemFontSize]]];
UIImage *image = nil;
UIGraphicsBeginImageContextWithOptions(bottomImage.size, NO, 0.0);
CGRect imageRect = CGRectMake(0.0f, 0.0f, bottomImage.size.width, bottomImage.size.height);
[bottomImage drawInRect:imageRect];
//the color that u want the text to have
[color set];
//change this textRect to change the position of text on image
CGRect textRect = CGRectMake((bottomImage.size.width - stringSize.width)/2.0f, bottomImage.size.height - stringSize.height, stringSize.width, stringSize.height);
[badgeText drawInRect:textRect withFont:[UIFont systemFontOfSize:[UIFont systemFontSize]]];
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
现在在开始之前,为了方便起见,扔掉标签(如果愿意,您可以用 CALayer 替换 UIImageView),即代替
imageView.image = image you can write layer.contents = (id)image.CGImage
现在,当您设置 ImageView 或 Layer 时,首先将图像获取为
UIImage *image = [self customImage:@"Image Name" withText:@"Text" textColor:[UIColor blackColor]];
并将该图像放入 imageView 或图层
现在,当您开始移动 ImageView 或图层时。在 touchesBegan
中或任何一个方法是您触摸的第一响应者。再次将图像替换为
UIImage *image = [self customImage:@"Image Name" withText:@"Text" textColor:[UIColor clearColor]];
并在 touchesEnded
中再次用黑色文本的第一次调用替换图像。
这应该能让你继续前进。如有问题请告诉我。
上述方法提供了将文本写入图像的顶部,并创建一个包含文本的新图像。因此您可以扔掉标签。
干杯,玩得开心。
编辑
试试这个,我自己没有尝试过,但应该可以
-(void)createAlphaClippedImage {
UIImageView *imageView = nil;//this will b your imageView, property that u are holding to
UILabel *label = nil;//this will be your label, property that u are holding to
if (CGRectIntersectsRect(imageView.frame, label.frame)) {
UIImage *bottomImage = imageView.image;
UIImage *image = nil;
UIGraphicsBeginImageContextWithOptions(bottomImage.size, NO, 0.0);
CGRect imageRect = CGRectMake(0.0f, 0.0f, bottomImage.size.width, bottomImage.size.height);
[bottomImage drawInRect:imageRect];
//the color that u want the text to have
[[UIColor clearColor] set];
//change this textRect to change the position of text on image
CGRect textRect = CGRectMake(CGRectGetMinX(label.frame) - CGRectGetMinX(imageView.frame), CGRectGetMinY(label.frame) - CGRectGetMinY(imageView.frame), CGRectGetWidth(label.frame), CGRectGetHeight(label.frame));
[label.text drawInRect:textRect withFont:label.font];
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
imageView.image = image;
}
}
在touchesBegan和touchesMoved中调用该方法。如果动画卡顿(这不会发生),请尝试在动画 block 内调用它。
试试这个并让我知道。
编辑:示例应用程序的链接
I have created a sample app showing how to clear overlapping areas. Check it. https://www.dropbox.com/sh/5z45gkwgmstfyvu/lwzbUyh6IR
关于ios - 从 UILabel 中删除与文本重叠的图像部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14811126/