开发者

UIButton Analog for reversesTitleShadowWhenHighlighted for Selected State

开发者 https://www.devze.com 2023-04-12 23:49 出处:网络
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(UI

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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号