UIButton Analog for reversesTitleShadowWhenHighlighted for Selected State
I am using the reversesTitleShadowWhenHighlighted
property on UIButton to reverse the title's text shadow(and it works great), but I really want the same thing for the selected state of the button(UIControlStateSelected
).
Any answers to the related ques开发者_开发百科tions are also welcome:
- Is there a way to do this with a
UIButton
without modifications? - Is there a way to set the shadowOffset of the title label per state with UIButton(similar to what is possible with the shadowColor)?
- If you were to extend UIButton to add this, how would you do it?
I run into the same problem, so I guess it's no for your first two questions. Here is how I subclassed it :
@interface MyButton : UIButton
@property (nonatomic) BOOL reversesTitleShadowWhenSelected;
@end
@implementation MyButton
@synthesize reversesTitleShadowWhenSelected;
- (void)setSelected:(BOOL)selected
{
if (self.reversesTitleShadowWhenSelected)
{
if ((selected && !self.isSelected) ||
(!selected && self.isSelected))
{
CGSize offset = self.titleLabel.shadowOffset;
offset.width *= -1;
offset.height *= -1;
self.titleLabel.shadowOffset = offset;
}
}
[super setSelected:selected];
}
@end
精彩评论