Animate UILabel to Change Colour? - ios

I would like to make the text in my UILabel called coloursLabel change text colour by itself. I have tried the following method as another SO answer suggested (albeit for the backgroundColor) but it still doesn't change. Am I missing something silly?
UILabel *coloursLabel = [[UILabel alloc] init];
coloursLabel.text = #"This sentence is colourful!";
[coloursLabel sizeToFit];
coloursLabel.center = self.view.center;
coloursLabel.frame = CGRectOffset(coloursLabel.frame, timeForPage(6), 0);
[UIView animateWithDuration:2.0 animations:^{
coloursLabel.textColor = [UIColor greenColor];
coloursLabel.textColor = [UIColor redColor];
coloursLabel.textColor = [UIColor blueColor];
coloursLabel.textColor = [UIColor yellowColor];
coloursLabel.textColor = [UIColor blackColor];
} completion:NULL];
coloursLabel shows the correct text but the text is in black.
Any help would be much appreciated!

UILabel's textColor is not animatable. You could write your own Label class, using a CATextLayer as mask and animate its layer's backgroundColor.

Related

PNG image displays half transparent in ios

I have subclassed an UITextField, so that there is an icon next to the text field, here is the code:
iconView = [UIImageView new];
self.textField = [UITextField new];
self.textField.borderStyle = UITextBorderStyleNone;
self.textField.textColor = [UIColor whiteColor];
self.layer.cornerRadius = 5;
self.layer.borderWidth = 1;
self.layer.borderColor = TEXTFIELD_BORDER_COLOR;
self.backgroundColor = [UIColor clearColor];
self.textField.backgroundColor = [UIColor clearColor];
//iconView.backgroundColor = [UIColor clearColor];
[self.textField setTranslatesAutoresizingMaskIntoConstraints:NO];
[iconView setTranslatesAutoresizingMaskIntoConstraints:NO];
[self setTranslatesAutoresizingMaskIntoConstraints:NO];
[self addSubview:iconView];
[self addSubview:self.textField];
[self setUpConstraints];
When I set an image, which doesn't have any transparency inside, everything works fine, but when I set with transparency I get faint, half visible image.
Here are two examples of image, first displays fine, second is barely visible:
The problem is with your image.
In the first image (the person one), the gray colored area is opaque. And in the second image (the lock), the gray colored area is almost transparent (opacity is at 31%).

IOS drawing text inside a subview

im trying to draw a text on a subview, the view appearing fine but the text not, this is what im doing, the view have black color, the text have white color, text and rect value given in other method:
NSString* sumText = [[NSString alloc]initWithFormat:#"%0.1f",sum];
CGRect textRect = CGRectMake(chartLocation.x+chartLength+10,
chartLocation.y,
20,
20);
[self drawTextRect:sumText inRect:textRect];
-(void)drawTextRect:(NSString *)sumText inRect:(CGRect)textRect {
UIFont* font=[UIFont fontWithName:#"Arial" size:(20/2)];
UIColor* textColor = [UIColor whiteColor];
NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle]mutableCopy];
paragraphStyle.alignment = NSTextAlignmentCenter;
NSDictionary* stringAttr=#{NSFontAttributeName:font,NSForegroundColorAttributeName:textColor,NSParagraphStyleAttributeName:paragraphStyle};
UIView* textView =[[UIView alloc] initWithFrame:textRect];
textView.backgroundColor = [UIColor blackColor];
[sumText drawInRect:textRect withAttributes:stringAttr];
[self.view addSubview:textView];}
am i do anything wrong? thanks
The drawInRect:withAttributes: method draws the string in the current graphics context. You have not defined a context, so your drawing goes nowhere.
If you want to draw the string yourself, you should subclass UIView, add the required properties (sumText), and overwrite drawRect:. A graphics context is automatically created for you in drawRect: so you can just go ahead and draw.
But really, you probably just want to use a UILabel instead.
there is another way you can achieve what you want .
UILabel* lblText = [[UILabel alloc]initWithFrame:CGRectMake(50, 50, 200, 200)];
lblText.text = #"Hello World!";
lblText.backgroundColor = [UIColor blackColor];
lblText.textColor = [UIColor whiteColor];
[self.view addSubview:lblText];
And Output is:

