Custom UITableViewCell label color animation - iphone

I've added 3 UILabels to a custom UITableViewCell and I'm trying to animate the textColors of those UILabels.
The last thing I tried was this piece of code in the setSelected and/or the setHighlighted method:
UIColor *shadowColor;
UIColor *textColor;
if (highlightedOrSelected) {
shadowColor = [UIColor colorWithRed:74.0/255.0 green:33.0/255.0 blue:6.0/255.0 alpha:1.0];
textColor = [UIColor whiteColor];
} else {
shadowColor = [UIColor whiteColor];
textColor = [UIColor colorWithRed:74.0/255.0 green:33.0/255.0 blue:6.0/255.0 alpha:1.0];
}
[UIView animateWithDuration:animated ? 0.3 : 0.0
animations:^{
self.label1.shadowColor = shadowColor;
self.label1.textColor = textColor;
self.label2.shadowColor = shadowColor;
self.label2.textColor = textColor;
self.label3.shadowColor = shadowColor;
self.label3.textColor = textColor;
}];
The animation just fails, I also tried a hardcoded 10.0 duration, but still no animation.
What am I doing wrong and how do I get this to work?

Related

Glow around the sides of a rectangle

I am trying to create glow just around the sides of the rectangle. The rest of the rectangle should be filled clear color. I create a UIView with CGRect of certain size and then -
self.backgroundColor = [UIColor clearColor];
self.layer.borderWidth = 2.0f;
self.layer.borderColor = [UIColor colorWithRed:203/255.0 green:1.0 blue:252/255.0 alpha:1.0f].CGColor;
self.layer.cornerRadius = 5.0f;
int glowSpread = 2;
CGRect glowRect = CGRectMake(self.bounds.origin.x-glowSpread, self.bounds.origin.y-glowSpread, self.bounds.size.width+2*glowSpread, self.bounds.size.height+2*glowSpread);
self.layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:glowRect cornerRadius:5.0].CGPath;
self.layer.shadowColor = [UIColor colorWithRed:203/255.0 green:1.0 blue:252/255.0 alpha:1.0f].CGColor;
self.layer.shadowOffset = CGSizeMake(0, 0);
self.layer.shadowOpacity = 0.5;
self.layer.shadowRadius = 2.0f;
With the above approach, I get the glow around the side but the whole CGRect gets filled with the shadow color, which I do not want. Any suggestion?
make a more complicated shadow path that isn't enclosed, or just add a sublayer of the color you want.

how to select a random color from a method with label and color objects?

