I'm trying to change the offset of the shadow behind the text in a UIBarButtonItem.
This is my code:
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
[attributes setValue:[UIColor colorWithWhite:0.30 alpha:1.0] forKey:UITextAttributeTextColor];
[attributes setValue:[UIColor whiteColor] forKey:UITextAttributeTextShadowColor];
[attributes setValue:[NSValue valueWithUIOffset:UIOffsetMake(0.0, 0.0)] forKey:UITextAttributeTextShadowOffset];
[[UIBarButtonItem appearance] setTitleTextAttributes:attributes forState:UIControlStateNormal];
Changing the text color works. Changing the shadow color works. Changing the shadow offset doesn't seem to do anything.
Is something wrong with the way I'm doing this? I've also tried setting it directly, without the appearance proxy, but that didn't work either.
I believe your code is correct though perhaps you expect something different from what it does. If I paste it into a test app and change the offset values to 10.0, 10.0 the shadow offset is visible for me. (iOS 5.0)
Your shadow UIOffset values are 0.0,0.0 which effectively causes no shadow. You will need to offset the shadow by at least 1 pixel in any direction. eg. this will give you a shadow to the bottom left side.
[NSValue valueWithUIOffset:UIOffsetMake(-1.0, 1.0)]
Related
No matter what settings I try, I cannot get UISegmentedControl to render with the tintColor I set. I took a few screenshots, and it seems whatever color I set, the rendered result is 20% darker (i.e. UIColor whiteColor ends up R:204,G:204,B:204)
I've tried every combination of the settings below, and nothing seems to change the fact it always looks darker. However, inspection of the tintAdjustmentMode at all stages before/after setting the values, appear, etc. comes out as UITintAdjustmentModeNormal. I can make the background color of the text render at full brightness, but the frame and selected index fill is always at 80% brightness. Oddly, this doesn't happen in the 7.1 Simulator, but it does happen on my iPhone 5 with 7.1.1.
Has anyone had similar problems? I've read many other threads about controls not dimming, or people wanting to set different colors for different parts of the control, but couldn't find any about tintColors that won't show at full brightness.
Various settings I've tried individually and together:
UIColor *tintColorToSet = [UIColor colorWithRed:0/255.0f green:241/255.0f blue:1.0f alpha:1.0f];
NSMutableDictionary *textAttributes = [NSMutableDictionary dictionaryWithObjectsAndKeys:
tintColorToSet, NSForegroundColorAttributeName,
[UIColor clearColor], NSBackgroundColorAttributeName,
[UIFont fontWithName:#"NesController" size:18.0f], NSFontAttributeName, nil];
[_lengthBar setTitleTextAttributes:textAttributes forState:UIControlStateNormal];
[textAttributes setValue:[UIColor blackColor] forKey:NSForegroundColorAttributeName];
[_lengthBar setTitleTextAttributes:textAttributes forState:UIControlStateSelected];
_lengthBar.tintColor = tintColorToSet;
_lengthBar.alpha = 1.0f;
_lengthBar.tintAdjustmentMode = UIViewTintAdjustmentModeNormal;
Thanks for the help.
I need to set a bottom shadow to a button. I have the shadow but somehow I cannot change the shadow color. What am I doing wrong? Here is the code:
[self.buttonsBar.layer setShadowColor:[UIColor colorWithRed:227.0f/255 green:233.0f/255 blue:239.0f/255 alpha:1.0f].CGColor];
[self.buttonsBar.layer setShadowOpacity:1];
[self.buttonsBar.layer setShadowRadius:1];
[self.buttonsBar.layer setShadowOffset:CGSizeMake(0, 1)];
Make sure your self.buttonsBar.clipsToBounds = NO;
also make sure your self.buttonsBar.layer.masksToBounds = NO;
first try large radius and offset to make sure you see them then reduce the numbers, sometimes it is not easy to see the shadow depending on the background colors ...
It looks like this whenever off:
While I'd prefer more of a grey background. Do I really have to use a UIImageView?
Here is how I changed the fill color of my iOS7 UISwitch.
First you need to import QuartzCore.
#import <QuartzCore/QuartzCore.h>
Then set the background color and round the UISwitch's corners.
UISwitch *mySwitch = [[UISwitch alloc] initWithFrame:CGRectMake(0.0, 0.0, 51.0, 31.0)];
mySwitch.backgroundColor = [UIColor redColor];
mySwitch.layer.cornerRadius = 16.0; // you must import QuartzCore to do this.
[self addSubview:mySwitch];
This will give you a UISwitch with a custom off (background) color.
Hope this helps someone:)
You can set the setOnTintColor property of your UISwitch to the color you desire.
You can also set this for the switch in Interface Builder. Just set the background colour of the UISwitch to whatever colour you want (white, in the example below), then set a User Defined Runtime Attribute of layer.cornerRadius = 16:
There's no API support for changing the off fill color of a UISwitch.
Adjusting the tintColor will only affect the outline, and adjusting the backgroundColor will affect the whole frame, including the parts outside the rounded bounds.
You either have to place a properly shaped opaque UIView behind it or - easier - use a custom open source implementation, such as MBSwitch, which allows you to set the off fill color.
You can also use an image as background, using the [UIColor colorWithPatternImage];
mySwitch.onTintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"toggle-bg-on"]];
mySwitch.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"toggle-bg-off"]];
Adding to Barry Wyckoff solution : set tint color also
UISwitch *mySwitch = [[UISwitch alloc] initWithFrame:CGRectMake(0.0, 0.0, 51.0, 31.0)];
mySwitch.backgroundColor = [UIColor redColor];
mySwitch.layer.cornerRadius = 16.0; // you must import QuartzCore to do this.
mySwitch.tintColor = [UIColor redColor];
[self addSubview:mySwitch];
I have seen some answers that show how to change the placeHolder text color for UITextField by overriding the drawPlaceholderInRect: method such as this one:
iPhone UITextField - Change placeholder text color
but that does not maintain the existing attributes such as alignment, font, etc...what is a better way to solve this?
From iOS 6,
Without any subclassing, one can accomplish this with a couple lines of code like so:
UIColor *color = [UIColor blackColor];
textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:placeholderText attributes:#{NSForegroundColorAttributeName: color}];
There are multiple ways to achieve this.
Using Interface Builder or Storyboard
Select the Textfield for which you want to change placeholder color
go to the Identity inspector menu on Top right of Xcode
Add the key value pair this way
Key path = _placeholderLabel.textColor
Click the Type and chose Color attribute .
Then select the color in value.
Set The placeholder color using code :
Process 1:
[textField setValue:[UIColor blueColor] forKeyPath:#"_placeholderLabel.textColor"];
Process 2 :
Override drawPlaceholderInRect:(CGRect)rect method
- (void) drawPlaceholderInRect:(CGRect)rect {
[[UIColor blueColor] setFill];
[[self placeholder] drawInRect:rect withFont:[UIFont systemFontOfSize:14]];
}
There is indeed a much better way to handle this now. This will work for iOS 6 and 7.
(Note this example, I created the code in AwakeFromNib since it won't be changing colors once set. But if you don't use XIB, you will have to change the location where you put this code, such as in drawPlaceholderInRect,)
In this example, we create a subclass of UITextField, override awakeFromNib and then set the placeHolder text color to red:
- (void)awakeFromNib
{
if ([self.attributedPlaceholder length])
{
// Extract attributes
NSDictionary * attributes = (NSMutableDictionary *)[ (NSAttributedString *)self.attributedPlaceholder attributesAtIndex:0 effectiveRange:NULL];
NSMutableDictionary * newAttributes = [[NSMutableDictionary alloc] initWithDictionary:attributes];
[newAttributes setObject:[UIColor redColor] forKey:NSForegroundColorAttributeName];
// Set new text with extracted attributes
self.attributedPlaceholder = [[NSAttributedString alloc] initWithString:[self.attributedPlaceholder string] attributes:newAttributes];
}
}
The nice thing about this approach is that it maintains the current UITextField properties for the placeHolder string and so will allow you to work in IB for most of what you set. In addition, its much more efficient than doing everytime you need to draw. It also allows you to change any other property you want on the placeHolder text while maintaining the rest of the properties.
As mentioned above, if don't use XIBs, then you will need to call this at some other time. If you do put this code in the drawPlaceholderInRect: method, then make sure you call [super drawPlaceholderInRect:] at the end of it.
The safe way to customize UITextField’s placeholder is subclassing the UITextField and overriding placeholderRectForBounds:, Apple won’t bother you on this one. However, if you want to take the risk, you can try this way:
[self.MyTextField setValue:[UIColor darkGrayColor] forKeyPath:#"_placeholderLabel.textColor"];
Source.
I am using UIButton's appearance proxy to customize all UIButtons in my application. Everything is working very well - I can set custom image, text color and shadow color. Only one thing is bugging me. To set shadow offset, I've used this piece of code:
[[UIButton appearance] setTitleShadowOffset:CGSizeMake(1, 1)];
And it's working. But the documentation says that setTitleShadowOffset: is deprecated, and instead we should use the shadowOffset property of the titleLabel. I've tried it like this:
[[[UIButton appearance] titleLabel] setShadowOffset:CGSizeMake (1.0, 1.0)];
but it's not working. Can I set shadow offset without using a deprecated method?
Try:
[[UILabel appearanceWhenContainedIn:[UIButton class], nil]
setShadowOffset:CGSizeMake(1.0, 1.0)];
([[UILabel appearance]
setShadowOffset:CGSizeMake(1.0, 1.0)]; should also work but is probably overkill as it will affect all UILabels, not just the ones contained in UIButtons.)