2011-10-15 77 views
2

我使用UIButton上的reversesTitleShadowWhenHighlighted屬性來顛倒標題的文字陰影(而且效果很好),但我真的想要爲按鈕的選定狀態(UIControlStateSelected)同樣的事情。UIButton Analog for reversesTitleShadowWhenHighlighted Selected Selected

到相關問題的任何答案,也歡迎:

  • 有沒有辦法用UIButton沒有修改,這樣做呢?
  • 有沒有辦法使用UIButton爲每個狀態設置標題標籤的shadowOffset(類似於shadowColor可能的)?
  • 如果你要擴展UIButton來添加這個,你會怎麼做呢?

回答

2

我遇到了同樣的問題,所以我想這不適合你的前兩個問題。下面是我如何分類它:

@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