UITextView Background Color

I am trying to get white text with a black background or transparent background for the UITextView in Xcode 5. There is no slider or setting for this in Xcode, must have been one in previous versions from what I can see in the questions here. but not anymore. I also have seen this
UItextView.backgroundColor = [UIColor clearColor];
as a way to fix it. But not sure where to put this as, if its even right. Thanks
Sure you can do it programmatically:
UITextView *txt = [[[UITextView alloc] initWithFrame: CGRectMake(10, 10, 100, 60)] autorelease];
[txt setBackgroundColor: [UIColor clearColor]];
[txt setTextColor: [UIColor whiteColor]];
but you can also accomplish this via the Attribute inspector of the graphic editor setting respectively the properties Color to 'White Color' and Background to 'Clear Color'.
This is the result with a custom background:
You should be write/set any property of your UITextView after initialization
_myTextViewName.backgroundColor = [UIColor clearColor]; // set clear backGround color or set as you want
_myTextViewName.textColor = [UIColor whiteColor]; // set white text color or set as you want
You can try this
UItextView *yourTextView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0 ,320, 50)];
yourTextView.backgroundColor = [UIColor clearColor];
yourTextView.textColor = [UIColor whiteColor];
[self.view addSubView:yourTextView];
For swift:
#IBOutlet weak var textField: UITextField!
textField.backgroundColor = UIColor.clearColor()

iOS 6 navigation bar not centering correctly

I have a problem with my navigation bar. I'm setting a custom font, and the centering is not right, the back button it's moving everything to the right.
UILabel *navLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0, 300, 100)];
navLabel.backgroundColor = [UIColor clearColor];
navLabel.text = dataFromOtherView.text;
navLabel.textColor = [UIColor whiteColor];
navLabel.shadowColor = [UIColor colorWithWhite:0.0 alpha:1.0];
navLabel.shadowOffset = CGSizeMake(0, 0);
navLabel.font = [UIFont fontWithName:#"Trebuchet MS" size:22.0];
navLabel.textAlignment = NSTextAlignmentCenter;
self.navigationItem.titleView = navLabel;
It looks like the label is so large that there is not enough 'open/flexible space' between the left side of the label and the right side of the back button. Try reducing the width of UILabel when you create it.

Changing the background color of a UILabel within a UITableViewCell

A UITableViewCell comes "pre-built" with a UILabel as its one and only subview after you've init'ed it. I'd really like to change the background color of said label, but no matter what I do the color does not change. The code in question:
UILabel* label = (UILabel*)[cell.contentView.subviews objectAtIndex:0];
label.textColor = [UIColor whiteColor];
label.backgroundColor = [UIColor darkGrayColor];
label.opaque = YES;
Your code snippet works fine for me, but it must be done after the cell has been added to the table and shown, I believe. If called from the initWithFrame:reuseIdentifier:, you'll get an exception, as the UILabel subview has not yet been created.
Probably the best solution is to add your own UILabel, configured to your standards, rather than relying on this (very rickety) path to the built-in one.
This doesn't work because the UITableViewCell sets its label backgroundColors in the layoutSubviews method.
If you want to change the color of the built-in textLabel or detailTextLabel, subclass the UITableViewCell and override layoutSubviews. Call the super implementation, THEN change the backgroundColor property to what you want.
- (void) layoutSubviews
{
[super layoutSubviews];
self.textLabel.backgroundColor = [UIColor redColor];
}
Add your own label to the contentView when you are allocating the cell, rather than relying on the extraction of the built in one. Then you can control all values:
UILabel* label = [[[UILabel alloc] init] autorelease];
label.textColor = [UIColor whiteColor];
label.backgroundColor = [UIColor darkGrayColor];
label.opaque = YES;
[cell.contentView addSubview:label];
for (UIView *views in views.subviews)
{
UILabel* temp = (UILabel*)[views.subviews objectAtIndex:0];
temp.textColor = [UIColor whiteColor];
temp.shadowColor = [UIColor blackColor];
temp.shadowOffset = CGSizeMake(0.0f, -1.0f);
}

Resources