Guest User

Untitled

a guest
Jul 21st, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.04 KB | None | 0 0
  1. @interface UIBarButtonItem (StyledButton)
  2. + (UIBarButtonItem *)styledBackBarButtonItemWithTarget:(id)target selector:(SEL)selector;
  3. + (UIBarButtonItem *)styledCancelBarButtonItemWithTarget:(id)target selector:(SEL)selector;
  4. + (UIBarButtonItem *)styledSubmitBarButtonItemWithTitle:(NSString *)title target:(id)target selector:(SEL)selector;
  5. @end
  6.  
  7. @implementation UIBarButtonItem (StyledButton)
  8.  
  9. + (UIBarButtonItem *)styledBackBarButtonItemWithTarget:(id)target selector:(SEL)selector;
  10. {
  11. UIImage *image = [UIImage imageNamed:@"button_back"];
  12. image = [image stretchableImageWithLeftCapWidth:20.0f topCapHeight:20.0f];
  13.  
  14. NSString *title = NSLocalizedString(@"Back", nil);
  15. UIFont *font = [UIFont boldSystemFontOfSize:12.0f];
  16.  
  17. UIButton *button = [UIButton styledButtonWithBackgroundImage:image font:font title:title target:target selector:selector];
  18. button.titleLabel.textColor = [UIColor blackColor];
  19.  
  20. CGSize textSize = [title sizeWithFont:font];
  21. CGFloat margin = (button.frame.size.height - textSize.height) / 2;
  22. CGFloat marginRight = 7.0f;
  23. CGFloat marginLeft = button.frame.size.width - textSize.width - marginRight;
  24. [button setTitleEdgeInsets:UIEdgeInsetsMake(margin, marginLeft, margin, marginRight)];
  25. [button setTitleColor:[UIColor colorWithRed:53.0f/255.0f green:77.0f/255.0f blue:99.0f/255.0f alpha:1.0f] forState:UIControlStateNormal];
  26.  
  27. return [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];
  28. }
  29.  
  30. + (UIBarButtonItem *)styledCancelBarButtonItemWithTarget:(id)target selector:(SEL)selector;
  31. {
  32. UIImage *image = [UIImage imageNamed:@"button_square"];
  33. image = [image stretchableImageWithLeftCapWidth:20.0f topCapHeight:20.0f];
  34.  
  35. NSString *title = NSLocalizedString(@"Cancel", nil);
  36. UIFont *font = [UIFont boldSystemFontOfSize:12.0f];
  37.  
  38. UIButton *button = [UIButton styledButtonWithBackgroundImage:image font:font title:title target:target selector:selector];
  39. button.titleLabel.textColor = [UIColor blackColor];
  40. [button setTitleColor:[UIColor colorWithRed:53.0f/255.0f green:77.0f/255.0f blue:99.0f/255.0f alpha:1.0f] forState:UIControlStateNormal];
  41.  
  42. return [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];
  43. }
  44.  
  45. + (UIBarButtonItem *)styledSubmitBarButtonItemWithTitle:(NSString *)title target:(id)target selector:(SEL)selector;
  46. {
  47. UIImage *image = [UIImage imageNamed:@"button_submit"];
  48. image = [image stretchableImageWithLeftCapWidth:20.0f topCapHeight:20.0f];
  49.  
  50. UIFont *font = [UIFont boldSystemFontOfSize:12.0f];
  51.  
  52. UIButton *button = [UIButton styledButtonWithBackgroundImage:image font:font title:title target:target selector:selector];
  53. button.titleLabel.textColor = [UIColor whiteColor];
  54. [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  55.  
  56. return [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];
  57. }
  58.  
  59. @interface UIButton (UIButton_StyledButton)
  60. + (UIButton *)styledButtonWithBackgroundImage:(UIImage *)image font:(UIFont *)font title:(NSString *)title target:(id)target selector:(SEL)selector;
  61. @end
  62.  
  63. @implementation UIButton (UIButton_StyledButton)
  64.  
  65. + (UIButton *)styledButtonWithBackgroundImage:(UIImage *)image font:(UIFont *)font title:(NSString *)title target:(id)target selector:(SEL)selector
  66. {
  67. CGSize textSize = [title sizeWithFont:font];
  68. CGSize buttonSize = CGSizeMake(textSize.width + 20.0f, image.size.width);
  69.  
  70. UIButton *button = [[[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, buttonSize.width, buttonSize.height)] autorelease];
  71. [button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
  72. [button setBackgroundImage:image forState:UIControlStateNormal];
  73. [button setTitle:title forState:UIControlStateNormal];
  74. [button.titleLabel setFont:font];
  75.  
  76. return button;
  77. }
  78.  
  79. @end
  80.  
  81. - (void)viewDidLoad
  82. {
  83. [super viewDidLoad];
  84.  
  85. self.navigationItem.leftBarButtonItem = [UIBarButtonItem styledBackBarButtonItemWithTarget:self selector:@selector(dismissModalViewController)];
  86. self.navigationItem.rightBarButtonItem = [UIBarButtonItem styledSubmitBarButtonItemWithTitle:NSLocalizedString(@"Done", nil) target:self selector:@selector(doneButtonTouched:)];
  87. }
  88.  
  89. - (void)viewDidLoad {
  90. [super viewDidLoad];
  91.  
  92. // Set the custom back button
  93. UIImage *buttonImage = [UIImage imageNamed:@"back_button.png"];
  94.  
  95. //create the button and assign the image
  96. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  97. [button setImage:buttonImage forState:UIControlStateNormal];
  98.  
  99. //set the frame of the button to the size of the image (see note below)
  100. button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
  101.  
  102. [button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
  103.  
  104. //create a UIBarButtonItem with the button as a custom view
  105. UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
  106. self.navigationItem.leftBarButtonItem = customBarItem;
  107.  
  108. // Cleanup
  109. [customBarItem release];
  110. }
  111.  
  112. -(void)back {
  113. // Tell the controller to go back
  114. [self.navigationController popViewControllerAnimated:YES];
  115. }
Add Comment
Please, Sign In to add comment