• 如何创建一个渐变背景色的按钮


    通过 NVUIGradientButton 类来实现,作者: Nicolas Verinaud

     1 //
     2 //  NVUIGradientButton.h
     3 //
     4 //  Created by Nicolas Verinaud on 20/06/12.
     5 //  Copyright (c) 2012 nverinaud.com. All rights reserved.
     6 //
     7 
     8 #import <UIKit/UIKit.h>
     9 
    10 typedef enum {
    11     NVUIGradientButtonStyleDefault = 1,
    12     NVUIGradientButtonStyleBlackOpaque,
    13     NVUIGradientButtonStyleBlackTranslucent
    14 } NVUIGradientButtonStyle;
    15 
    16 
    17 @interface NVUIGradientButton : UIControl
    18 
    19 @property (nonatomic, assign) NVUIGradientButtonStyle style;
    20 //角度半径
    21 @property (nonatomic) CGFloat cornerRadius; // Default to 10.0
    22 //边宽
    23 @property (nonatomic) CGFloat borderWidth; // Default to 2.0
    24 //填充颜色
    25 @property (strong, nonatomic) UIColor *tintColor; // Default to gray
    26 //高亮时填充颜色
    27 @property (strong, nonatomic) UIColor *highlightedTintColor; // Default to nice blue
    28 //边框颜色
    29 @property (strong, nonatomic) UIColor *borderColor; // Default to darkGray
    30 //高亮时边框颜色
    31 @property (strong, nonatomic) UIColor *highlightedBorderColor; // Default to white
    32 //字体颜色
    33 @property (strong, nonatomic) UIColor *textColor; // Default to black
    34 //高亮时字体颜色
    35 @property (strong, nonatomic) UIColor *highlightedTextColor; // Default to white
    36 //字体阴影颜色
    37 @property (strong, nonatomic) UIColor *textShadowColor; // Default to clear
    38 //高亮时字体阴影颜色
    39 @property (strong, nonatomic) UIColor *highlightedTextShadowColor; // Default to darkGrey
    40 //内容文字
    41 @property (copy, nonatomic) NSString *text;
    42 //高亮时内容文字
    43 @property (copy, nonatomic) NSString *highlightedText; // Default to text
    44 //设置disabled状态颜色
    45 @property (copy, nonatomic) NSString *disabledText; // Default to text
    46 //字所在的label
    47 @property (strong, nonatomic, readonly) UILabel *titleLabel;
    48 //设置YES渐变色,设置NO单色
    49 @property (nonatomic, getter = isGradientEnabled) BOOL gradientEnabled; // Default to YES, set to NO to draw flat color
    50 //rightAccessory图片
    51 @property (nonatomic, strong) UIImage *rightAccessoryImage;
    52 //高亮时rightAccessory图片
    53 @property (nonatomic, strong) UIImage *rightHighlightedAccessoryImage;
    54 
    55 //三种初始化便利方式
    56 // Designated initializer
    57 - (id)initWithFrame:(CGRect)frame style:(NVUIGradientButtonStyle)style cornerRadius:(CGFloat)cornerRadius borderWidth:(CGFloat)borderWidth andText:(NSString *)text;
    58 // Convenient initializers
    59 - (id)initWithFrame:(CGRect)frame cornerRadius:(CGFloat)cornerRadius borderWidth:(CGFloat)borderWidth andText:(NSString *)text;
    60 - (id)initWithFrame:(CGRect)frame style:(NVUIGradientButtonStyle)style;
    61 
    62 // Convenience for configuration depending on states
    63 //填充颜色
    64 - (void)setTintColor:(UIColor *)tintColor forState:(UIControlState)state;
    65 //边颜色
    66 - (void)setBorderColor:(UIColor *)borderColor forState:(UIControlState)state;
    67 //字颜色
    68 - (void)setTextColor:(UIColor *)textColor forState:(UIControlState)state;
    69 //字阴影颜色
    70 - (void)setTextShadowColor:(UIColor *)textShadowColor forState:(UIControlState)state;
    71 //文字
    72 - (void)setText:(NSString *)text forState:(UIControlState)state;
    73 //RightAccessory
    74 - (void)setRightAccessoryImage:(UIImage *)rightAccessoryImage forState:(UIControlState)state;
    75 
    76 @end
    View Code
      1 //
      2 //  NVUIGradientButton.m
      3 //
      4 //  Created by Nicolas Verinaud on 20/06/12.
      5 //  Copyright (c) 2012 nverinaud.com. All rights reserved.
      6 //
      7 
      8 #import "NVUIGradientButton.h"
      9 
     10 @interface NVUIGradientButton ()
     11 - (void)performDefaultInit;
     12 - (void)updateAccordingToStyle;
     13 - (BOOL)isHighlightedOrSelected;
     14 - (UIColor *)tintColorAccordingToCurrentState;
     15 - (UIColor *)borderColorAccordingToCurrentState;
     16 - (UIColor *)textColorAccordingToCurrentState;
     17 - (UIColor *)textShadowColorAccordingToCurrentState;
     18 - (NSString *)textAccordingToCurrentState;
     19 - (UIImage *)rightAccessoryImageAccordingToCurrentState;
     20 - (CGGradientRef)newGradientAccordingToCurrentState;
     21 @property (strong, nonatomic, readwrite) UILabel *titleLabel;
     22 @end
     23 
     24 
     25 @implementation NVUIGradientButton
     26 
     27 #pragma mark - Creation
     28 
     29 #define NVUIGradientButtonDefaultCorderRadius    10.0
     30 #define NVUIGradientButtonDefaultBorderWidth    2.0
     31 
     32 - (void)performDefaultInit
     33 {
     34     // Defaults
     35     _highlightedText = _text;
     36     _disabledText = _text;
     37     
     38     // Label
     39     _titleLabel = [[UILabel alloc] init];
     40     _titleLabel.textAlignment = UITextAlignmentCenter;
     41     _titleLabel.lineBreakMode = UILineBreakModeMiddleTruncation;
     42     _titleLabel.numberOfLines = 0;
     43     _titleLabel.font = [UIFont boldSystemFontOfSize:15.0];
     44     _titleLabel.minimumFontSize = 12.0;
     45     _titleLabel.shadowOffset = CGSizeMake(0, -1);
     46     
     47     _gradientEnabled = YES;
     48     
     49     self.opaque = NO;
     50     self.backgroundColor = [UIColor clearColor];
     51 }
     52 
     53 
     54 - (void)updateAccordingToStyle
     55 {
     56     switch (_style)
     57     {
     58         case NVUIGradientButtonStyleBlackOpaque:
     59         {
     60             self.tintColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1];
     61             self.highlightedTintColor = [UIColor colorWithRed:(CGFloat)3/255 green:(CGFloat)112/255 blue:(CGFloat)236/255 alpha:1];
     62             self.borderColor = [UIColor whiteColor];
     63             self.highlightedBorderColor = [UIColor whiteColor];
     64             self.textColor = [UIColor whiteColor];
     65             self.highlightedTextColor = [UIColor whiteColor];
     66             self.textShadowColor = [UIColor clearColor];
     67             self.highlightedTextShadowColor = [UIColor clearColor];
     68             break;
     69         }
     70             
     71         case NVUIGradientButtonStyleBlackTranslucent:
     72         {
     73             self.tintColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.7];
     74             self.highlightedTintColor = [UIColor colorWithRed:(CGFloat)3/255 green:(CGFloat)112/255 blue:(CGFloat)236/255 alpha:0.7];
     75             self.borderColor = [UIColor whiteColor];
     76             self.highlightedBorderColor = [UIColor whiteColor];
     77             self.textColor = [UIColor whiteColor];
     78             self.highlightedTextColor = [UIColor whiteColor];
     79             self.textShadowColor = [UIColor clearColor];
     80             self.highlightedTextShadowColor = [UIColor clearColor];
     81             break;
     82         }
     83             
     84         case NVUIGradientButtonStyleDefault:
     85         {
     86             CGFloat gray = 220.0/255.0;
     87             self.tintColor = [UIColor colorWithRed:gray green:gray blue:gray alpha:1];
     88             self.highlightedTintColor = [UIColor colorWithRed:0 green:(CGFloat)157/255 blue:1 alpha:1];
     89             self.borderColor = [UIColor darkGrayColor];
     90             self.highlightedBorderColor = [UIColor whiteColor];
     91             self.textColor = [UIColor blackColor];
     92             self.highlightedTextColor = [UIColor whiteColor];
     93             self.textShadowColor = [UIColor clearColor];
     94             self.highlightedTextShadowColor = [UIColor darkGrayColor];
     95             break;
     96         }
     97     }
     98 }
     99 
    100 #pragma mark Designated initializer
    101 
    102 - (id)initWithFrame:(CGRect)frame style:(NVUIGradientButtonStyle)style cornerRadius:(CGFloat)cornerRadius borderWidth:(CGFloat)borderWidth andText:(NSString *)text
    103 {
    104     self = [super initWithFrame:frame];
    105     if (self)
    106     {
    107         _cornerRadius = cornerRadius;
    108         _borderWidth = borderWidth;
    109         _text = text;
    110         
    111         [self performDefaultInit];
    112         
    113         self.style = style;
    114     }
    115     return self;
    116 }
    117 
    118 
    119 #pragma mark Convenient initializers
    120 
    121 - (id)initWithFrame:(CGRect)frame cornerRadius:(CGFloat)cornerRadius borderWidth:(CGFloat)borderWidth andText:(NSString *)text
    122 {
    123     return [self initWithFrame:frame style:NVUIGradientButtonStyleDefault cornerRadius:cornerRadius borderWidth:borderWidth andText:text];
    124 }
    125 
    126 
    127 - (id)initWithFrame:(CGRect)frame style:(NVUIGradientButtonStyle)style
    128 {
    129     return [self initWithFrame:frame style:style cornerRadius:NVUIGradientButtonDefaultCorderRadius borderWidth:NVUIGradientButtonDefaultBorderWidth andText:nil];
    130 }
    131 
    132 #pragma mark Overriden initializers
    133 
    134 - (id)initWithFrame:(CGRect)frame
    135 {
    136     return [self initWithFrame:frame style:NVUIGradientButtonStyleDefault cornerRadius:NVUIGradientButtonDefaultCorderRadius borderWidth:NVUIGradientButtonDefaultBorderWidth andText:nil];
    137 }
    138 
    139 
    140 - (id)initWithCoder:(NSCoder *)aDecoder
    141 {
    142     self = [super initWithCoder:aDecoder];
    143     if (self)
    144     {
    145         _cornerRadius = NVUIGradientButtonDefaultCorderRadius;
    146         _borderWidth = NVUIGradientButtonDefaultBorderWidth;
    147         
    148         [self performDefaultInit];
    149         
    150         self.style = NVUIGradientButtonStyleDefault;
    151     }
    152     return self;
    153 }
    154 
    155 
    156 #pragma mark - Setters
    157 
    158 - (void)setStyle:(NVUIGradientButtonStyle)style
    159 {
    160     if (style != _style)
    161     {
    162         _style = style;
    163         [self updateAccordingToStyle];
    164     }
    165 }
    166 
    167 
    168 - (void)setTintColor:(UIColor *)tintColor
    169 {
    170     if (tintColor != _tintColor)
    171     {
    172         _tintColor = tintColor;
    173         
    174         if (self.state == UIControlStateNormal)
    175             [self setNeedsDisplay];
    176     }
    177 }
    178 
    179 
    180 - (void)setHighlightedTintColor:(UIColor *)highlightedTintColor
    181 {
    182     if (highlightedTintColor != _highlightedTintColor)
    183     {
    184         _highlightedTintColor = highlightedTintColor;
    185         
    186         if ([self isHighlightedOrSelected])
    187             [self setNeedsDisplay];
    188     }
    189 }
    190 
    191 
    192 - (void)setBorderColor:(UIColor *)borderColor
    193 {
    194     if (borderColor != _borderColor)
    195     {
    196         _borderColor = borderColor;
    197         
    198         if (self.state == UIControlStateNormal)
    199             [self setNeedsDisplay];
    200     }
    201 }
    202 
    203 
    204 - (void)setHighlightedBorderColor:(UIColor *)highlightedBorderColor
    205 {
    206     if (highlightedBorderColor != _highlightedBorderColor)
    207     {
    208         _highlightedBorderColor = highlightedBorderColor;
    209         
    210         if ([self isHighlightedOrSelected])
    211             [self setNeedsDisplay];
    212     }
    213 }
    214 
    215 
    216 - (void)setTextColor:(UIColor *)textColor
    217 {
    218     if (textColor != _textColor)
    219     {
    220         _textColor = textColor;
    221         
    222         if (self.state == UIControlStateNormal)
    223             [self setNeedsDisplay];
    224     }
    225 }
    226 
    227 
    228 - (void)setHighlightedTextColor:(UIColor *)highlightedTextColor
    229 {
    230     if (highlightedTextColor != _highlightedTextColor)
    231     {
    232         _highlightedTextColor = highlightedTextColor;
    233         
    234         if ([self isHighlightedOrSelected])
    235             [self setNeedsDisplay];
    236     }
    237 }
    238 
    239 
    240 - (void)setTextShadowColor:(UIColor *)textShadowColor
    241 {
    242     if (textShadowColor != _textShadowColor)
    243     {
    244         _textShadowColor = textShadowColor;
    245         
    246         if (self.state == UIControlStateNormal)
    247             [self setNeedsDisplay];
    248     }
    249 }
    250 
    251 
    252 - (void)setHighlightedTextShadowColor:(UIColor *)highlightedTextShadowColor
    253 {
    254     if (highlightedTextShadowColor != _highlightedTextShadowColor)
    255     {
    256         _highlightedTextShadowColor = highlightedTextShadowColor;
    257         
    258         if ([self isHighlightedOrSelected])
    259             [self setNeedsDisplay];
    260     }
    261 }
    262 
    263 
    264 - (void)setText:(NSString *)text
    265 {
    266     if (![text isEqualToString:_text])
    267     {
    268         _text = text;
    269         
    270         if (self.state == UIControlStateNormal)
    271             [self setNeedsDisplay];
    272     }
    273 }
    274 
    275 
    276 - (void)setHighlightedText:(NSString *)highlightedText
    277 {
    278     if (![highlightedText isEqualToString:_highlightedText])
    279     {
    280         _highlightedText = highlightedText;
    281         
    282         if ([self isHighlightedOrSelected])
    283             [self setNeedsDisplay];
    284     }
    285 }
    286 
    287 
    288 - (void)setDisabledText:(NSString *)disabledText
    289 {
    290     if (![disabledText isEqualToString:_disabledText])
    291     {
    292         _disabledText = disabledText;
    293         
    294         if (self.state & UIControlStateDisabled)
    295             [self setNeedsDisplay];
    296     }
    297 }
    298 
    299 
    300 - (void)setRightAccessoryImage:(UIImage *)rightAccessoryImage
    301 {
    302     if (rightAccessoryImage != _rightAccessoryImage)
    303     {
    304         _rightAccessoryImage = rightAccessoryImage;
    305         
    306         if (self.state == UIControlStateNormal)
    307             [self setNeedsDisplay];
    308     }
    309 }
    310 
    311 
    312 - (void)setRightHighlightedAccessoryImage:(UIImage *)rightHighlightedAccessoryImage
    313 {
    314     if (rightHighlightedAccessoryImage != _rightHighlightedAccessoryImage)
    315     {
    316         _rightHighlightedAccessoryImage = rightHighlightedAccessoryImage;
    317         
    318         if ([self isHighlightedOrSelected])
    319             [self setNeedsDisplay];
    320     }
    321 }
    322 
    323 
    324 #pragma mark - Convenience configuration for states
    325 
    326 - (BOOL)isHighlightedOrSelected
    327 {
    328     return (self.state & UIControlStateHighlighted || self.state & UIControlStateSelected);
    329 }
    330 
    331 
    332 - (void)setTintColor:(UIColor *)tintColor forState:(UIControlState)state
    333 {
    334     if (state == UIControlStateNormal)
    335         self.tintColor = tintColor;
    336     
    337     if (state & UIControlStateHighlighted || state & UIControlStateSelected)
    338         self.highlightedTintColor = tintColor;
    339 }
    340 
    341 
    342 - (void)setBorderColor:(UIColor *)borderColor forState:(UIControlState)state
    343 {
    344     if (state == UIControlStateNormal)
    345         self.borderColor = borderColor;
    346     
    347     if (state & UIControlStateHighlighted || state & UIControlStateSelected)
    348         self.highlightedBorderColor = borderColor;
    349 }
    350 
    351 
    352 - (void)setTextColor:(UIColor *)textColor forState:(UIControlState)state
    353 {
    354     if (state == UIControlStateNormal)
    355         self.textColor = textColor;
    356     
    357     if (state & UIControlStateHighlighted || state & UIControlStateSelected)
    358         self.highlightedTextColor = textColor;
    359 }
    360 
    361 
    362 - (void)setTextShadowColor:(UIColor *)textShadowColor forState:(UIControlState)state
    363 {
    364     if (state == UIControlStateNormal)
    365         self.textShadowColor = textShadowColor;
    366     
    367     if (state & UIControlStateHighlighted || state & UIControlStateSelected)
    368         self.highlightedTextShadowColor = textShadowColor;
    369 }
    370 
    371 
    372 - (void)setText:(NSString *)text forState:(UIControlState)state
    373 {
    374     if (state == UIControlStateNormal)
    375         self.text = text;
    376     
    377     if (state & UIControlStateDisabled)
    378         self.disabledText = text;
    379     
    380     if (state & UIControlStateHighlighted || state & UIControlStateSelected)
    381         self.highlightedText = text;
    382 }
    383 
    384 
    385 - (void)setRightAccessoryImage:(UIImage *)rightAccessoryImage forState:(UIControlState)state
    386 {
    387     if (state == UIControlStateNormal)
    388         self.rightAccessoryImage = rightAccessoryImage;
    389     
    390     if (state & UIControlStateHighlighted || state & UIControlStateSelected)
    391         self.rightHighlightedAccessoryImage = rightAccessoryImage;
    392 }
    393 
    394 
    395 - (UIColor *)tintColorAccordingToCurrentState
    396 {
    397     UIColor *tintColor = _tintColor;
    398     
    399     if([self isHighlightedOrSelected])
    400         tintColor = _highlightedTintColor;
    401     
    402     return tintColor;
    403 }
    404 
    405 
    406 - (UIColor *)borderColorAccordingToCurrentState
    407 {
    408     UIColor *borderColor = _borderColor;
    409     
    410     if ([self isHighlightedOrSelected] && _highlightedBorderColor)
    411         borderColor = _highlightedBorderColor;
    412     
    413     return borderColor;
    414 }
    415 
    416 
    417 - (UIColor *)textColorAccordingToCurrentState
    418 {
    419     UIColor *textColor = _textColor;
    420     
    421     if ([self isHighlightedOrSelected] && _highlightedTextColor)
    422         textColor = _highlightedTextColor;
    423     
    424     if (!self.enabled)
    425         textColor = [textColor colorWithAlphaComponent:0.5];
    426     
    427     return textColor;
    428 }
    429 
    430 
    431 - (UIColor *)textShadowColorAccordingToCurrentState
    432 {
    433     UIColor *textShadowColor = _textShadowColor;
    434     
    435     if ([self isHighlightedOrSelected] && _highlightedTextShadowColor)
    436         textShadowColor = _highlightedTextShadowColor;
    437     
    438     return textShadowColor;
    439 }
    440 
    441 
    442 - (NSString *)textAccordingToCurrentState
    443 {
    444     NSString *text = _text;
    445     
    446     if (!self.enabled && _disabledText)
    447         text = _disabledText;
    448     else if ([self isHighlightedOrSelected] && _highlightedText)
    449         text = _highlightedText;
    450     
    451     return text;
    452 }
    453 
    454 
    455 - (UIImage *)rightAccessoryImageAccordingToCurrentState
    456 {
    457     UIImage *image = _rightAccessoryImage;
    458     
    459     if ([self isHighlightedOrSelected] && _rightHighlightedAccessoryImage)
    460         image = _rightHighlightedAccessoryImage;
    461     
    462     return image;
    463 }
    464 
    465 
    466 #pragma mark - Gradient
    467 
    468 - (CGGradientRef)newGradientAccordingToCurrentState
    469 {
    470     CGGradientRef gradient = NULL;
    471     
    472     // Compute the colors of the gradient
    473     UIColor *middleColor = [self tintColorAccordingToCurrentState];
    474     
    475     CGFloat red = 0, green = 0, blue = 0, alpha = 0;
    476     if ([middleColor respondsToSelector:@selector(getRed:green:blue:alpha:)]) // iOS 5+
    477     {
    478         [middleColor getRed:&red green:&green blue:&blue alpha:&alpha];
    479     }
    480     else if (middleColor)
    481     {
    482         CGColorRef color = [middleColor CGColor];
    483         const CGFloat *components = CGColorGetComponents(color);
    484         red = components[0];
    485         green = components[1];
    486         blue = components[2];
    487         alpha = components[3];
    488     }
    489     
    490     CGFloat offsetColor = 50.0f/255.0f;
    491     UIColor *topColor = [UIColor colorWithRed:fminf(1, red+offsetColor) green:fminf(1, green+offsetColor) blue:fminf(1, blue+offsetColor) alpha:alpha];
    492     UIColor *bottomColor = [UIColor colorWithRed:fminf(1, red-offsetColor) green:fminf(1, green-offsetColor) blue:fminf(1, blue-offsetColor) alpha:alpha];
    493     
    494     CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    495     
    496     // Create an array of colors
    497     CFMutableArrayRef colors = CFArrayCreateMutable(NULL, 3, NULL);
    498     CFArrayAppendValue(colors, [topColor CGColor]);
    499     CFArrayAppendValue(colors, [middleColor CGColor]);
    500     CFArrayAppendValue(colors, [bottomColor CGColor]);
    501     
    502     // Create the gradient
    503     gradient = CGGradientCreateWithColors(colorSpace, colors, NULL);
    504     
    505     // Memory free
    506     CFRelease(colors);
    507     CGColorSpaceRelease(colorSpace);
    508     
    509     return gradient;
    510 }
    511 
    512 
    513 #pragma mark - Drawing
    514 
    515 #define DEFAULT_PADDING        4
    516 
    517 - (void)drawRect:(CGRect)rect
    518 {
    519     // Setting Env
    520     CGContextRef ctx = UIGraphicsGetCurrentContext();
    521     UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:_cornerRadius];
    522     CGFloat padding = _borderWidth + DEFAULT_PADDING;
    523     UIColor *borderColor = [self borderColorAccordingToCurrentState];
    524     UIColor *textColor = [self textColorAccordingToCurrentState];
    525     UIColor *textShadowColor = [self textShadowColorAccordingToCurrentState];
    526     NSString *text = [self textAccordingToCurrentState];
    527     
    528     // Draw
    529     [path addClip];
    530     
    531     // Draw background
    532     if (_gradientEnabled)
    533     {
    534         CGGradientRef gradient = [self newGradientAccordingToCurrentState];
    535         CGFloat midX = CGRectGetMidX(self.bounds);
    536         CGFloat botY = CGRectGetMaxY(self.bounds);
    537         CGPoint startPoint = CGPointMake(midX, 0);
    538         CGPoint endPoint = CGPointMake(midX, botY);
    539         CGContextDrawLinearGradient(ctx, gradient, startPoint, endPoint, 0);
    540         CGGradientRelease(gradient);
    541     }
    542     else
    543     {
    544         UIColor *tint = [self tintColorAccordingToCurrentState];
    545         [tint set];
    546         [path fill];
    547     }
    548     
    549     // Draw right image
    550     UIImage *rightImage = [self rightAccessoryImageAccordingToCurrentState];
    551     CGRect rightAccessoryRect = CGRectZero;
    552     if (rightImage)
    553     {
    554         CGFloat maxHeight = CGRectGetHeight(self.bounds) - padding*2;
    555         rightAccessoryRect.size.height = MIN(maxHeight, rightImage.size.height);
    556         rightAccessoryRect.size.width = rightAccessoryRect.size.height / rightImage.size.height * rightImage.size.width;
    557         rightAccessoryRect.origin.y = (CGRectGetHeight(self.bounds) - CGRectGetHeight(rightAccessoryRect)) / 2;
    558         rightAccessoryRect.origin.x = CGRectGetWidth(self.bounds) - CGRectGetWidth(rightAccessoryRect) - padding;
    559         [rightImage drawInRect:rightAccessoryRect];
    560     }
    561     
    562     // Draw border
    563     if (_borderWidth > 0)
    564     {
    565         path.lineWidth = _borderWidth;
    566         [borderColor set];
    567         [path stroke];
    568     }
    569     
    570     // Draw text
    571     CGRect innerRect = self.bounds;
    572     innerRect = CGRectInset(innerRect, padding, padding);
    573     if (rightImage)
    574         innerRect.size.width = CGRectGetMinX(rightAccessoryRect);
    575     
    576     _titleLabel.textColor = textColor;
    577     _titleLabel.shadowColor = textShadowColor;
    578     _titleLabel.text = text;
    579     
    580     [textColor set];
    581     [_titleLabel drawTextInRect:innerRect];
    582 }
    583 
    584 
    585 #pragma mark - Touch Handling
    586 
    587 - (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event
    588 {
    589     self.highlighted = YES;
    590     [self setNeedsDisplay];
    591     return [super beginTrackingWithTouch:touch withEvent:event];
    592 }
    593 
    594 
    595 - (BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event
    596 {
    597     self.highlighted = [self isTouchInside];
    598     [self setNeedsDisplay];
    599     return [super continueTrackingWithTouch:touch withEvent:event];
    600 }
    601 
    602 
    603 - (void)endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event
    604 {
    605     [super endTrackingWithTouch:touch withEvent:event];
    606     self.highlighted = NO;
    607     [self setNeedsDisplay];
    608 }
    609 
    610 
    611 - (void)cancelTrackingWithEvent:(UIEvent *)event
    612 {
    613     [super cancelTrackingWithEvent:event];
    614     self.highlighted = NO;
    615     [self setNeedsDisplay];
    616 }
    617 
    618 
    619 #pragma mark - Accessibility
    620 
    621 - (BOOL)isAccessibilityElement
    622 {
    623     return YES;
    624 }
    625 
    626 
    627 - (NSString *)accessibilityLabel
    628 {
    629     return [self textAccordingToCurrentState];
    630 }
    631 
    632 
    633 - (UIAccessibilityTraits)accessibilityTraits
    634 {
    635     UIAccessibilityTraits traits = UIAccessibilityTraitButton;
    636     
    637     if ([self isHighlightedOrSelected])
    638         traits = traits >> UIAccessibilityTraitSelected;
    639     
    640     if (!self.enabled)
    641         traits = traits >> UIAccessibilityTraitNotEnabled;
    642     
    643     return traits;
    644 }
    645 
    646 
    647 - (NSString *)accessibilityHint
    648 {
    649     return [self textAccordingToCurrentState];
    650 }
    651 
    652 
    653 @end
    View Code

    使用范例:

    NVUIGradientButton* btnRight = [[NVUIGradientButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width-80, 5, 70, 30) style:NVUIGradientButtonStyleDefault];
        btnRight.text = @"返回首页";
        btnRight.textColor = [UIColor whiteColor];
        btnRight.highlightedTextColor = [UIColor whiteColor];
        btnRight.textShadowColor = [UIColor darkGrayColor];
        btnRight.tintColor = [UIColor colorWithRed:(CGFloat)98.0f/255.0f green:(CGFloat)122.0f/255.0f blue:(CGFloat)156.0/255.0f alpha:1];
        btnRight.highlightedTintColor = [UIColor colorWithRed:(CGFloat)81.0f/255.0f green:(CGFloat)102.0f/255.0f blue:(CGFloat)131.0f/255.0f alpha:1];
        btnRight.borderColor = [UIColor colorWithRed:(CGFloat)47.0f/255.0f green:(CGFloat)57.0f/255.0f blue:(CGFloat)67.0f/255.0f alpha:1];
        btnRight.highlightedBorderColor = [UIColor colorWithRed:(CGFloat)46.0f/255.0f green:(CGFloat)62.0f/255.0f blue:(CGFloat)87.0f/255.0f alpha:1];
        btnRight.titleLabel.font = [UIFont systemFontOfSize:12.0f];
        [btnRight addTarget:self action:@selector(onBtnRight:) forControlEvents:UIControlEventTouchUpInside];
  • 相关阅读:
    浏览器 显示一个对话框,对话框中包含一条文字信息,用来提示用户输入文字。window.prompt()
    JS字符串转换为JSON对象的四种方法
    js中 json对象与json字符串相互转换的几种方式 $.parseJSON(jsonStr)json字符串转换为json对象
    C#变量命名规则(命名规范)
    C#中AppDomain.CurrentDomain.BaseDirectory(获取程序的基目录)及各种路径获取方法
    C# 的 Path.GetFileName、Path.GetExtension、Path.GetDirectoryName千一网络 http://www.itpow.com/
    js计算两个时间相差天数,获取时间的毫秒数之差
    C#将DataTable转化为List<T>
    https://www.cnblogs.com/sizhizhiyue/p/4820973.html asp.net后台导出excel的方法一:使用response导出excel
    .NET调用AS/400上的程序(.NET CALL AS/400 PGM)
  • 原文地址:https://www.cnblogs.com/qc0815/p/3198289.html
Copyright © 2020-2023  润新知