Trying to run random colors from the three defines color properties,
int num1 = rand() %3;
color1= [UIColor colorWithRed:0.5/num1 green:0.7/num1 blue:0.5/num1 alpha:1.0];
[self setColor:color1 setLabel:label1];
int num2 = rand() %3;
color2 = [UIColor colorWithRed:0.5/num2 green:0.7/num2 blue:0.5/num2 alpha:1.0];
[self setColor:color2 setLabel:label2];
int num3 = rand() %3;
color3= [UIColor colorWithRed:0.5/num3 green:0.7/num3 blue:0.5/num3 alpha:1.0];
[self setColor:color3 setLabel:label3];
trying to generate random colors from the color object, label object.
Want to put random() for the entire color variable not just for the properties. How can i do that. Thanks in advance
You should change rgb values randomly....
color1= [self getRandomColor];
[self setColor:color1 setLabel:firstColorObject];
color2 = [self getRandomColor];;
[self setColor:color2 setLabel:secondColorObject];
color3= [self getRandomColor];
[self setColor:color3 setLabel:thirdColorObject];
The method definition:
-(UIColor *)getRandomColor{
UIColor *color;
float randomRed = rand()%3;//3:you can write any number as you wish...
float randomGreen =rand()%2;//2:you can write any number as you wish...
float randomBlue =rand()%4;//4:you can write any number as you wish...
color= [UIColor colorWithRed:randomRed green:randomGreen blue:randomBlue alpha:1.0];
return color;
}
EDIT: (For ios)
-(NSDictionary *)randomColorAndLabel{
UIColor *color1= [UIColor colorWithRed:0.2 green:0.4 blue:0.3 alpha:1.0];
UIColor *color2= [UIColor colorWithRed:0.3 green:0.4 blue:0.3 alpha:1.0];
UIColor *color3= [UIColor colorWithRed:0.4 green:0.4 blue:0.3 alpha:1.0];
NSDictionary *colorDict1=#{#"color1" : color1};
NSDictionary *colorDict2=#{#"color2" : color2};
NSDictionary *colorDict3=#{#"color3" : color3};
NSArray *colors=#[colorDict1, colorDict2, colorDict3];
NSInteger randomNumber=arc4random()%3;
return colors[randomNumber];
}
which returns dictionary with color and name that you can use for label.
Earlier solved for OSX
I solved for OSX, with NSColor instead of UIColor and method name is changed as
colorWithRed:randomRed green:randomGreen blue:randomBlue alpha:1.0];
-(NSDictionary *)randomColorAndLabel{
NSColor *color1= [NSColor colorWithCalibratedRed:0.2 green:0.4 blue:0.3 alpha:1.0];
NSColor *color2 = [NSColor colorWithCalibratedRed:0.3 green:0.4 blue:0.3 alpha:1.0];
NSColor *color3= [NSColor colorWithCalibratedRed:0.4 green:0.4 blue:0.3 alpha:1.0];
NSDictionary *colorDict1=#{#"color1" : color1};
NSDictionary *colorDict2=#{#"color2" : color2};
NSDictionary *colorDict3=#{#"color3" : color3};
NSArray *colors=#[colorDict1, colorDict2, colorDict3];
NSInteger randomNumber=arc4random()%3;
return colors[randomNumber];
}
this returns dictionary with name and color.
-(UIColor*) randomColor {
CGFloat r = (float)(arc4random()%256) / 255.f;
CGFloat g = (float)(arc4random()%256) / 255.f;
CGFloat b = (float)(arc4random()%256) / 255.f;
return [UIColor colorWithRed:r green:g blue:b alpha:1.f];
}
Finlay I found solution for random colour
Just use below code
- (UIColor *)getRandomColor {
srand48(arc4random());
float red = 0.0;
while (red < 0.1 || red > 0.84) {
red = drand48();
}
float green = 0.0;
while (green < 0.1 || green > 0.84) {
green = drand48();
}
float blue = 0.0;
while (blue < 0.1 || blue > 0.84) {
blue = drand48();
}
return [UIColor colorWithRed:red green:green blue:blue alpha:1.0f];
}
Please check reference of colour

Shadow on UILabel does not appear

