How to change the color of the title in TTLauncherItem? - three20

I am having a lot of trouble trying to change the color in the TTLauncherItem, because the default gray color does no work with my background.
Any ideas?

Here's what I used to change the text color of TTLauncherItem from the default gray color to black (looks better on a white background):
(1) Create a Stylesheet that inherits from TTDefaultStyleSheet:
Stylesheet.h:
#interface StyleSheet : TTDefaultStyleSheet {}
#end
Stylesheet.m:
// Style for TTLauncherItems
- (TTStyle*)launcherButton:(UIControlState)state {
return
[TTPartStyle styleWithName: #"image"
style: TTSTYLESTATE(launcherButtonImage:, state)
next: [TTTextStyle
styleWithFont:[UIFont boldSystemFontOfSize:11]
color: RGBCOLOR(0, 0, 0)
minimumFontSize: 11
shadowColor: nil
shadowOffset: CGSizeZero
next: nil]];
}
(2) In AppDelegate.m, initialize the Stylesheet:
[TTStyleSheet setGlobalStyleSheet:[[[StyleSheet alloc] init] autorelease]];
That's it ... in the Stylesheet, change the UIFont and RGBCOLOR(0, 0, 0) to suit your requirements.

You can find the answer here:
http://groups.google.com/group/three20/browse_thread/thread/552d453dea748645
Basically you need to set a TTStyleSheet and perform all you customizations there.

Related

Can't change navigation bar color to white

I can make the navigation bar black or any other color but when I try white, it just makes it grey. For the curious, here's the code I'm using:
[[UINavigationBar appearance] setBarTintColor:[UIColor whiteColor]];
What's going on here?
Found the solution. Just setting the translucency off did the trick.
[[UINavigationBar appearance] setTranslucent:NO];
When I stop and think about it, it should have been obvious.
If you want to set the colour of a individual bar put the following in your .m file:
Creating the navigation controller:
UINavigationController *myNavigationController = [[UINavigationController alloc] initWithRootViewController:tableViewController];
Changing the color of the bar:
myNavigationController.navigationBar.backgroundColor = [UIColor blueColor];
setBarTintColor is a tint, meaning that it's tinted — which is why you see gray instead of white (if not overlaying white). If you want to fill the navigation bar with a solid color then try using setBackGroundColor:
[[UINavigationBar appearance] setBackgroundColor:[UIColor whiteColor]];
If you still wanted some control with the alpha values you could use:
[[UINavigationBar appearance] setBackgroundColor:[UIColor colorWithWhite:1.0 alpha:1.0]];
Beginning with iOS 7, navigation bars, tab bars, and tool bars
(henceforth referred to as 'bars') are configured to be translucent by
default. A translucent bar mixes its barTintColor with gray before
combining it with a system-defined alpha value to produce the final
background color that is used to composite the bar with the content it
overlies.
↳ UIColor Class Reference
I made new class CustomViewController.swift, like this:
import UIKit
class CustomViewController: UINavigationController {
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
let nav = self.navigationBar
nav.barStyle = UIBarStyle.black
nav.barTintColor = UIColor.white
//nav.tintColor = UIColor.white
nav.backgroundColor = UIColor.white
nav.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
}
}
It's not work till I remove in another class lines that overvrite my changes:
//nc.navigationBar.shadowImage = UIImage()
//nc.navigationBar.setBackgroundImage(#imageLiteral(resourceName: "bar_background"), for: .default)
For iOS13, the new way to customize navigation bar is to use UINavigationBarAppearance.
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let navBarAppearance = UINavigationBarAppearance()
navBarAppearance.backgroundColor = .white
// or navBarAppearance.configureWithTransparentBackground()
navigationController?.navigationBar.standardAppearance = navBarAppearance
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
let navBarAppearance = UINavigationBarAppearance()
navBarAppearance.configureWithDefaultBackground()
navigationController?.navigationBar.standardAppearance = navBarAppearance
}
Note:
.scrollEdgeAppearance will be used if your view contains a
scrollview and it’s scrolled at the top
.compactAppearance for
iPhone in landscape
.standardAppearance for the rest

How set font and color on nstextfield in cocoa?

