TableView backgroundColor not changing - objective-c

I have a grouped tableView, and I am trying to change the default background to custom color. I have looked all over, and the closest thing to working is this:
- (void)viewDidLoad {
UIColor *backgroundColor = [UIColor colorWithRed:181 green:293 blue:223 alpha:0];
self.tableView.backgroundView = [[UIView alloc]initWithFrame:self.tableView.bounds];
self.tableView.backgroundView.backgroundColor = backgroundColor;
}
This code changes the background to white, but I can't get it to change to the custom color. Can someone help me out?

You are creating the color incorrectly. The RGBA values need to be in the range 0.0 - 1.0. UIColor will treat anything over 1.0 as 1.0. So your color is being set to white since all three RGB values will be treated as 1.0. Also note that an alpha of 0 means totally transparent. You want 1.0 to mean totally visible.
- (void)viewDidLoad {
UIColor *backgroundColor = [UIColor colorWithRed:181/255.0 green:293/255.0 blue:223/255.0 alpha:1.0];
self.tableView.backgroundView = [[UIView alloc]initWithFrame:self.tableView.bounds];
self.tableView.backgroundView.backgroundColor = backgroundColor;
}
Please note that your green value is 293. That needs to be changed to something from 0 to 255.

Your RGBA values should be 0.0 - 1.0.
Make sure alpha value should not be 0.0, to see the colour effect
UIColor *backgroundColor = [UIColor colorWithRed:0.7 green:1.0 blue:0.85 alpha:1.0];
self.tableView.backgroundView = [[UIView alloc]initWithFrame:self.tableView.bounds];
self.tableView.backgroundView.backgroundColor = backgroundColor;

Related

Objective-C drawRect Custom RGB Fill Color

I'm trying to fill a circle in Objective-C with a custom RGB color. Here is my drawRect method:
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
UIColor *color = [UIColor colorWithRed:10.0 green:131.0 blue:254.0 alpha:1.0];
CGContextSetStrokeColorWithColor(context, [color CGColor]);
CGContextSetLineWidth(context, 1.0);
CGContextAddEllipseInRect(context, [ball getRect]);
CGContextStrokePath(context);
}
Whenever I specify anything in place of [color CGColor] like greenColor it works fine, but with the above code nothing gets rendered (an invisible object). I know it is moving around the page because my update method is NSLog'ing a string for every update.
So, I want to fill and get the outer line of the circle to be the specified RGB color above. What am I doing wrong?
Thanks
[UIColor colorWithRed:green:blue:alpha] accept values between 0.0 to 1.0 (Apple Docs Reference) try changing the code like this:
UIColor *color = [UIColor colorWithRed:10.0/255.0 green:131.0/255.0 blue:254.0/255.0 alpha:1.0];
The value for each parameter in this method call:
[UIColor colorWithRed:10.0 green:131.0 blue:254.0 alpha:1.0];
should be between 0.0 and 1.0.

Drawing line through UITableView cells

