I am trying to addapt my code to iOS 7.
[[UIBarButtonItem appearance] setTitleTextAttributes:#{
UITextAttributeTextColor: [UIColor colorWithRed:214.0f/255.0f green:210.0f/255.0f blue:197.0f/255.0f alpha:1.0f],
UITextAttributeTextShadowColor: [UIColor colorWithWhite:0.0f alpha:0.750f],
UITextAttributeTextShadowOffset: [NSValue valueWithCGSize:CGSizeMake(0.0f, 1.0f)]
I am getting a few errors, UITextAttributeColor is deprecated, UITextAttributeTextShadowColor is deprecated, and UITextAttributeTextShadowOffset is deprecated.
NSShadow *shadow = [NSShadow new];
[shadow setShadowColor: [UIColor colorWithWhite:0.0f alpha:0.750f]];
[shadow setShadowOffset: CGSizeMake(0.0f, 1.0f)];
[[UIBarButtonItem appearance] setTitleTextAttributes:#{
NSForegroundColorAttributeName: [UIColor colorWithRed:214.0f/255.0f green:210.0f/255.0f blue:197.0f/255.0f alpha:1.0f],
NSShadowAttributeName: shadow]
}];
NSShadow *shadow = [NSShadow new];
[shadow setShadowColor : [UIColor colorWithWhite:0.0f alpha:0.750f]];
[shadow setShadowOffset : CGSizeMake(0.0f, 1.0f)];
[[UITabBarItem appearance] setTitleTextAttributes:
#{
NSFontAttributeName : [UIFont fontWithName:#"AmericanTypewriter" size:10.0f],
NSForegroundColorAttributeName : [UIColor grayColor],
NSShadowAttributeName: shadow
}
forState:UIControlStateNormal];
[[UITabBarItem appearance] setTitleTextAttributes:
#{
NSFontAttributeName : [UIFont fontWithName:#"AmericanTypewriter" size:10.0f],
NSForegroundColorAttributeName : [UIColor blackColor],
NSShadowAttributeName : shadow
}
forState:UIControlStateSelected];
UIColor *blue = [UIColor colorWithRed:64.0/255.0
green:119.0/255.0
blue:255.0/255.0
alpha:1.0];
NSShadow *shadow = [NSShadow.alloc init];
shadow.shadowColor = [UIColor clearColor];
NSDictionary *attributes = #{
NSForegroundColorAttributeName: blue,
NSShadowAttributeName: shadow
};
[[UIBarButtonItem appearance] setTitleTextAttributes:attributes
forState:UIControlStateNormal];
Related
How can I recreate the letterpress-like effect applied to the backbarbuttonitem in the notes app in ios 7? I tried the following:
NSShadow *textShadow = [[NSShadow alloc] init];
textShadow.shadowOffset = CGSizeMake(0.0, -1.0);
textShadow.shadowColor = [UIColor blackColor];
NSAttributedString *attributedTitle = [[NSAttributedString alloc] initWithString:#"Back" attributes:#{NSForegroundColorAttributeName : [UIColor orangeColor], NSShadowAttributeName : textShadow}];
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:attributedTitle style:UIBarButtonItemStylePlain target:nil action:nil];
But it says I can't use NSAttributedString in place of an NSString.
For iOS 5 and above, you can use the UIAppearance functionality to change the Text Color, Font, Tint Color etc of multiple UI Components like UITabBar, UIBarButton, etc.
for UIBarButtonItem, check the below two options.
Option1: For ANY UIBarButtonItem:
NSDictionary *aButtonAttribute = [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor darkGrayColor], UITextAttributeTextColor,
[UIColor whiteColor], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, 1)], UITextAttributeTextShadowOffset, nil];
[[UIBarButtonItem appearance] setTitleTextAttributes:aButtonAttribute forState:UIControlStateNormal];
Option 2: For UIBarButtonItem of a UINavigationBar ONLY:
NSDictionary *aButtonAttribute = [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor darkGrayColor], UITextAttributeTextColor,
[UIColor whiteColor], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, 1)], UITextAttributeTextShadowOffset, nil];
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTitleTextAttributes:aButtonAttribute forState: UIControlStateNormal];
NOTE: You can add these lines (either of the 2 options) in the .m file of AppDelegate..
Ive added a segmented control to the header of a uitableview. this works fine. but for some reason i cannot make the segmented buttons (or at least just the first button) have a red background color. it just loads with the default silver.
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView* NEWview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
NEWview.backgroundColor = [UIColor colorWithRed:78.0/255.0f green:88.0/255.0f blue:74.0/255.0f alpha:1.0];
NSArray *itemArray = [NSArray arrayWithObjects: #"Organisations", #"Events", nil];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];
segmentedControl.frame = CGRectMake(15, 5, 290, 30);
segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;
segmentedControl.selectedSegmentIndex = 1;
UIColor *newSelectedTintColor = [UIColor redColor];
[[[segmentedControl subviews] objectAtIndex:0] setTintColor:newSelectedTintColor];
[NEWview addSubview:segmentedControl];
return NEWview;
}
Any Ideas? Thanks in advance for any help..
Tint color for segmentedcontrol works only if segmented control is of bar style.
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.tintColor=[UIColor redColor];
Refer this link to changed background color of selected segment
U can also do is set title color slected and not selected like this:
NSDictionary *normalAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:#"Arial" size:20.0],UITextAttributeFont,
[UIColor colorWithRed:75.0/255.0 green:75.0/255.0 blue:75.0/255.0 alpha:1.0], UITextAttributeTextColor,
[UIColor clearColor], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, 1)], UITextAttributeTextShadowOffset,
nil];//[NSDictionary dictionaryWithObject: [UIColor redColor]forKey:UITextAttributeTextColor];
[segmentCtrl setTitleTextAttributes:normalAttributes forState:UIControlStateNormal];
NSDictionary *selectedAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:#"Arial" size:20.0],UITextAttributeFont,
[UIColor redColor], UITextAttributeTextColor,
[UIColor clearColor], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, 1)], UITextAttributeTextShadowOffset,
nil] ;//[NSDictionary dictionaryWithObject: [UIColor redColor]forKey:UITextAttributeTextColor];
[segmentCtrl setTitleTextAttributes:selectedAttributes forState:UIControlStateSelected];
Is there any way to use UIAppearance to change the text color of a selected segment in UISegmented Controller and let other segments have same Color ?
NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor colorWithRed:220.0/255.0 green:104.0/255.0 blue:1.0/255.0 alpha:1.0], UITextAttributeTextColor,
[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, 1)], UITextAttributeTextShadowOffset,
[UIFont systemFontOfSize:14], UITextAttributeFont,
nil];
[[UISegmentedControl appearance] setTitleTextAttributes:attributesDictionary forState:UIControlStateSelected];
Docs:
UIAppearance Protocol Reference
setTitleTextAttributes:forState:
I'm aware that I can individually change the font of a navigation bar as outlined in this answer: Change the navigation bar's font
Currently I'm using a more global approach:
//in my app delegate:
[[UINavigationBar appearance] setBarStyle:UIBarStyleBlackTranslucent];
Is there a way to globally change the font that the Navbar through the appearance object?
thank you!
From Ray Wenderlich:
http://www.raywenderlich.com/4344/user-interface-customization-in-ios-5
// Customize the title text for *all* UINavigationBars
[[UINavigationBar appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0],
UITextAttributeTextColor,
[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8],
UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, -1)],
UITextAttributeTextShadowOffset,
[UIFont fontWithName:#"Arial-Bold" size:0.0],
UITextAttributeFont,
nil]];
#Javy's answer with #Philip007's suggestion:
[[UINavigationBar appearance] setTitleTextAttributes: #{
UITextAttributeTextColor: [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0],
UITextAttributeTextShadowColor: [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8],
UITextAttributeTextShadowOffset: [NSValue valueWithUIOffset:UIOffsetMake(0.0f, 1.0f)],
UITextAttributeFont: [UIFont fontWithName:#"Helvetica-Light" size:0.0f]
}];
ahh... that's better!
Above answers with updates for deprecated keys and use of NSShadow:
NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor blackColor];
shadow.shadowBlurRadius = 0.0;
shadow.shadowOffset = CGSizeMake(0.0, 2.0);
[[UINavigationBar appearance] setTitleTextAttributes: #{
NSForegroundColorAttributeName : [UIColor blackColor],
NSFontAttributeName : [UIFont fontWithName:#"Helvetica-Light" size:0.0f],
NSShadowAttributeName : shadow
}];
Also setting the font size to 0 so it automatically resizes based on navigation bar orientation/height.
The question is plain easy and simple, the answer unfortunately not.
How can you change the font of the text in the UINavigationBar?
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
}];
From iOS 5 and later:
[[UINavigationBar appearance] setTitleTextAttributes: #{
UITextAttributeTextColor: [UIColor greenColor],
UITextAttributeTextShadowColor: [UIColor redColor],
UITextAttributeTextShadowOffset: [NSValue valueWithUIOffset:UIOffsetMake(0.0f, 1.0f)],
UITextAttributeFont: [UIFont fontWithName:#"Helvetica" size:20.0f]
}];
Earlier than iOS 5:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 400, 44)];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:20.0];
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
label.textAlignment = UITextAlignmentCenter;
label.textColor =[UIColor whiteColor];
label.text=self.title;
self.navigationItem.titleView = label;
[label release];
If you wanted to change the font in the Interface Builder itself (without any code) here is the way to do it in Xcode6:
1.) Find the Navigation Bar view under the Navigation Controller Scene
2.) Change the Title Font, Color and Shadow attributes in the Attributes Inspector.
The above answer works. I would add the following line before the last line. If I don't, it seems the label is center-aligned incorrectly if there is a back button on the left side but no right button.
...
[self.navigationItem.titleView sizeToFit];
[label release]; // not needed if you are using ARC
Updated for iOS 7:
[[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1.0], NSForegroundColorAttributeName,
shadow, NSShadowAttributeName,
[UIFont fontWithName:#"HelveticaNeue-CondensedBlack" size:21.0], NSFontAttributeName, nil]];
courtesy of:
http://www.appcoda.com/customize-navigation-status-bar-ios-7/
Not sure why all the answers included the shadow. Adding the lines that manipulate the shadow does nothing in respect to changing text font. These 2 lines of code will work for iOS 8.4 and Swift
let attributesDictionary = [NSFontAttributeName: UIFont(name: "Helvetica Neue", size: 14)!]
navigationController!.navigationBar.titleTextAttributes = attributesDictionary
The titleTextAttributes stores a dictionary that will dictate the font, color, size, and other attributes of the navigation bar's title.
As of iOS 5 you can use the appearance proxy.
The answer is in a duplicate of this question: https://stackoverflow.com/a/12364740/883413
NSShadow *shadow = [NSShadow new];
[shadow setShadowColor: [UIColor clearColor]];
[shadow setShadowOffset: CGSizeMake(0.0f, 1.0f)];
[self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:#"TimeBurner" size:27.0f], NSFontAttributeName,
[UIColor whiteColor], NSForegroundColorAttributeName,
shadow, NSShadowAttributeName,nil]];