I want set font on panel and change the selected font. I am using NSColorWell to open and select the color. For font, what can I use? How can I open the font panel and perform action when font panel is closed?
Currently I am using
'- (IBAction)Open_Font_Button:(id)sender
{
NSFontManager *fontManager = [NSFontManager sharedFontManager];
[fontManager setDelegate:self];
[fontManager setTarget:self];
[fontManager orderFrontFontPanel:self];
}
- (void)changeFont:(id)sender
{
font = [sender convertFont:font];
NSLog(#"%#", font);
}
'
but on chnageFont, when I change any font or its size it crashes.
I assume you have outlets to the ColorWell and textField connected:
IBOutlet NSColorWell *colorWell;
IBOutlet NSTextField *textfield;
You should set some things about the NSColorPanel:
[NSColor setIgnoresAlpha:NO];
[[NSColorPanel sharedColorPanel] setShowsAlpha:YES];
When you open or close a window that might display a color panel you should be sure you aren't left with a color panel hanging around:
if ([NSColorPanel sharedColorPanelExists])
{
[[NSColorPanel sharedColorPanel] close];
}
Then in your IBAction method for the color well you can get the color:
NSColor *color;
color = [colorWell color];
You can then set the font and color with:
[textField setFont:anNSFont *];
[textField setTextColor:color];
EDIT:
I just realized you're also asking how to get a new font from the font panel.
To get a new font from the font panel your code should actually work fine unless "font" (the old font) was never initialized. If font is null then [sender convertFont:font] will return null.
This prints null:
- (void)changeFont:(id)sender
{
NSFont *font;
font = [sender convertFont:font]; // Reset the font
NSLog(#"%#", font);
}
This prints a font:
- (void)changeFont:(id)sender
{
NSFont *font = [NSFont fontWithName:#"Helvetica" size:12]; // Initialize the old font
font = [sender convertFont:font]; // Reset the font
NSLog(#"%#", font);
}

Change border "glow" color of NSTextField

I have an NSText field in MainMenu.xib and I have an action set to validate it for an email address. I want the NSTexFields border color (That blue glow) to be red when my action returns NO and green when the action returns YES. Here is the action:
-(BOOL) validEmail:(NSString*) emailString {
NSString *regExPattern = #"^[A-Z0-9._%+-]+#[A-Z0-9.-]+\\.[A-Z]{2,4}$";
NSRegularExpression *regEx = [[NSRegularExpression alloc] initWithPattern:regExPattern options:NSRegularExpressionCaseInsensitive error:nil];
NSUInteger regExMatches = [regEx numberOfMatchesInString:emailString options:0 range:NSMakeRange(0, [emailString length])];
NSLog(#"%ld", regExMatches);
if (regExMatches == 0) {
return NO;
} else
return YES;
}
I call this function and setting the text-color right now, but I would like to set the NSTextField's glow color instead.
- (void) controlTextDidChange:(NSNotification *)obj{
if ([obj object] == emailS) {
if ([self validEmail:[[obj object] stringValue]]) {
[[obj object] setTextColor:[NSColor colorWithSRGBRed:0 green:.59 blue:0 alpha:1.0]];
[reviewButton setEnabled:YES];
} else {
[[obj object] setTextColor:[NSColor colorWithSRGBRed:.59 green:0 blue:0 alpha:1.0]];
[reviewButton setEnabled:NO];
}
}
}
I am open to sub-classing NSTextField, but the cleanest way to do this would be greatly appreciated!
My solution for Swift 2 is
#IBOutlet weak var textField: NSTextField!
...
// === set border to red ===
// if text field focused right now
NSApplication.sharedApplication().mainWindow?.makeFirstResponder(self)
// disable following focusing
textField.focusRingType = .None
// enable layer
textField.wantsLayer = true
// change border color
textField.layer?.borderColor = NSColor.redColor().CGColor
// set border width
textField.layer?.borderWidth = 1
The way to go about this is to subclass NSTextFieldCell and draw your own focus ring. As far as I can tell there's no way to tell the system to draw the focus ring using your own color, so you'll have to call setFocusRingType:NSFocusRingTypeNone, check if the control has first responder status in your drawing method, and if so draw a focus ring using your own color.
If you decide to use this approach remember that the focus ring is a user defined style (blue or graphite), and there's no guarantee future versions of OSX won't allow the user to change the standard color to red or green. It's also likely the focus ring drawing style will change in future versions of OSX, at which point you'll have to update your drawing code in order for things to look right.

How does +[NSColor selectedMenuItemColor] magically draw a gradient?

I'm implementing a custom NSMenuItem view that shows a highlight as the user mouses over it. To do this, the code calls NSRectFill after setting [NSColor selectedMenuItemColor] as the active color. However, I noticed that the result is not simply a solid color — it actually draws a gradient instead. Very nice, but wondering how this "magic" works — i.e. if I wanted to define my own color that didn't just draw solid, how would I?
I don't know how this actually works, but I found a way to replicate the behavior with custom gradients (or any other drawing operations). The "trick" is to use a CGPatternRef, which allows you to specify a callback function for drawing the pattern. Normally, this callback function draws one "cell" of the pattern, but you can just specify a very large pattern size (e.g. CGFLOAT_MAX) to be able to fill the entire area in one invocation of the callback.
To demonstrate the technique, here's a category on NSColor that allows you to create a color from an NSGradient. When you set that color and then use it to fill an area, the gradient is drawn (linear, from bottom to top, but you can easily change that). This even works for stroking paths or filling non-rectangular paths, like [[NSBezierPath bezierPathWithOvalInRect:NSMakeRect(0, 0, 100, 100)] fill] because NSBezierPath automatically clips the drawing.
//NSColor+Gradient.h
#import <Cocoa/Cocoa.h>
#interface NSColor (Gradient)
+ (NSColor *)my_gradientColorWithGradient:(NSGradient *)gradient;
#end
//NSColor+Gradient.m
#import "NSColor+Gradient.h"
#import <objc/runtime.h>
static void DrawGradientPattern(void * info, CGContextRef context)
{
NSGraphicsContext *currentContext = [NSGraphicsContext currentContext];
CGRect clipRect = CGContextGetClipBoundingBox(context);
[NSGraphicsContext setCurrentContext:[NSGraphicsContext graphicsContextWithGraphicsPort:context flipped:NO]];
NSGradient *gradient = (__bridge NSGradient *)info;
[gradient drawInRect:NSRectFromCGRect(clipRect) angle:90.0];
[NSGraphicsContext setCurrentContext:currentContext];
}
#implementation NSColor (Gradient)
+ (NSColor *)my_gradientColorWithGradient:(NSGradient *)gradient
{
CGColorSpaceRef colorSpace = CGColorSpaceCreatePattern(NULL);
CGPatternCallbacks callbacks;
callbacks.drawPattern = &DrawGradientPattern;
callbacks.releaseInfo = NULL;
CGPatternRef pattern = CGPatternCreate((__bridge void *)(gradient), CGRectMake(0, 0, CGFLOAT_MAX, CGFLOAT_MAX), CGAffineTransformIdentity, CGFLOAT_MAX, CGFLOAT_MAX, kCGPatternTilingConstantSpacing, true, &callbacks);
const CGFloat components[4] = {1.0, 1.0, 1.0, 1.0};
CGColorRef cgColor = CGColorCreateWithPattern(colorSpace, pattern, components);
CGColorSpaceRelease(colorSpace);
NSColor *color = [NSColor colorWithCGColor:cgColor];
objc_setAssociatedObject(color, "gradient", gradient, OBJC_ASSOCIATION_RETAIN);
return color;
}
#end
Usage example:
NSArray *colors = #[ [NSColor redColor], [NSColor blueColor] ];
NSGradient *gradient = [[NSGradient alloc] initWithColors:colors];
NSColor *gradientColor = [NSColor my_gradientColorWithGradient:gradient];
[gradientColor set];
NSRectFill(NSMakeRect(0, 0, 100, 100));
[[NSBezierPath bezierPathWithOvalInRect:NSMakeRect(100, 0, 100, 100)] fill];
Result:
My best guess is that it is defined as some sort of pattern image, however this does not fully answer my question because it looks as though these patterns would normally be drawn tiled rather than stretched.
This is corroborated by an Apple engineer's post on cocoa-dev which states:
[[NSColor selectedMenuItemColor] set];
NSRectFill(someRect);
This works because the selectedMenuItemColor is a pattern that happens
to draw a gradient. You could just as easily draw nearly anything with a pattern […]
He does not elaborate how these patterns can be drawn stretched instead of tiled, though, as the highlighted menu item background is. Another post in that thread claims it is a "special-case within the drawing code" but he may simply be speculating.

NSTextField transparent background

I create transparent NSTextField
self.myTextField = [[NSTextField alloc] initWithFrame:CGRectMake(backgroundView.frame.origin.x + backgroundView.frame.size.width + 20, self.projectTitle.frame.origin.y - 30.0, 100, 20)];
self.myTextField.editable = NO;
self.myTextField.bezeled = NO;
self.myTextField.drawsBackground = YES;
self.myTextField.backgroundColor = [NSColor clearColor];
self.myTextField.selectable = NO;
self.myTextField.font = [NSFont fontWithName:#"Helvetica Neue" size:16];
[self addSubview:self.compressingTime];
And as a result text look bad.
If I set background color
self.myTextField.backgroundColor = [NSColor colorWithCalibratedRed:0.85 green:0.85 blue:0.85 alpha:1.0];
everything looks ok
I have also tried with drawsBackground = NO; Do you guys know how to fix this?
The secret is setting ALL THREE of these properties on the NSTextField...
myTextField.bezeled = NO;
myTextField.editable = NO;
myTextField.drawsBackground = NO;
There is a property in the .xib file, on the interface builder window for the text field, under attribute inspector
Check the Display Draws Background
Select a background color. Select clear color for transparent background.
As of 10.12 you can just do:
let label = NSTextField(labelWithString: "HELLO")
Came here looking for this too, and have got the background to give me a transparent grey. Key is to not have a bezel. My code below:
NSTextField *yourLabel = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, width , height * 1.0/3.0)];
yourLabel.editable = false;
yourLabel.bezeled = false;
[yourLabel setTextColor:[NSColor blackColor]];
[yourLabel setBackgroundColor:[NSColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:0.1]];
For completeness I had got the width and height earlier because they get used many times for layout:
height = self.window.frame.size.height;
width = self.window.frame.size.width;
I ended up using CATextLayer instead NSTextField.
I have same problem. Default appearance is empty. I try set dark mode and it work.
self.nameTextField.appearance = [NSAppearance appearanceNamed:NSAppearanceNameVibrantDark];
I had this problem just now. I fixed it by removing a property named backgroundColor from the NSTextField's superview.
I was using backgroundColor just as a convenience getter/setter for the CALayer properties on an NSView subclass. Although this property isn't documented on NSView, it looks like I had accidentally overridden a property on NSView.
Yay for subclassing! 😒
The clear color will make the current view (ie)NSTextView's background as transparent hence the color of NSView which holds the NSTextView is visible.

Resources