Salve,
mal wieder einer aus der Rubrik "Stirn schlägt auf Tischplatte" in Verbindung mit einem selbstgemalten UIButton. Ergebnis soll in etwa so aussehen wie unten, nur mit gescheitem Text.
Auf einem View stelle ich einen UIButton dar. Auf Basis einer Subclass mit folgendem Code:
Alles anzeigen
Im ViewController versuche ich dann noch properties...
Alles anzeigen
und einen Titel zu setzen:
Alles anzeigen
Und was passiert? Nix! Outlets sind alle gesetzt, auch Subclass (Im IB). Hat das was mit der Subclass zu tun? Hab schon alles mögliche ausprobiert, nix hilft. Ich habe das Gefühl, dass Apple alles immer komplizierter macht. So ein dämlicher Button
kann einen doch nicht einen halbe Tag beschäftigen?
Ciao. Kay.
mal wieder einer aus der Rubrik "Stirn schlägt auf Tischplatte" in Verbindung mit einem selbstgemalten UIButton. Ergebnis soll in etwa so aussehen wie unten, nur mit gescheitem Text.
Auf einem View stelle ich einen UIButton dar. Auf Basis einer Subclass mit folgendem Code:
Quellcode
- #import "IntroductionButton.h"
- @implementation IntroductionButton
- static int BUTTON_SHADOW_HEIGHT_PADDING = 1;
- static int BUTTON_SHADOW_WIDTH_PADDING = 2;
- - (id)initWithFrame:(CGRect)frame
- {
- self = [super initWithFrame:frame];
- if (self) {
- }
- return self;
- }
- // Only override drawRect: if you perform custom drawing.
- // An empty implementation adversely affects performance during animation.
- - (void)drawRect:(CGRect)rect
- {
- //// General Declarations
- CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
- CGContextRef context = UIGraphicsGetCurrentContext();
- //// Color Declarations
- UIColor* niceButtonGradientColor = [UIColor colorWithRed: 0.837 green: 0.837 blue: 0.837 alpha: 1];
- //// Gradient Declarations
- NSArray* niceButtonGradientColors = [NSArray arrayWithObjects:
- (id)[UIColor whiteColor].CGColor,
- (id)[UIColor colorWithRed: 0.92 green: 0.92 blue: 0.92 alpha: 1].CGColor,
- (id)niceButtonGradientColor.CGColor,
- (id)[UIColor colorWithRed: 0.92 green: 0.92 blue: 0.92 alpha: 1].CGColor,
- (id)[UIColor whiteColor].CGColor, nil];
- CGFloat niceButtonGradientLocations[] = {0, 0, 0.5, 1, 1};
- CGGradientRef niceButtonGradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)niceButtonGradientColors, niceButtonGradientLocations);
- //// Shadow Declarations
- UIColor* outerShadow = [UIColor grayColor];
- CGSize outerShadowOffset = CGSizeMake(1.1, 1.1);
- CGFloat outerShadowBlurRadius = 0.5;
- UIColor* innerShadow = [UIColor whiteColor];
- CGSize innerShadowOffset = CGSizeMake(1.1, 1.1);
- CGFloat innerShadowBlurRadius = 1.7;
- //// Abstracted Attributes
- CGRect niceButtonRect = CGRectMake(0.5, 0.5, rect.size.width-BUTTON_SHADOW_WIDTH_PADDING, rect.size.height-BUTTON_SHADOW_HEIGHT_PADDING);
- CGFloat niceButtonStrokeWidth = 0.5;
- CGFloat niceButtonCornerRadius = 4;
- //// Nice Button Drawing
- UIBezierPath* niceButtonPath = [UIBezierPath bezierPathWithRoundedRect: niceButtonRect cornerRadius: niceButtonCornerRadius];
- CGContextSaveGState(context);
- CGContextSetShadowWithColor(context, outerShadowOffset, outerShadowBlurRadius, outerShadow.CGColor);
- CGContextBeginTransparencyLayer(context, NULL);
- [niceButtonPath addClip];
- CGContextDrawLinearGradient(context, niceButtonGradient, CGPointMake(109.5, 0.5), CGPointMake(109.5, 36.5), 0);
- CGContextEndTransparencyLayer(context);
- ////// Nice Button Inner Shadow
- CGRect niceButtonBorderRect = CGRectInset([niceButtonPath bounds], -innerShadowBlurRadius, -innerShadowBlurRadius);
- niceButtonBorderRect = CGRectOffset(niceButtonBorderRect, -innerShadowOffset.width, -innerShadowOffset.height);
- niceButtonBorderRect = CGRectInset(CGRectUnion(niceButtonBorderRect, [niceButtonPath bounds]), -1, -1);
- UIBezierPath* niceButtonNegativePath = [UIBezierPath bezierPathWithRect: niceButtonBorderRect];
- [niceButtonNegativePath appendPath: niceButtonPath];
- niceButtonNegativePath.usesEvenOddFillRule = YES;
- CGContextSaveGState(context);
- {
- CGFloat xOffset = innerShadowOffset.width + round(niceButtonBorderRect.size.width);
- CGFloat yOffset = innerShadowOffset.height;
- CGContextSetShadowWithColor(context,
- CGSizeMake(xOffset + copysign(0.1, xOffset), yOffset + copysign(0.1, yOffset)),
- innerShadowBlurRadius,
- innerShadow.CGColor);
- [niceButtonPath addClip];
- CGAffineTransform transform = CGAffineTransformMakeTranslation(-round(niceButtonBorderRect.size.width), 0);
- [niceButtonNegativePath applyTransform: transform];
- [[UIColor grayColor] setFill];
- [niceButtonNegativePath fill];
- }
- CGContextRestoreGState(context);
- CGContextRestoreGState(context);
- [[UIColor darkGrayColor] setStroke];
- niceButtonPath.lineWidth = niceButtonStrokeWidth;
- [niceButtonPath stroke];
- //// Cleanup
- CGGradientRelease(niceButtonGradient);
- CGColorSpaceRelease(colorSpace);
- }
- @end
Im ViewController versuche ich dann noch properties...
Quellcode
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- // Do any additional setup after loading the view from its nib.
- UIBarButtonItem* doneButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(finishInitialSetup)];
- self.navigationItem.rightBarButtonItems = @[doneButtonItem];
- [self.birthdateButton setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
- [self.birthdateButton setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
- [self.birthdateButton setTitleShadowColor:[UIColor whiteColor] forState:UIControlStateNormal];
- [self.birthdateButton setTitleShadowColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
- [[self.birthdateButton titleLabel] setFont:[UIFont boldSystemFontOfSize:14.0f]];
- }
und einen Titel zu setzen:
Quellcode
- NSLog(@"Birthdate picked: %@",selectedBirthdate.description);
- NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
- [dateFormatter setTimeStyle:NSDateFormatterNoStyle];
- [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
- NSString* formattedDateString = [NSString stringWithFormat:@"%@: %@",NSLocalizedString(@"introduction.birthdate.button.title.prefix", nil),[dateFormatter stringFromDate:selectedBirthdate]];
- NSLog(@"%@",formattedDateString);
- [self.birthdateButton setTitle:formattedDateString forState:UIControlStateNormal|UIControlStateHighlighted|UIControlStateSelected];
Und was passiert? Nix! Outlets sind alle gesetzt, auch Subclass (Im IB). Hat das was mit der Subclass zu tun? Hab schon alles mögliche ausprobiert, nix hilft. Ich habe das Gefühl, dass Apple alles immer komplizierter macht. So ein dämlicher Button
kann einen doch nicht einen halbe Tag beschäftigen?
Ciao. Kay.