UINavigationBar setTitleTextAttributes with UnderLine Text - ios

I am trying to setTextAttribute with UnderLine in all Views using this code
[[UINavigationBar appearance] setTitleTextAttributes:#{NSForegroundColorAttributeName : [UIColor redColor],
// NSUnderlineStyleAttributeName: #(NSUnderlineStyleSingle),
(NSString *) kCTUnderlineStyleAttributeName:#(kCTUnderlineStyleDouble),
NSFontAttributeName: [UIFont fontWithName:#"HelveticaNeue-Bold" size:17]}];
but its not working , I know another approach using creating custom UILabel with NSAttributedString and setting it on TitleView but is there any other way to achieve this using appearance protocol?

NSUnderlineStyleAttributeName should do that job for you.
Try this Dude:
NSDictionary *attributes = #{
NSUnderlineStyleAttributeName: #1,
NSForegroundColorAttributeName : [UIColor redColor],
NSFontAttributeName: [UIFont fontWithName:#"HelveticaNeue-Bold" size:17]
};
[[UINavigationBar appearance] setTitleTextAttributes:attributes];
Updates:
I refered the Apple's documentation, It seems to be not possible through appearance protocol.
It says
"You can specify the font, text color, text shadow color, and text shadow offset for the title in the text attributes dictionary, using the text attribute keys described in NSString UIKit Additions Reference."
I tried by creating the new simple project as well. Couldn't see the line.
Apple Documentations:
UINavigationBar
NSString Keys for Text Attributes Dictionaries

Related

Change UI color globally(Swift)

I want to do something like this:
In Setting of app, can chose a color, after that some UI element like Navigationbar, tabbar highlight will change to that color.
Is there anyway of tut for that?
Here's how you'd do it in Objective-C.
- (void)setCustomizedNavigationBarStyle {
// UIBarButtonItem styling
NSShadow *shadow = [[NSShadow alloc]init];
shadow.shadowOffset = CGSizeMake(0.0, 1.0);
shadow.shadowColor = [UIColor clearColor];
NSDictionary *enabledTextAttributeDictionary = #{NSForegroundColorAttributeName : [UIColor darkGrayColor], NSShadowAttributeName: shadow, NSFontAttributeName:[UIFont fontWithName:#"GillSans" size:17.0]};
[[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:#[[UINavigationBar class]]] setTitleTextAttributes:enabledTextAttributeDictionary forState:UIControlStateNormal];
NSDictionary *disabledTextAttributeDictionary = #{NSForegroundColorAttributeName : [UIColor lightGrayColor], NSShadowAttributeName: shadow, NSFontAttributeName:[UIFont fontWithName:#"GillSans" size:17.0]};
[[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:#[[UINavigationBar class]]] setTitleTextAttributes:disabledTextAttributeDictionary forState:UIControlStateDisabled];
// UINavigationBarTitle styling
NSDictionary *titleAttributeDictionary = #{NSForegroundColorAttributeName : [UIColor blackColor], NSShadowAttributeName: shadow, NSFontAttributeName:[UIFont fontWithName:#"GillSans" size:18.0]};
[[UINavigationBar appearanceWhenContainedInInstancesOfClasses:#[[UINavigationController class]]]setTitleTextAttributes:titleAttributeDictionary];
}
You could call this in didFinishLaunchingWithOptions. Once you translate to Swift, you'd add this line to didFinishLaunchingWithOptions:
setCustomizedNavigationBarStyle()
This should be readily translatable into Swift.
Adding to this, you can create a custom palette of colors. You may find this post on the topic helpful:
How do I create a category in Xcode 6 or higher?
You could save the color in NSUserDefaults and retrieve it by the key whenever you need to set that color to your view elements. You'll need an extension to NSUserDefaults that returns an UIColor.
Check out the accepted answer to this question.
Hope it helps!

Programmatically add a drop shadow to text in iOS

I define a title object with attributes for how the text will appear. Is there a way to also add a drop shadow?
Obj-C
_titleAttributes = #{
NSForegroundColorAttributeName: [UIColor whiteColor],
NSFontAttributeName: [UIFont fontWithName:#"Harabara" size:25.0f]
};

NSAttributedString Draws No Stroke with NSStrokeColorAttributeName and NSStrokeWidthAttributeName Set

This example below is supposed to draw a stroke together with fill but it does not. What is wrong? I am using negative value to have stoke showed up according to Apple's documentation. If I make it a positive value, then text completely disappears.
UITextView *rte = [[UITextView alloc]initWithFrame:
CGRectMake(50,50,100,100)];
[self.view addSubview:rte];
NSDictionary *typingAttributes = #{
NSFontAttributeName: [UIFont fontWithName:#"Helvetica-Bold" size:20.0f],
NSForegroundColorAttributeName : [UIColor redColor],
NSStrokeColorAttributeName : [UIColor yellowColor],
NSStrokeWidthAttributeName : [NSNumber numberWithFloat:-2.0]
};
NSAttributedString *str = [[NSAttributedString alloc]
initWithString:#"Enter text here..."
attributes:typingAttributes];
rte.attributedText = str;
I have tried your code in iOS 7.0.3 simulator and got the result:
It is not working in iOS 6. I think it is a bug.
This approach is working
Sorry I don't have any explanation why in iOS 6 it doesn't work.

Letterpress effect for UILabel in iOS 7 SDK

In the WWDC 2013 videos they show that a developer can use a letterpress effect on text.
Does anyone have any example code of how to do this/how to do this with a UILabel?
They've made it pretty simple now.
//Use any font you want or skip defining it
UIFont* font = [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline];
//Use any color you want or skip defining it
UIColor* textColor = [UIColor blueColor];
NSDictionary *attrs = #{ NSForegroundColorAttributeName : textColor,
NSFontAttributeName : font,
NSTextEffectAttributeName : NSTextEffectLetterpressStyle};
NSAttributedString* attrString = [[NSAttributedString alloc]
initWithString:note.title
attributes:attrs];
myTextLabel.attributedText = attrString;
It's part of NSAttributedString UIKitAdditions

NavigationBar title is coming in black color

I am using a UINavigationBar in my app with UITabBar.
On the first tab, the navigation bar title is coming properly as a white title with the default background color, but on the second tab it's not working in the same way. I'm not using any code to change the title color or the navigation bar's tintColor.
First view:
http://img407.imageshack.us/img407/7192/4go.png
Second View:
Why is the second view's UINavigationBar title drawn in this black color?
Generally, you can not change default color of UINavigationBar Title. In case of If you want to change color of UINavigationBar Title than you need to customize UINavigationBar. so put code for your second ViewController for more Understanding.
EDIT:
After searching, I found that You can change title color of UINavigationBar by
self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:UITextAttributeTextColor];
This code is working in iOS5 and later.
Most of the above suggestions are deprecated now, for iOS 7 use -
NSDictionary *textAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor],NSForegroundColorAttributeName,
[UIColor whiteColor],NSBackgroundColorAttributeName,nil];
self.navigationController.navigationBar.titleTextAttributes = textAttributes;
self.title = #"Title of the Page";
Also, checkout the NSAttributedString.h for various text properties that could be set.
Try this in AppDelegate:
[[UINavigationBar appearance] setTitleTextAttributes:#{NSForegroundColorAttributeName:[UIColor whiteColor]}];
This will allow you to change the colors
NSDictionary *navbarTitleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor],UITextAttributeTextColor,
[UIColor blackColor], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(-1, 0)], UITextAttributeTextShadowOffset, nil];
[[UINavigationBar appearance] setTitleTextAttributes:navbarTitleTextAttributes];
Use this bit of code for iOS7 as UITextAttributeTextColor has been deprecated.
self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor orangeColor] forKey:NSForegroundColorAttributeName];
This code changes the text of all the navigationBar, with this code the text can be customized 100%.
in appDelegate:
//custom text navBar
[[UINavigationBar appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:0x73/255.0 green:0x47/255.0 blue:0x41/255.0 alpha:1.0], UITextAttributeTextColor,
[UIColor colorWithRed:0x1D/255.0 green:0x1D/255.0 blue:0x1B/255.0 alpha:1], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, 1)],UITextAttributeTextShadowOffset,
[UIFont fontWithName:#"yourFont" size:20], UITextAttributeFont, nil]];

Resources