Is it possible to draw a line with different width through some cells in UITableView?
Something like this:
I'm using Xcode 4.6 and target is iOS5+
Rough idea on how it might work. You'll need to manage the red line when the reusable cell content changes.
CGSize size = [cell.textLabel.text sizeWithFont:cell.textLabel.font];
CGFloat y = cell.contentView.frame.size.height / 2;
UIView *line = [[UIView alloc] initWithFrame:CGRectMake(5,y,size.width, 3)];
line.backgroundColor = [UIColor redColor];
[cell.textLabel addSubview:line];
make some lines(like image, etc) above the tableViewCell
like [cell.contentView addSubView:$LINE_IMAGE_VIEW]
you can get the label's width like this
CGFloat width = cell.titleLabel.frame.size.width
then assign that width to custom line image.
This will strike through the whole cell. In case you need it to look like completed task:
CGSize size = cell.contentView.frame.size; // you'll draw the line in whole cell.
UIView *line = [[UIView alloc] initWithFrame:CGRectMake(15,size.height / 2,size.width - 30, 1)];
line.backgroundColor = [UIColor grayColor]; // set your preferred color
[cell addSubview:line];
You may indicate some other value in CGRectMake instead of 15 - it's offset by X. In this case you'd better then decrease width by doubled value (in my case it's 15*2 = 30) in order it look nice.

Perspective transform on UILabel

I'm trying to add some perspective on a UILabel, to make it display as if it's painted over an angled face.
Here's my test label:
And after applying the transform:
My code:
#implementation MyViewController
- (void)viewDidLoad {
[super viewDidLoad];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100.f, 250.f, 100.f, 30.f)];
label.text = #"Hello";
label.textAlignment = NSTextAlignmentCenter;
label.backgroundColor = [UIColor greenColor];
[label tiltDegrees:20.f];
[self.view addSubview:label];
[label release];
}
#end
#implementation UIView (Perspective)
-(void) tiltDegrees:(CGFloat)degrees {
CATransform3D aTransform = CATransform3DIdentity;
CGFloat zDistance = 100; // affects the sharpness of the transform
aTransform.m34 = 1.0 / -zDistance;
aTransform = CATransform3DRotate(aTransform, degrees * M_PI / 180.0f, 1.0f, 0.0f, 0.0f);
self.layer.transform = aTransform;
}
#end
I'm stuck with a bunch of problems:
The text inside the label doesn't seem to apply the transformation in the same way as the UILabel itself does. It's out of frame (clipped), not centered, and the geometry doesn't seem right. Maybe size is wrong too?
It is blurred. How could I apply the transform on the text vector before it is rasterized. I think that would produce crisp results.
I can't seem to work out the coordination between zDistance and the degrees of rotation, so that they result in a correct perspective. How can I calculate zDistance correctly based on degrees?
Any help to solve (1) (2) (3) would be greatly appreciated.
Many Thanks
I don't know if you already solved this, but the problem is basically that your text label is passing through the gray layer behind it, hence why it seems to clip in the middle of the text.
Translate the text layer towards the camera to solve this problem.

Objective c pagecontrol background color

I have a scrollview with a pagecontrol, when i change her color, my pagecontrol dissapear.
UIColor *col = [UIColor colorWithRed:14.5 green:35.6 blue:72.9 alpha:1];
self.pageControl.backgroundColor = col;
The RGB values are supposed to be between 0 and 1. (See the docs). So you're setting the background color to fully opaque white here.
UIColor *col = [UIColor colorWithRed:14.5 green:35.6 blue:72.9 alpha:1];
If you have the RGB ASCII values 0 to 255
you can specify it as
UIColor *col = [UIColor colorWithRed:(255/255) green:(0/255) blue:(0/255) alpha:1]; // This represent Red color

Problem with NSColors in cocoa

i am trying to compose colors using NSColor and when i am trying to create RGB color with the following values it just displays the white colors instead:
(r,g,b):(50,50,50)
(r,g,b):(100,100,100)
(r,g,b):(150,150,150)
(r,g,b):(200,200,200)
etc...
the code used to create the colors is:
// the code to genearet simple images with background colors
NSColor * myColor = [NSColor colorWithDeviceRed:100.0 green:100.0 blue:100.0 alpha:1.0];
NSImage* image1 = [[NSImage alloc] initWithSize:NSMakeSize(10.0, 100.0)];
NSRect imageBounds1 = NSMakeRect (0, 0, 10, 100);
[image1 lockFocus];
[myColor set];
NSRectFill (imageBounds1);
[image1 unlockFocus];
I couldn't find any resource or sample on the web, which provides some sort of help on my above queries.It's highly appreciated if someone could share his wisdom on how I can achieve this.
Thanks in advance..
If I recall correctly you'll want the range 0-1 as your RGB as well
NSColor components have values in [0..1] so you should normalize the values you have, e.g.:
NSColor * myColor = [NSColor colorWithDeviceRed:100.0/255 green:100.0/255 blue:100.0/255 alpha:1.0];
If you try to set values greater then 1 to colour components then they're interpreted as 1, so your code will be actually equivalent to
NSColor * myColor = [NSColor colorWithDeviceRed:1.0 green:1.0 blue:1.0 alpha:1.0];
Which creates white colour.
As stated in the documentation:
Values below 0.0 are interpreted as 0.0, and values above 1.0 are interpreted as 1.0
This means that your values (100,100,100) are going to be converted in (1.0,1.0,1.0) which is white. What you have to do is convert each channel value using the following equation:
100 : 255 = x : 1.0 => x = 100/255
where x is the value that you will use for the method
-(NSColor*)colorWithDeviceRed:CGFloat red green:CGFloat green blue:CGFloat blue alpha:CGFloat alpha];
You should have something like this in your code
[NSColor colorWithDeviceRed:100.0/255.0 green:100.0/255.0 blue:100.0/255.0 alpha:1.0];