I have a custom UILabel class which is used in my mainViewController.
I have a settings screen and based on the colour chosen in the settings screen, all the UILabels used in the mainViewController will have to update the colour chosen.
Lets say my UILabel colour is black in home screen,I go to settings screen and change the colour to white, and when I come back to my home screen I want all the subclassed labels to update the label colour to white.
I want to update this colour in my customUILabel implementation class and not in the mainViewController, as this label is getting used in 100 other places in my application. I need a single point code change to update the colours.
I have tried using setNeedsDisplay in the viewWillAppear of my viewController, It doesn't work!
Any thoughts in getting this implementation working?
When u change the color you set the color's name in NSString and save it in NSUserDefaults.
To save it:
NSString *colorName = K_COLOR_RED;
[[NSUserDefaults standardUserDefaults] setValue:colorName forKey:K_KEY_COLOR_LABEL];
To retrieve it:
[[NSUserDefaults standardUserDefaults] stringForKey:K_KEY_COLOR_LABEL];
The color K_COLOR_RED and K_KEY_COLOR_LABEL must exist in a Global.h!!
This work fine for me!
Related
I've searched around for some code on NSSliderCell but I can't find a way to do what I'm looking for.
I'd like to create something like this (where the white line is the slider knob, 1 pixel width):
I'm going to use this for the time bar of a music track, so it's going to move every second (that's why I want to use a NSSlider to make things easy).
What do I need to do to make a slider, with a transparent middle bar, similar to the image above?
PS: It's not going to be touchable, it's just for display.
You can just override drawRect:, as when subclassing NSView, except in your implementation, use [self doubleValue] to determine where to draw the line. However, I don't see the point of subclassing NSSlider in this case. A custom NSView could have a property that determines where to draw the line, and then other code could set that property or bind to it.
That looks like a vertical split view to me with a 1 pixel wide divider. You might try that. There's a method to set the position of the divider so it would be easy to move as you need. And you can make the divider 1 pixel by creating a subclass of NSSplitview and overriding the dividerThickness method to return 1. Then you just set the background of the 2 subviews to black and there you go. It's easy to try so maybe it will work for you. Good luck.
I finally got it:
I created a NSSliderCell subclass with a property #property float x;
I overrode the drawKnob method and inside it I wrote:
-(void)drawKnob:(NSRect)knobRect{ self.x = knobRect.origin.x; }
I dragged a NSSlider into my window (made it small, changed it's width to the window's width) and changed it's cell class to the one I created;
And then when the music is playing, every time a second goes by I do:
[_timeBarSlider setMinValue:0];
[_timeBarSlider setMaxValue:myTrack.duration];
[_timeBarSlider setDoubleValue:myPlayer.currentPosition];
[[_timeBarImageView animator] setFrame:NSMakeRect(_timeBarSliderCell.x, yourYCoordinate, yourWidth, yourHeight)];
_timerBarSlider is the NSSlider I have in IB / _timerBarImageView is the image view that contains the vertical image line / _timerBarSlderCell is the NSSlider's cell (subclassed)
PS: the NSSlider is behind every object in that window, so that the user can't see it. You can't setHidden:YES on it because the drawKnob method will not be called.
I'm working on an interface prototype and am using a storyboard to do so. Part of the prototype involves setting the detail UILabel for UITableView cells to a certain color. I would like to avoid having to manually recolor every label within a storyboard.
I found that I can use:
[[UILabel appearanceWhenContainedIn:[UITableViewCell class], nil]
setTextColor:[UIColor cyanColor]];
To change the appearance of labels within tableview cells. Is there a way to further refine this code to only apply to detail tableview labels? Currently it changes both textLabel and detailTextLabel of UITableViewCell.
Thank you!
You could trick around this by subclassing the cells in the detail view, then use
[[UILabel appearanceWhenContainedIn:[YOUR_UITableViewCell class], nil]
setTextColor:[UIColor other_colr]];
If you want to keep using the appearance proxy, you will have to create a custom table view cell and a new label.
In the storyboard editor you set the style property of your UITableCell to "custom".
Drag two label to the table view cell and configure it like you want it.
Create a new class that inherits from UITableViewCell and make IBOutlets for every label.
In the storyboard editor you set the class of the table view cell to the table view cell class you just created. Connect the outlets to the labels. Actually this is enough, because you can configure the appearance of the cells in the storyboard as well. If you want to configure the label through code you have to execute the following steps as well.
Create a new class that inherits from UILabel and leave it as is.
Go back to the storyboard editor and select the detailtextlabel and change the class to the label class you just create.
If you want more information on how to create your own table view cells see Table View Programming Guide for IOS
I use UITableView's cellForRowAtIndexPath to display the data (NSString*) by assigning cell.textLabel.text = [list objectAtIndex:indexPath.row];
Everything is fine but I need to color certain lines, so I use cell.textLabel.backgroundColor and cell.textLabel.textColor. The colored lines look a bit ugly - they have blank space at the beginning and at the end (both are roughly 1 char wide). Maybe these are reserved for scrolling marker (up/down)? Is there a way to use the whole width of the cell while keeping scrolling ability up? Any special style or flag? Thanks. Victor
The label does not fill the entire cell, so setting its background colour will not cover everything.
Set the cell's background colour instead, but this has to be done in the willDisplayCell: delegate method, not cellForRowAtIndexPath: (see the docs for details: http://developer.apple.com/library/ios/DOCUMENTATION/UIKit/Reference/UITableViewCell_Class/Reference/Reference.html#//apple_ref/occ/cl/UITableViewCell
i've done a feed reader with a UITableView structure, i would to change the UITextLabel TextColor after an user click on it (like reeader app)
could someone help me?
thank you,
devskip
If you're using a built-in table view cell style, all you have to do is set the highlightedTextColor of the cell's labels. Then, when the cell is highlighted, the labels will change color as appropriate.
If you're using a custom UITableViewCell subclass, you can still set the highlightedTextColor, but you must also override setHighlighted:animated: to update the highlighted property of any labels.
I am new to iPhone programming and working on my first real application (i.e. one not written in a book or online) and I've run into a small problem which I could solve a multitude of ways, but feel like there should be a good solution that perhaps I am just missing.
Here is the scenario: I have a UITableView with a bunch of standard UITableViewCells in it. What I want to do is toggle a green check mark when the cell is selected and I have that part working (note: I'm already using the accessoryType for something else, so I can't use it for the checkmark...besides, it's not as pretty). Unfortunately, when I toggle the checkmark like so:
if (...) {
cell.imageView.image = [UIImage imageNamed:#"checkmark.png"];
} else {
cell.imageView.image = nil;
}
It makes the cell's label bounce back and forth depending on whether it is checked or not. What is the proper way to align the cell's text (set via cell.textLabel.text) regardless of whether or not it has an image set? The solutions I have come up with are:
Create a blank 40x40 png image in Photoshop and set the unchecked to that
Create a blank 40x40 image solely in code
Set some setting that I don't know about that will align it for me
Create a subclass of UITableCellView that does what I need (which would be stupid, I'd just go with option 1...)
Suggestions? Thoughts? Comments? Thank you very much :-)
P.S. I'd like the solution to work with OS 3.0 and 4.0 if that makes any sort of difference.
Option 1 is the way to go for a standard cell.
Strictly speaking, you do not have to subclass UITableViewCell to customize the layout. You can add any views you wish to the contentView. So you can add a UILabel and UIImageView to the contentView instead of using the imageView and textLabel properties.