Adding shadow to the title of the NavBar - objective-c

I got a program from a colleague and he is using .xib files to setup his view.
(being a hard core coder, I never used IB myself...)
So he is setting up his ViewControler and he has a NavBar (that I could not find in the .xib file!).
I want to add drop shadows to the title.
If I was making the NavBar, the relative code would be something like:
textViewTitle.layer.shadowOpacity = 2.0;
textViewTitle.layer.shadowRadius = 3.0;
textViewTitle.layer.shadowOffset = CGSizeMake(2.0, 3.0);
and that works fine.
Digging around I found that now I need to use this:
NSDictionary *navbarTtlAts = [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], UITextAttributeTextColor,
[UIColor blackColor], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(2.0, 3.0)], UITextAttributeTextShadowOffset,
nil];
[self.navigationController.navigationBar setTitleTextAttributes:navbarTtlAts];
Although this works, I could not find how to add the shadowOpacity & shadowRadius stuff.
I found that I could add the following in the NSDictionary, but it did not work...
[NSNumber numberWithFloat: 2.0], #"shadowOpacity",
[NSNumber numberWithFloat: 3.0], #"shadowRadius",
Any ideas?

You should use these keys for the shadow color and offset.
UITextAttributeTextShadowColor
UITextAttributeTextShadowOffset
There is no way to specify the shadow radius as a text attribute.
If you really wanted a custom shadow you could create your own UILabel and set the shadow like in your first example. Then you would set that label as the titleView of your view controllers UINavigationItem.
You would have to update the text yourself though (the navigation controller would no longer do it for you automatically).

Related

Why doesn't this code draw white text?

