Shadow on UILabel does not appear - iphone

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);

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.

Custom UITableViewCell label color animation

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?

I am using UITextView on a scroll view ...and I want it to auto expand when I write something in it ......

textViewBusiness = [[UITextView alloc] initWithFrame:CGRectMake(25,332,268,60)];
textViewBusiness.text=strMyBusiness;
textViewBusiness.editable=NO;
textViewBusiness.font = [UIFont fontWithName:#"Arial" size: 17.0];
textViewBusiness.layer.borderWidth = 2.0f;
textViewBusiness.layer.borderColor = [[UIColor grayColor] CGColor];
textViewBusiness.backgroundColor = [UIColor clearColor];
[textViewBusiness setTextColor:[UIColor blackColor]];
[self.scrollView addSubview: textViewBusiness];
CGRect frame = textViewBusiness.frame;
frame.size.height = textViewBusiness.contentSize.height;
textViewBusiness.frame = frame;
with the increase in contents i want to increase the size of text field....
This code is not working for me ...
Thanks
You don't need to take UITextView for showing un-editable text. Besides this you can use UILabel and you can find out the height of label at run time. Each time the content vary in size set frame of UILabel accordingly.
Use this to find out height of your label at run time
- (CGFloat) heightOfTextLabel:(NSString *) contentText
{
CGSize constraint = CGSizeMake(268,4000);
CGSize size = [contentText sizeWithFont:[UIFont fontWithName:#"Arial" size: 17.0] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
return size.height;
}
This method would return you a variable height of the content each time.
Now set this height to your UILabel
CGFloat heightOfLabel = [self heightOfTextLabel:strMyBusiness];
UILabel* textToShowLabel = [[UILabel alloc] initWithFrame:CGRectMake(25,332,268,heightOfLabel)];
textToShowLabel.text=strMyBusiness;
textToShowLabel.font = [UIFont fontWithName:#"Arial" size: 17.0];
textToShowLabel.layer.borderWidth = 2.0f;
textToShowLabel.layer.borderColor = [[UIColor grayColor] CGColor];
textToShowLabel.backgroundColor = [UIColor clearColor];
[textToShowLabel setTextColor:[UIColor blackColor]];
[self.scrollView addSubview: textToShowLabel];
You need to divide your code into 2 fragments, then place them into proper places, then your code should work.
Fragment 1 (in my test, I place this fragment in viewDidLoad):
textViewBusiness = [[UITextView alloc] initWithFrame:CGRectMake(25,332,268,60)];
textViewBusiness.text=strMyBusiness;
textViewBusiness.editable=NO;
textViewBusiness.font = [UIFont fontWithName:#"Arial" size: 17.0];
textViewBusiness.layer.borderWidth = 2.0f;
textViewBusiness.layer.borderColor = [[UIColor grayColor] CGColor];
textViewBusiness.backgroundColor = [UIColor clearColor];
[textViewBusiness setTextColor:[UIColor blackColor]];
[self.scrollView addSubview: textViewBusiness];
Ensure that the above fragment run, and your text view is displayed in the screen, then run the second fragment. If your text view is not displayed in the screen, then the contentView is not initialized, and the height is undefined.
Fragment 2 (in my test, I place this fragment in viewDidAppear):
CGRect frame = textViewBusiness.frame;
frame.size.height = textViewBusiness.contentSize.height;
textViewBusiness.frame = frame;
Good luck!
GrowingTextView is a reusable iOS compenent that does exactly what you need, you can find it on GitHub here .
I hope this helps.

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

UISegmentedControl tint color on touch

I have a UISegmentedControl in my app (see code below) :
// --------------- SETTING NAVIGATION BAR RIGHT BUTTONS
NSArray *segControlItems = [NSArray arrayWithObjects:[UIImage imageNamed:#"up.png"],[UIImage imageNamed:#"down.png"], nil];
segControl = [[UISegmentedControl alloc] initWithItems:segControlItems];
segControl.segmentedControlStyle = UISegmentedControlStyleBar;
segControl.momentary = YES;
segControl.frame = CGRectMake(25.0, 7, 65.0, 30.0);
segControl.tintColor = [UIColor blackColor];
[segControl addTarget:self action:#selector(segAction:) forControlEvents:UIControlEventValueChanged];
if (current == 0) [segControl setEnabled:NO forSegmentAtIndex:0];
if (current == ([news count]-1)) [segControl setEnabled:NO forSegmentAtIndex:1];
// ---------------
But I can't make it to show something when you click on it ...
It functionnally works perfectly but I would like it to tint to gray when you click on it (but just when you click) ... would that be possible ?
Thank you,
Gotye.
Seems as if the selected segment's tint color is made darker than original tint. Therefore if your tint is black then the selected segment's tint color doesn't change since there isn't anything darker than that.
I've looked around and haven't found any nice way to control the selected segment's tint color for this control.
A recipe to create own segmented controls, which (I guess) can be configured as you want:
idevrecipes custom segmented control
BTW: A very very interesting website!
Try this. It works if you use segment titles, you may need to modify it if you use images.
segControl.segmentedControlStyle = UISegmentedControlStyleBar;
[segControl addTarget:self action:#selector(changeColors:) forControlEvents:UIControlEventValueChanged];
-(IBAction)changeColors:(UISegmentedControl *)sc{
for (int i=0; i<sc.numberOfSegments; i++) {
// reset all the titles in the segmented control
[sc setTitle:[NSString stringWithFormat:#"%i", i] forSegmentAtIndex:i];
}
if (sc.selectedSegmentIndex < 0 || sc.selectedSegmentIndex >= sc.numberOfSegments) {
return;
}
CGFloat width = sc.frame.size.width / sc.numberOfSegments - 1;
CGRect rect = CGRectMake(0, 0, width, sc.frame.size.height - 2);
UIGraphicsBeginImageContextWithOptions(rect.size, NO, [UIScreen mainScreen].scale);
CGContextRef ctx = UIGraphicsGetCurrentContext();
// background gradient
UIColor *colorOne = [UIColor colorWithWhite:0.6 alpha:1.0]; //top of gradient
UIColor *colorTwo = [UIColor colorWithWhite:0.4 alpha:1.0]; //bottom of gradient
NSArray *colors = [NSArray arrayWithObjects:(id)colorOne.CGColor, (id)colorTwo.CGColor, nil];
CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
CGGradientRef gradient = CGGradientCreateWithColors(space, (__bridge CFArrayRef)colors, NULL);
CGContextDrawLinearGradient(ctx, gradient, CGPointMake(0,0), CGPointMake(0,rect.size.height), 0);
// black shadow at the top
colors = [NSArray arrayWithObjects:(id)[UIColor colorWithWhite:0.0 alpha:0.3].CGColor, (id)[UIColor clearColor].CGColor, nil];
gradient = CGGradientCreateWithColors(space, (__bridge CFArrayRef)colors, NULL);
CGContextDrawLinearGradient(ctx, gradient, CGPointMake(0,0), CGPointMake(0,2.0), 0);
UILabel * title = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, sc.frame.size.height - 4)];
title.text = [sc titleForSegmentAtIndex:sc.selectedSegmentIndex];
title.font = [UIFont fontWithName:#"Helvetica-Bold" size:12.0];
title.textAlignment = UITextAlignmentCenter;
title.textColor = [UIColor whiteColor];
title.backgroundColor = [UIColor clearColor];
title.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
title.shadowOffset = CGSizeMake(0, -0.5);
[title.layer renderInContext:ctx];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[sc setImage:img forSegmentAtIndex:sc.selectedSegmentIndex];
}