So the text of my playingCardLabel is #"A \u2663"
- (IBAction)flipCardButtonTapped:(UIButton *)sender
{
if([self.playingCardLabel.textColor isEqual:[UIColor whiteColor]])
self.playingCardLabel.textColor = [UIColor blackColor];
else
self.playingCardLabel.textColor = [UIColor whiteColor];
}
How come when I tap the button, only the A changes to white, the spade (or whatever it is called) remains black no matter what?
Those symbols appear to be fixed in color. Use \U2663 for the black club (♣︎) and use \U2667 for the white club (♧). There are similar black or white version of the spade, heart, and diamond.
create image from symbol of unicode using your colour. use UIImageView instead UILabel, or use UILabel with NSAttributedString + NSTextAttachment (image inside).
#implementation NSString (OSExt)
- (UIImage *)toImageWithAttributes:(NSDictionary*)attributes
{
CGSize size = [self sizeWithAttributes:attributes];
UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
[self drawAtPoint:CGPointZero withAttributes:attributes];
CGContextRef imgctx = UIGraphicsGetCurrentContext();
// use blend mode to fill symbols like #"♣︎"
CGContextSetBlendMode(imgctx, kCGBlendModeSourceAtop);
CGContextSetFillColorWithColor(imgctx, [attributes[NSForegroundColorAttributeName] CGColor]);
CGContextFillRect(imgctx, (CGRect){CGPointZero, size});
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
#end
// use
id attrs = #{NSForegroundColorAttributeName:[UIColor greenColor]};
UIImage *image = [#"♣︎" toImageWithAttributes:attrs];
Related
I have an image with white border and black color filled inside it. I want to just change the black color to another color at runtime. The user will select the color at runtime in HSB format. How can I do that? I tried CGImageCreateWithMaskingColors by taking
const float colorMasking[4]={255, 255, 255, 255};
but I am getting a nil CGImageRef everytime. Please help.
- (UIImage*) maskBlackInImage :(UIImage*) image color:(UIColor*)color
{
const CGFloat colorMasking[4] = { 222, 255, 222, 255 };
CGImageRef imageRef = CGImageCreateWithMaskingColors(image.CGImage, colorMasking);
UIImage* imageB = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return imageB;
}
I am attaching the image of bulb - Bulb with Black color filled, white border and transparent background
Update:
I was able to fill black color with another color after using the code in the accepted answer. But, I could see a little bit of color on white border. The image doesn't look that sharp. Attaching the output:
Create a category of UIImage class and add the following method
- (UIImage *)imageTintedWithColor:(UIColor *)color
{
UIImage *image;
if (color) {
// Construct new image the same size as this one.
UIGraphicsBeginImageContextWithOptions([self size], NO, 0.0); // 0.0 for scale means "scale for device's main screen".
CGRect rect = CGRectZero;
rect.size = [self size];
// tint the image
[self drawInRect:rect];
[color set];
UIRectFillUsingBlendMode(rect, kCGBlendModeScreen);
// restore alpha channel
[self drawInRect:rect blendMode:kCGBlendModeDestinationIn alpha:1.0f];
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
return image;
}
I want to change UISlider thumb image during the slide.
Basically to set thumbImage acording to the value.
The idea was to rotate the image according to the value and set it to the thumb.
So I tried to set the thumb image by overriding
-(BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event
[self setThumbImage:[self imageRotatedByDegrees:50] forState:UIControlStateHighlighted];
and I also tried the same thing when I added an action to my slider.
Unfortunately in both implementations the image simply disappears.
Do you think it possible to achieve in the way I do it?
If NO please explain and suggest an alternative way (hopefully not one that will customize and replace the whole slider)
If Yes I will really appreciate code sample.
The more close answer that I found here
Thanks a lot.
I found a mistake in my Code. Apparently I was releasing one of the image before calling CGContextDrawImage.
(I still need to improve my GUI appearance to make it more nice, for instance to make the track image as I planed I made the original one transparent and added the as subview the one I need.)
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.thumbImage = [self imageWithImage:[UIImage imageNamed:#"IconForSlider.png"] scaledToSize:CGSizeMake(frame.size.height, frame.size.height)];
size = self.thumbImage.size.height;
[self setThumbImage:[self imageRotatedByDegrees:0] forState:UIControlStateNormal];
[self addTarget:self action:#selector(changeValue) forControlEvents:UIControlEventValueChanged];
}
return self;
}
-(void)changeValue{
NSLog(#"ChangeValue");
[self setThumbImage:(UIImage *)[self imageRotatedByDegrees:(self.value * 100*(10/9))] forState:UIControlStateHighlighted];
[self setThumbImage:(UIImage *)[self imageRotatedByDegrees:(self.value * 100*(10/9))] forState:UIControlStateNormal];
}
#pragma mark ImageResize
- (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
#pragma mark Rotate
- (UIImage *)imageRotatedByDegrees:(float)degrees
{
NSLog(#"ImageRotateByDegres");
static float Prc = 0.6;
degrees = (degrees > 90)? 90: degrees;
UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0,size,size)];
CGSize rotatedSize = rotatedViewBox.frame.size;
// Create the bitmap context
UIGraphicsBeginImageContextWithOptions(rotatedSize , NO, 0.0);
CGContextRef bitmap = UIGraphicsGetCurrentContext();
UIColor *color = [self.colorsArray objectAtIndex:lround(self.value*100)];
[color setFill];
CGContextRef bitmap = UIGraphicsGetCurrentContext();
[[UIColor colorWithRed:((139 + (116/100)*self.value*100)/255.0) green:141/255.0f blue:149/255.0f alpha:1.0f] setFill];
// Move the origin to the middle of the image so we will rotate and scale around the center.
CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2);
// Rotate the image context
CGContextRotateCTM(bitmap,DEGREES_TO_RADIANS(degrees));
CGContextClipToMask(bitmap, CGRectMake(-1*size/ 2, -1*size / 2, size, size), [self.thumbImage CGImage]);
CGContextAddRect(bitmap, CGRectMake(-1*size/ 2, -1*size / 2, size, size));
CGContextDrawPath(bitmap,kCGPathFill);
//Prc and 1.07 are for better view
CGContextDrawImage(bitmap, CGRectMake(-1.07*size/2*Prc, -1*size/2*Prc,size*Prc,size*Prc), [[UIImage imageNamed:#"ActiveIcon.png"]CGImage]);
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
with some help from this link I create an array of colours for my slider Get Pixel Color of Uiimage
- (UIColor *)GetColorAtValue:(float)value{
long pixelInfo = (long)floorf(_sliderImage.size.width * _sliderImage.size.height/2 + _sliderImage.size.width * value) * 4 ;
UInt8 red = data[pixelInfo]; // If you need this info, enable it
UInt8 green = data[(pixelInfo + 1)]; // If you need this info, enable it
UInt8 blue = data[pixelInfo + 2]; // If you need this info, enable it
//UInt8 alpha = data[pixelInfo + 3]; // I need only this info for my maze game
//CFRelease(pixelData);
UIColor* color = [UIColor colorWithRed:red/255.0f green:green/255.0f blue:blue/255.0f alpha:1]; // The pixel color info
return color;
}
I create a UIImageView with two UILabel as subviews of it:
UIImageView *img=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, [[UIScreen mainScreen] bounds].size.height)];
img.backgroundColor=self.view.backgroundColor;
UILabel *textOne=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 290, 250)];
textOne.text=_textOneLabel.text;
textOne.textColor=_textOneLabel.textColor;
UILabel *textTwo = [[UILabel alloc] initWithFrame:CGRectMake(100,180,200,100)];
textTwo.text=_textTwoLabel.text;
textTwo.textColor=_textTwoLabel.textColor;
[img addSubview:textOne];
[img addSubview:textTwo];
UIImageWriteToSavedPhotosAlbum(img.image, nil, nil, nil); //img.image is null
I want to create a UIImage to save it in the camera roll with the UIImageView and the 2 created UILabel. img.image returns null because there is no image assigned to the UIImageView.
Do you have an idea of how achieving that?
Use below method to convert your UIImageView to UIImage.
UIImage *image = [self ChangeImageViewToImage:img];
//Method
-(UIImage *) ChangeImageViewToImage : (UIImageView *) view{
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
This should help you
/* Creates an image with a home-grown graphics context, burns the supplied string into it. */
- (UIImage *)burnTextIntoImage:(NSString *)text :(UIImage *)img {
UIGraphicsBeginImageContext(img.size);
CGRect aRectangle = CGRectMake(0,0, img.size.width, img.size.height);
[img drawInRect:aRectangle];
[[UIColor redColor] set]; // set text color
NSInteger fontSize = 14;
if ( [text length] > 200 ) {
fontSize = 10;
}
UIFont *font = [UIFont boldSystemFontOfSize: fontSize]; // set text font
[ text drawInRect : aRectangle // render the text
withFont : font
lineBreakMode : UILineBreakModeTailTruncation // clip overflow from end of last line
alignment : UITextAlignmentCenter ];
UIImage *theImage=UIGraphicsGetImageFromCurrentImageContext(); // extract the image
UIGraphicsEndImageContext(); // clean up the context.
return theImage;
}
In the iOS7 notification centre, the labels (and the separator lines) have a very interesting background: the blur image, and what looks like the soft light blend mode.
I'm unsure what to search for. A pointer as to how this could be done would be really appreciated.
Till now, I've tried to replicate the effect by setting a part of the blurred image as the background using label.textColor = [UIColor colorWithPatternImage:...]. This also doesn't account for the case when the background is all black (or white), and leads to the text becoming unreadable.
But that doesn't seem to work just right.
Like this:
Here's what I've tried:
- (void)viewDidLoad
{
[super viewDidLoad];
const CGFloat fontSize = 25.f;
const NSString *text = #"A long-ish string";
CGSize size = [text sizeWithAttributes:#{NSFontAttributeName: [UIFont fontWithName:#"Avenir Next" size:fontSize]}];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(80, 270, size.width, size.height)];
label.font = [UIFont fontWithName:#"Avenir Next" size:fontSize];
label.textAlignment = NSTextAlignmentNatural;
label.backgroundColor = [UIColor clearColor];
label.text = text;
UIImage *image = [UIImage imageNamed:#"wat#2x"];
UIImage *blurredImage = [image applyBlurWithRadius:20.5 tintColor:[UIColor clearColor] saturationDeltaFactor:1.f maskImage:nil];
UIImageView *imageView = [[UIImageView alloc] initWithImage:[blurredImage applyDarkEffect]];
imageView.frame = self.view.bounds;
CGFloat imgScale = image.scale;
CGRect labelFrame = label.frame;
CGRect realRect = CGRectMake(labelFrame.origin.x * imgScale, labelFrame.origin.y * imgScale, labelFrame.size.width * imgScale, labelFrame.size.height * 2.0);
CGImageRef labelPatternImage = CGImageCreateWithImageInRect(image.CGImage, realRect);
label.textColor = [UIColor colorWithPatternImage:[UIImage imageWithCGImage:labelPatternImage scale:2.f orientation:UIImageOrientationUp]];
CGImageRelease(labelPatternImage);
[self.view addSubview:imageView];
[self.view addSubview:label];
}
This code results in
Code Result http://caughtinflux.com/static/result.png
As you can see, that isn't similar to the NC label.
EDIT
The blurred image background for the text should align with the actual background as much as possible. Hopefully the simulator screenshot of my code helps make sense of what I'm saying.
This is a blur effect. You can find Apple's category on UIImage with this effect available for download here. The files name is UIImage+ImageEffects.h/UIImage+ImageEffects.m any you can use it like that:
UIImage *backgImage = [image applyBlurWithRadius:2
tintColor:tintColor saturationDeltaFactor:0.8 maskImage:nil];
//Extended
You can create your view with the labels on it with lets say white text colour (to highlight in when you will blur whole view) and after that you can create snapshot of of this view and set up it as a background of the view you can use (by [self.view setBackgroundColor:[UIColor colorWithPatternImage:blurredImage];).
UIView *snapshotView = [YOURUIVIEW resizableSnapshotViewFromRect:self.contentView.frame afterScreenUpdates:YES withCapInsets:UIEdgeInsetsZero];
UIGraphicsBeginImageContextWithOptions( self.contentView.bounds.size, YES, 0.0f);
BOOL result = [snapshotView drawViewHierarchyInRect:self.contentView.bounds
afterScreenUpdates:YES];
UIImage *snapshotImage =
UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
if (result){
UIColor *tintColor = [UIColor colorWithWhite:0.97 alpha:0.82];
UIImage *blurredImage = [snapshotImage applyBlurWithRadius:4 tintColor:tintColor saturationDeltaFactor:1.8
maskImage:nil];
}
Let me know is it something you need. If it doesn't can you explain again, with more details, what you want to achieve?
Did you try adjusting the labels alpha value yet (as easy as it sounds)?
You could try that, and maybe add a bit of white to the blur before applying it to the label.
I have a sample project with notification center like separators, selected background views and (as of today) labels.
You can have a look at it here: https://github.com/mikrohard/BluredTableView
There may still be some bugs, perfomance issues, etc. But feel free to use and improve it.
I've tried setting the title text attributes. The Button is set by using an icon and then setting the title property. The icon is properly tinted. The title remains 'untinted'
Screenshot attached.
If tintColor does not affect the text color, the only solution I know of is to essentially create a composite image of the icon and text, then use that as your button image:
UIGraphicsBeginImageContextWithOptions(someSize, NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
// draw the image
// Use UIKit NSString additions to draw the text
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
David's suggestion works. Here is sample code:
+ (UIImage *) image:(NSString *) imageName withTitle:(NSString *)title {
UIFont *titleFont = [UIFont boldSystemFontOfSize:10];
CGSize textSize = [title sizeWithFont:titleFont];
CGFloat width = MAX(35, textSize.width);
UIGraphicsBeginImageContextWithOptions(CGSizeMake(width, 35), NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
UIImage *toolbarImage = [UIImage imageNamed:imageName];
CGContextSaveGState(context); {
CGContextTranslateCTM(context, 0, 35);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextDrawImage(context, CGRectMake((width - toolbarImage.size.width) / 2, 14, toolbarImage.size.width, toolbarImage.size.height), toolbarImage.CGImage);
}
CGContextRestoreGState(context);
[title drawInRect:CGRectMake((width - textSize.width) / 2, 22, textSize.width, textSize.height) withFont:titleFont];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
Tweak some of the frame parameters to fit your images.