This code doesn't draw white text, why?
NSMutableParagraphStyle* style = [[NSMutableParagraphStyle alloc] init];
[style setAlignment:NSCenterTextAlignment];
NSFont *font = [NSFont fontWithName:#"System" size:13];
NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:style, NSParagraphStyleAttributeName, font, NSFontAttributeName, [NSColor whiteColor], NSForegroundColorAttributeName, nil];
[button.title drawInRect:textRect withAttributes:attrs];
(Assuming the [cocoa] tag doesn't mean cocoa touch)
It's because NSButton is likely overriding the choice you've made to draw it's text in when it's -drawRect: gets called again. You can apply the attributes you've given in that dictionary to an NSAttributedString and call setAttributedTitle: to keep your style choices around.
If you need more fine-grain control over text rendering, either edit and move your logic into -drawRect: if it isn't already there, or provide an NSTextField or NSTextView as appropriate.
The main problem with the code you've provided is that #"System" isn't a font name.

NSTextView with shadow

I'm trying to add a nice looking shadow to a NSTextViews string, I have this Code so far:
NSShadow *textShadow = [[NSShadow alloc] init];
textShadow.shadowColor = [[NSColor blackColor]
colorWithAlphaComponent:0.3];
textShadow.shadowOffset = NSMakeSize(5.0, -5.0);
textShadow.shadowBlurRadius = 3;
NSDictionary *d = #{NSShadowAttributeName : textShadow,
NSFontAttributeName : [NSFont fontWithName:#"Arial Black" size:36.0],
NSStrokeWidthAttributeName : [NSNumber numberWithFloat:-3.0],
NSStrokeColorAttributeName : [NSColor whiteColor]};
[tv setTypingAttributes:d];
all in all this brings up a pretty looking Drop shadow on the right and the bottom of the string in the NSTextView but because the internal drawing mechanism of the textview seems to draw the "fill" of the Characters first and then the stroke around it, the Shadow lays above the fill of the text in the upper left of the Chars, which looks very bad as you can see here(would post an Image but not enough reputation right now 8-/ )
Is there a better way to add the shadow or a way to "raise" the fill color of the String so it lays above the shadow or is this kind of a Bug in the Foundation framework?
Thanks and greetings,
Alex.

Adding two right bar button items to the navigation bar

I have a navigation bar to which I have added right BarButton successfully. Both the NavigationBar and BarButton are created programmatically. Now according to my requirement I got to add two right BarButtons to my navigation Bar. Can anyone tell me how to do this? My app is targeting ios4.
This code will do the trick for you,
NSArray *barButtonItems= [[NSArray alloc] initWithObjects:self.addButton,self.sortbyButton,nil];
self.navigationItem.rightBarButtonItems=barButtonItems;
where addButton and sortbyButton are 2 separate BarButton Items
I know it is too late but I faced it recently. So here is what I did
Create a UIView in code and add the buttons as subviews into this view.
Create a ToolbarButton using [[UIBarButtonItem alloc] initWithCustomView:buttons]
Assign this toolbar button as the Left or right barbuttonItem as U wish.
If you application is targeting iOS 4 and above then you should take UISegmentControl and have two segments on it. Catch value changed action event and check which segment is selected and do you operation accordingly.
You can also set images to segments to make look and feel better.
As the documentation to UINavigationItem1 describes, it has a property rightBarButtonItems (as well as leftBarButtonItems) where you can give an array of UIBarButtons. They are display from right (index 0) to left (index n-1).
#Matthias, As stated in documentation, rightBarButtonItems property is available from iOS 5 and above and this function needs to be supported also on iOS 4.
So, UISegmentControl is best way to achieve this.
NSArray *segmentTextContent = [NSArray arrayWithObjects:
NSLocalizedString(#\"Group By\", #\"\"),
NSLocalizedString(#\"Filter By\", #\"\"),
nil];
UISegmentedcontrol *segmentedControl = [[UISegmentedControl alloc] initWithItems:segmentTextContent];
segmentedControl.selectedSegmentIndex = 0;
segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleWidth;
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.frame = CGRectMake(0, 0, 125, 30);
[segmentedControl addTarget:self action:#selector(toggleUnit) forControlEvents:UIControlEventValueChanged];
segmentedControl.tintColor = [UIColor lightGrayColor];
defaultTintColor = [segmentedControl.tintColor retain];
self.navigationItem.rightBarButtonItem = segmentedControl;
[segmentedControl release];

Changing Text Color of Unselected UITabBarItems

Here's what I've tried:
I've tried the following in the parent view controller:
[self.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], UITextAttributeTextColor,
nil] forState:UIControlStateNormal];
I've tried the following in the application delegate:
[[UITabBarItem appearance]
setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], UITextAttributeTextColor,
nil]
forState:UIControlStateNormal];
I've even tried changing UIControlStateNormal to any of the other available constants. The only one that changes anything is UIControlStateHighlighted which changes the color of the highlighted tab. Is this a bug in the API or is there something I'm missing?
Key things to note:
I'm using storyboarding
I have a UITabBarController where each tab has an embedded UINavigationController (pretty standard setup)
I've tried embedding the first code snippet into both the UINavigationController subclass as well as the root UIViewController subclass that is inside the UINavigationController. No luck there either.

AQGridView iOS Create custom icons (image+label) instead of image

I've just started using AQGridView from Alan Quatermain more specifically, the SpringBoard demo..
The demo shows icons/tiles that consist of a UIImageView. I'd like to know how to use a custom UIView instead so that I can have an icon with text/button underneath.
I'm currently trawling through the code with no real luck in finding how my icons are created.
Any ideas/suggestions would be greatly appreciated..
Cheers.
Check out AQGridViewCell and, specifically, contentView property. This is similar to customizing UITableViewCell.
Have you checked out three20's TTLauncherView?
Although it is for the iPhone you might be able to adopt it for the iPad. It allows for an icon with text underneath. Though you might have to hack it for inserting a custom UIView in place of the image and text.
I found iOSGuy's tutorial helpful when working with TTLauncherView.
And here is an excerpt from iOSGuy's tutorial to show little bit about how it is setup/used.
TTLauncherView* launcherView = [[TTLauncherView alloc]
initWithFrame:self.view.bounds];
launcherView.backgroundColor = [UIColor blackColor];
launcherView.columnCount = 4;
launcherView.pages = [NSArray arrayWithObjects:
[NSArray arrayWithObjects:
[self launcherItemWithTitle:#"Google"
image:#"bundle://safari_logo.png"
URL:#"http://google.com"],
[self launcherItemWithTitle:#"Apple"
image:#"bundle://safari_logo.png"
URL:#"myAppController/myView"]
, nil]
, nil];