Removing UIBarButtonItem's text shadow - ios

I have adjusted a UIBarButtonItem to have custom font and color properties using
self.followButton = [[[UIBarButtonItem alloc]
initWithTitle: NSLocalizedString(#"TWITTER_FOLLOW_BUTTON_TEXT", nil)
style:UIBarButtonItemStylePlain
target:self
action:#selector(handleFollowButtonPressed:)]
autorelease];
,
[self.followButton setBackgroundImage:[UIImage new] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
,
[followButton setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIFont fontWithName:#"Helvetica" size:14.0], UITextAttributeFont,nil] forState:UIControlStateNormal];
and
NSDictionary *attributes = #{UITextAttributeTextColor : [UIColor colorWithRed:1 green:0.176 blue:0.333 alpha:1 /*#ff2d55*/ ]};
[followButton setTitleTextAttributes:attributes
forState:UIControlStateNormal];
to get the appearance of
How can I remove the buttonitem's text shadow without resorting to changing it for an image?

Set the UIBarButtonItem's UITextAttributeTextShadowOffset attribute to UIOffsetMake(0.0, 0.0).
You'll need to store the UIOffset in an NSValue object: [NSValue valueWithUIOffset: UIOffsetMake(0.0, 0.0)].

Alternatively you could just set UITextAttributeTextShadowColor to [UIColor clearColor]
#{UITextAttributeTextShadowColor : [UIColor clearColor]}

Related

Change the title color of keyboard toolbar objective-C

I want to change the title color of keyboard toobar i.e(submit button), and the other is how can I add the image for keyboard toolbar. TIA
UIToolbar *keyboardToolbar = [[UIToolbar alloc] init];
[keyboardToolbar sizeToFit];
keyboardToolbar.translucent=NO; //if you want it.
keyboardToolbar.barTintColor = [UIColor lightGrayColor];
_txtCommentView.inputAccessoryView = keyboardToolbar;
keyboardToolbar.items = [NSArray arrayWithObjects:
[[UIBarButtonItem alloc]initWithTitle:#"Submit" style:UIBarButtonItemStyleBordered target:self action:#selector(submitClicked:)],
nil];
Try below code and do changes as per your requirement:
UIToolbar *keyboardToolbar = [[UIToolbar alloc] init];
[keyboardToolbar sizeToFit];
keyboardToolbar.translucent=NO; //if you want it.
keyboardToolbar.barTintColor = [UIColor lightGrayColor];
_txtCommentView.inputAccessoryView = keyboardToolbar;
UIBarButtonItem *submit = [[UIBarButtonItem alloc] initWithTitle:#"Submit"
style:UIBarButtonItemStyleBordered
target:self action:#selector(submitClicked:)];
//Change submit button attributes here as you want
[submit setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:#"Helvetica-Bold" size:18.0], NSFontAttributeName,
[UIColor whiteColor], NSForegroundColorAttributeName,
nil] forState:UIControlStateNormal];
keyboardToolbar.items = [NSArray arrayWithObjects:submit, nil];
If you want to change the change in whole application Toolbar then Use
[UIToolbar appearance].tintColor = [UIColor redColor];
[UIToolbar appearance].barTintColor = [UIColor greenColor];
Also you can Use below code to change :
NSDictionary *attributes = #{
NSForegroundColorAttributeName: [UIColor colorWithRed:220.0/255.0 green:104.0/255.0 blue:1.0/255.0 alpha:1.0],
NSFontAttributeName: [UIFont fontWithName:#"Arial" size:16.0]
};
[[UIBarButtonItem appearance] setTitleTextAttributes:attributes forState:UIControlStateNormal];

UISegmentedControl only changes text color when revisiting ViewController

UPDATE ANSWERED. BY ME.
Currently having problems with the text color change of my UISegmentedControl; it needs to change on first load with UIControlStateSelected. Code works, but only conditionally. It works when you visit the page with the segmented control on the navigation bar, hit the back button, and then visit the page again. I'm assuming there's a problem with inheritance here. Let me explain..
The location of the the segmented control lies on top of my navigation bar.
Inheritance of the ViewController which contains the SegmentedControl:
TabBarViewController(managed with AppDelegate)-->navigation Controller-->ViewController(where 'inviteSegBar' lies)
Here's the code within AppDelegate.m:
[[UINavigationBar appearance] setBarTintColor:[UIColor colorWithHexString:#"#669900"]];//this one sets it green.
[[UINavigationBar appearance] setBackgroundColor:[UIColor whiteColor]];
And here's the viewDidLoad: code for the VC which contains 'inviteSegBar', the UISegmentedControl in question:
- (void)viewDidLoad
{
[super viewDidLoad];
//CUSTOM APPEARANCE <below>
self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
self.navigationController.navigationBar.tintColor = [UIColor colorWithHexString:#"#669900"];
inviteSegBar.tintColor = [UIColor colorWithHexString:#"#333333"];
[[UISegmentedControl appearance] setTitleTextAttributes:#{NSForegroundColorAttributeName : [UIColor colorWithHexString:#"#669900"]} forState:UIControlStateSelected];
}
Like I said the last line works, but only when you re-visit the page. Why is this happening?
PS This is the same issue guys, I had already tried this code before any of the answers were listed.
ANSWER FOUND: Simply move
[[UISegmentedControl appearance] setTitleTextAttributes:#{NSForegroundColorAttributeName : [UIColor colorWithHexString:#"#669900"]} forState:UIControlStateSelected];
to your AppDelegate.m file
Use
UIColor *whitecolor = [UIColor whiteColor];
NSDictionary *attributes = [NSDictionary dictionaryWithObjects:#[whitecolor] forKeys:#[UITextAttributeTextColor]];
[yourSegment setTitleTextAttributes:attributes
forState:UIControlStateNormal];
UIColor *grayColor = [UIColor darkGrayColor];
NSDictionary *attributes = [NSDictionary dictionaryWithObjects:#[grayColor] forKeys:#[UITextAttributeTextColor]];
[yourSegment setTitleTextAttributes:attributes
forState:UIControlStateSelected];
update
UIColor *whitecolor = [UIColor whiteColor];
NSDictionary *attributes = [NSDictionary dictionaryWithObjects:#[whitecolor] forKeys:#[NSForegroundColorAttributeName]];
[yourSegment setTitleTextAttributes:attributes
forState:UIControlStateNormal];
UIColor *grayColor = [UIColor darkGrayColor];
NSDictionary *attributes = [NSDictionary dictionaryWithObjects:#[grayColor] forKeys:#[NSForegroundColorAttributeName]];
[yourSegment setTitleTextAttributes:attributes
forState:UIControlStateSelected];
This code allows you to set some text attributes for label in segmented control:
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor blackColor], UITextAttributeTextColor,
nil];
[_segmentedControl setTitleTextAttributes:attributes forState:UIControlStateSelected];
More allowed attributes in Apple documentation: link
This may help you:
UIAppearance proxy to set title text attributes but preserve tintColor for borders.
[[UISegmentedControl appearance] setTitleTextAttributes:#{
NSForegroundColorAttributeName : [UIColor redColor]
} forState:UIControlStateNormal];
For change UISegmentedControl appearance insert for example into viewDidLoad function this code:
// color selected text ---> red
[[UISegmentedControl appearance] setTitleTextAttributes:#{ NSForegroundColorAttributeName : [UIColor redColor] } forState:UIControlStateSelected];
// color disabled text ---> blue
[[UISegmentedControl appearance] setTitleTextAttributes:#{ NSForegroundColorAttributeName : [UIColor blueColor] } forState:UIControlStateNormal];
// color tint segmented control ---> black
[[UISegmentedControl appearance] setTintColor:[UIColor greenColor]];

setting the text color of UIBarButtonItem by appearance proxy

Here's what is being set and it's close to the color I need:
NSDictionary *barButtonItemTitleAttributesEnabled = #{
NSFontAttributeName:[UIFont MRFontLightOfSize:17],
NSForegroundColorAttributeName:[UIColor whiteColor]
};
NSDictionary *barButtonItemTitleAttributesDisabled = #{
NSFontAttributeName:[UIFont MRFontLightOfSize:17],
NSForegroundColorAttributeName:[UIColor colorWithWhite:1.0f alpha:0.25f]
};
[[UIBarButtonItem appearance] setTitleTextAttributes:barButtonItemTitleAttributesEnabled forState:UIControlStateNormal];
[[UIBarButtonItem appearance] setTitleTextAttributes:barButtonItemTitleAttributesDisabled forState:UIControlStateDisabled];
But I'd prefer to somehow set the disabled text color to the same color as the selected state, is there a way to do that with the appearance proxy calls?
Also tried this:
NSDictionary *barButtonItemDisabled = [[UIBarButtonItem appearance] titleTextAttributesForState:UIControlStateSelected];
[[UIBarButtonItem appearance] setTitleTextAttributes:barButtonItemTitleAttributesEnabled forState:UIControlStateNormal];
[[UIBarButtonItem appearance] setTitleTextAttributes:barButtonItemDisabled forState:UIControlStateDisabled];
resolved by removing TextTitleAttribute code above and setting the toolBar tintColor appearance proxy:
[[UIToolbar appearance] setTintColor:[UIColor whiteColor]];
this made it so that just enabling or disabling the buttons set them to the right color.
If you want to give different colours for different items then you should use
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]
initWithTitle:#"Done" style:UIBarButtonItemStylePlain
target:self action:#selector(done)];
[doneButton setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor whiteColor], NSForegroundColorAttributeName,nil] forState:UIControlStateNormal];
Otherwise you should just simply write
[toolBar setTintColor:[UIColor whiteColor]];

iOS 7 UINavigationBar appearance not working first timeā€¦

I am trying to change the look of the UINavigationBar in my iOS7 app. I am doing the following:
- (void)viewDidLoad
{
[super viewDidLoad];
m_sNumberToCall = #"";
UIBarButtonItem * btn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"IconHome.png"] style:UIBarButtonItemStyleBordered target:self action:#selector(btHomeTouched:)];
self.navigationItem.leftBarButtonItem = btn;
self.navigationController.navigationBar.translucent = YES;
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"TVCNavBack.png"] forBarMetrics:UIBarMetricsDefault];
NSShadow * shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8];
shadow.shadowOffset = CGSizeMake(0, 1);
[[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:#"Helvetica-Bold" size:21.0],
NSFontAttributeName,
nil]];
}
But, the first time I present the UITableViewController it is the standard iOS7 nav bar, then I press home and present it again and it is my new look.
Any ideas why it does not work the first time?
Don't change the appearance but the navigation bar directly. The appearance affects only the future instances but not the already created ones.
Change:
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"TVCNavBack.png"] forBarMetrics:UIBarMetricsDefault];
to:
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"TVCNavBack.png"] forBarMetrics:UIBarMetricsDefault];
The answer before only helps you with the background image but not with the title text attributes.
You don't need to change your code but all you have to do is move it to
applicationDidFinishLaunchingWithOptions
in your AppDelegate.m file.

UISegmentedControl titletext is blurry

I have implemented a header view in a collection view.
It is a UISegmentedControll with three items.
Here is the code:
NSArray *itemArray = [NSArray arrayWithObjects: #"Following", #"Everybody", #"Nearby", nil];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];
UIFont *font = [UIFont fontWithName:#"PatuaOne-Regular" size:12.0f];
UIColor *notChosenButtonColor = [UIColor colorWithRed:(201.0/255.0f) green:(198.0/255.0f) blue:(191.0/255.0f) alpha:1.0];
UIColor *chosenButtonColor = [UIColor colorWithRed:(235.0/255.0f) green:(218.0/255.0f) blue:(102.0/255.0f) alpha:1.0];
NSDictionary *normalAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
font, UITextAttributeFont,
notChosenButtonColor, UITextAttributeTextColor,
nil];
NSDictionary *selectedAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
font, UITextAttributeFont,
chosenButtonColor, UITextAttributeTextColor,
nil];
[segmentedControl setTitleTextAttributes:normalAttributes forState:UIControlStateNormal];
[segmentedControl setTitleTextAttributes:selectedAttributes forState:UIControlStateSelected];
[segmentedControl setBackgroundImage:[UIImage imageNamed:#"greenbt_bg.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[segmentedControl setBackgroundImage:[UIImage imageNamed:#"standard_bt_h"] forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
[segmentedControl setBackgroundColor:[UIColor clearColor] ];
segmentedControl.frame = CGRectMake(5, 20, 280, 25);
segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;
segmentedControl.selectedSegmentIndex = 0;
[segmentedControl setDividerImage:[UIImage imageNamed:#"separator.png"]
forLeftSegmentState:UIControlStateNormal
rightSegmentState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
[headerView addSubview:segmentedControl];
The segmentedcontrol now looks like this:
Why is the text of the segments blurry when not selected?
Edit
Solved it! added [UIColor clearColor], UITextAttributeTextShadowColor to normalAttributes
That is most likely caused by the default shadowColor.
Try to set UITextAttributeTextShadowColor and UITextAttributeTextShadowOffset in your titleTextAttributes.
You might have to experiment to get the best visual appearance. You can also simply set the shadowColor attribute to [UIColor clearColor].
It's also possible that you are setting a frame that is between pixel boundaries. Using CGRectIntegral did the trick for me.

Resources