I want to add a little shadow to my UILabel, but it is not showing up.
companyLabel.textAlignment = UITextAlignmentLeft;
companyLabel.font = [UIFont fontWithName:#"Arial" size:13];
companyLabel.adjustsFontSizeToFitWidth = YES;
companyLabel.minimumFontSize = 10.0;
companyLabel.backgroundColor = [UIColor clearColor];
companyLabel.textColor = [UIColor colorWithRed:103.0/255.0 green:103.0/255.0 blue:103.0/255.0 alpha:1.0];
companyLabel.layer.shadowColor = [[UIColor colorWithRed:241.0/255.0 green:241.0/255.0 blue:241.0/255.0 alpha:1.0] CGColor];
companyLabel.layer.shadowOffset = CGSizeMake(0.0, -1.0);
companyLabel.highlightedTextColor = [UIColor whiteColor];
You're not setting shadowOpacity, which defaults to 0.0. You need to set that to something else to make the shadow show up. However, there's no reason to even touch CALayer right now because UILabel has its own shadowColor and shadowOffset properties.
companyLabel.shadowColor = [UIColor colorWithRed:241.0/255.0 green:241.0/255.0 blue:241.0/255.0 alpha:1.0];
companyLabel.shadowOffset = CGSizeMake(0.0, -1.0);

Large UIImage drawinrect extremely slow on selection

I have a custom UITableView cell that sports an Image, and a headline. I used the fast scrolling example of ABTableViewCell from atebits.
It seems when I select the first row (which is my row with the largest image) it takes a considerable amoun of time to highlight the row, and then push the new view controller.
However when I select any of the other rows, with much smaller images, its almost instant.
Ive attempted to draw the picture using CGContextDrawImage and drawInRect but both yield the same results.
- (void)drawContentView:(CGRect)r highlighted:(BOOL)highlighted {
if ([type isEqualToString:#"first"]) {
CGContextRef context = UIGraphicsGetCurrentContext();
UIColor *backgroundColor = [UIColor blackColor];
UIColor *textColor = [UIColor whiteColor];
UIColor *textBackgroundColor = [[UIColor alloc] initWithWhite:0 alpha:.5];
UIColor *highlightColor;
if (highlighted) {
backgroundColor = [UIColor clearColor];
textColor = [UIColor blueColor];
highlightColor = [[UIColor alloc] initWithWhite:0 alpha:.3];
} else {
highlightColor = [UIColor clearColor];
}
[backgroundColor set];
CGContextFillRect(context, r);
[self.picture drawInRect:CGRectMake(0.0, 0.0, 320, 225)];
[textBackgroundColor set];
CGContextFillRect(context, CGRectMake(0, 170, 320, 55));
[textColor set];
[self.title drawInRect:CGRectMake(5, 170, 320, 55) withFont:firstHeadlineFont lineBreakMode:UILineBreakModeWordWrap];
[highlightColor set];
CGContextFillRect(context, r);
} else {
CGContextRef context = UIGraphicsGetCurrentContext();
UIColor *backgroundColor = [UIColor blackColor];
UIColor *textColor = [UIColor blueColor];
if (highlighted) {
backgroundColor = [UIColor clearColor];
textColor = [UIColor whiteColor];
}
[backgroundColor set];
CGContextFillRect(context, r);
CGPoint p;
p.x = 0;
p.y = 0;
[self.picture drawInRect:CGRectMake(0.0, 0.0, 44, 44)];
[textColor set];
p.x += 44 + 6; // space between words
[self.title drawAtPoint:p withFont:[UIFont systemFontOfSize:20]];
}
}
EDIT:
IS there anything I can do to speed this up? I am caching the image and reading it as well.
I realized I was using a very very large version of the image for my first row, I was able to scale it down before I pulled it from the webserver and this vastly improved performance. NOTE: double check your dimensions are not huge. IE: 2184x1456 hahah

UILabel background color

I am trying to change the UILabel background color with this code
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
m_ShopName.text = m_CurrShop.m_Name;
m_ShopAddress.layer.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0].CGColor;
}
but nothing is happening.
This will help you
UILable *lbl = [[UILabel alloc]initWithFrame:CGRectMake(10,20,50,200)];
lbl.backgroundColor = [UIColor redColor];
Can you do this:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
m_ShopName.text = m_CurrShop.m_Name;
m_ShopAddress.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];
}
I always did the following
m_ShopAddress.backgroundColor = [UIColor red];
When I wanted to change the alpha...
m_ShopAddress.alpha = 0.0f;
An alpha value of 0 means, it's fully transparent. That's probably why nothings happens (whatever you mean by that).
And I wouldn't access the background color of the layer, but of the UILabel directly.
If you are using a storyboard, check to see if you have a color for the view background in the storyboard. The view background color was overriding the layer color for me. I changed the background color for the view in the storyboard to default and this fixed it for me.
So I had the code:
func select() -> Void {
imageViewBackgroundView.layer.backgroundColor = UIColor.black.cgColor
cellImageView.layer.backgroundColor = UIColor.black.cgColor
cellLabel.layer.backgroundColor = UIColor.black.cgColor
cellLabel.textColor = UIColor.white
}
The cellLabel was the view background that was not changing. I had at one time set a background color for the view in the Storyboard. Once I changed the view background for the cellLabel to default the layer color took effect.
View Background