Programmatically add a drop shadow to text in iOS - 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]
};

Related

UINavigationBar: Changing the font programmatically

I'm implementing a UINavigationBar with some text on it but I want to change the font in the UINavigationBar text any of you knows if is posible to change font in the text programmatically?
I'll really appreciate your help
From iOS 7 and later:
NSShadow* shadow = [NSShadow new];
shadow.shadowOffset = CGSizeMake(0.0f, 1.0f);
shadow.shadowColor = [UIColor redColor];
[[UINavigationBar appearance] setTitleTextAttributes: #{
NSForegroundColorAttributeName: [UIColor greenColor],
NSFontAttributeName: [UIFont fontWithName:#"Helvetica" size:20.0f],
NSShadowAttributeName: shadow
}];

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!

UINavigationBar setTitleTextAttributes with UnderLine Text

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

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

Resources