BKTouchIDSwitchView.m 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //
  2. // BKTouchIDSwitchView.m
  3. // BKPasscodeViewDemo
  4. //
  5. // Created by Byungkook Jang on 2014. 10. 11..
  6. // Copyright (c) 2014년 Byungkook Jang. All rights reserved.
  7. //
  8. #import "BKTouchIDSwitchView.h"
  9. @implementation BKTouchIDSwitchView
  10. - (instancetype)initWithFrame:(CGRect)frame
  11. {
  12. self = [super initWithFrame:frame];
  13. if (self) {
  14. [self _initialize];
  15. }
  16. return self;
  17. }
  18. - (id)initWithCoder:(NSCoder *)aDecoder
  19. {
  20. self = [super initWithCoder:aDecoder];
  21. if (self) {
  22. [self _initialize];
  23. }
  24. return self;
  25. }
  26. - (void)_initialize
  27. {
  28. self.switchBackgroundView = [[UIView alloc] init];
  29. self.switchBackgroundView.backgroundColor = [UIColor whiteColor];
  30. self.switchBackgroundView.layer.borderColor = [UIColor lightGrayColor].CGColor;
  31. self.switchBackgroundView.layer.borderWidth = .5f;
  32. [self addSubview:self.switchBackgroundView];
  33. self.messageLabel = [[UILabel alloc] init];
  34. self.messageLabel.numberOfLines = 0;
  35. self.messageLabel.lineBreakMode = NSLineBreakByWordWrapping;
  36. self.messageLabel.textAlignment = NSTextAlignmentCenter;
  37. self.messageLabel.text = NSLocalizedStringFromTable(@"Do you want to use Touch ID for authentication?", @"BKPasscodeView", @"Touch ID를 사용하시겠습니까?");
  38. self.messageLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline];
  39. [self addSubview:self.messageLabel];
  40. self.titleLabel = [[UILabel alloc] init];
  41. self.titleLabel.text = NSLocalizedStringFromTable(@"Enable Touch ID", @"BKPasscodeView", @"Touch ID 사용");
  42. self.titleLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
  43. [self addSubview:self.titleLabel];
  44. self.touchIDSwitch = [[UISwitch alloc] init];
  45. [self addSubview:self.touchIDSwitch];
  46. self.doneButton = [UIButton buttonWithType:UIButtonTypeSystem];
  47. [self.doneButton.titleLabel setFont:[UIFont systemFontOfSize:20.f]];
  48. [self.doneButton setTitle:NSLocalizedStringFromTable(@"Done", @"BKPasscodeView", @"확인") forState:UIControlStateNormal];
  49. [self.doneButton addTarget:self action:@selector(doneButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
  50. [self addSubview:self.doneButton];
  51. }
  52. - (void)layoutSubviews
  53. {
  54. [super layoutSubviews];
  55. UIEdgeInsets contentInset = UIEdgeInsetsMake(20, 20, 20, 20);
  56. static CGFloat verticalSpaces[] = { 40, 30 };
  57. CGRect contentBounds = UIEdgeInsetsInsetRect(self.bounds, contentInset);
  58. self.messageLabel.frame = CGRectMake(0, 0, CGRectGetWidth(contentBounds), 0);
  59. [self.messageLabel sizeToFit];
  60. [self.titleLabel sizeToFit];
  61. [self.doneButton sizeToFit];
  62. CGFloat contentHeight = (CGRectGetHeight(self.messageLabel.frame) + verticalSpaces[0] +
  63. CGRectGetHeight(self.touchIDSwitch.frame) + verticalSpaces[1] +
  64. CGRectGetHeight(self.doneButton.frame));
  65. CGFloat offsetY = floorf((CGRectGetHeight(self.frame) - contentHeight) * 0.5f);
  66. CGRect rect;
  67. rect = self.messageLabel.frame;
  68. rect.origin = CGPointMake(contentInset.left, offsetY);
  69. rect.size.width = CGRectGetWidth(contentBounds);
  70. self.messageLabel.frame = rect;
  71. offsetY += CGRectGetHeight(rect) + verticalSpaces[0];
  72. rect = self.touchIDSwitch.frame;
  73. rect.origin = CGPointMake(CGRectGetMaxX(contentBounds) - CGRectGetWidth(self.touchIDSwitch.frame), offsetY);
  74. self.touchIDSwitch.frame = rect;
  75. rect = self.titleLabel.frame;
  76. rect.origin = CGPointMake(contentInset.left, offsetY);
  77. rect.size.height = CGRectGetHeight(self.touchIDSwitch.frame);
  78. self.titleLabel.frame = rect;
  79. offsetY += CGRectGetHeight(rect) + verticalSpaces[1];
  80. rect = self.doneButton.frame;
  81. rect.size.width += 10;
  82. rect.size.height += 10;
  83. rect.origin.x = floorf((CGRectGetWidth(self.frame) - CGRectGetWidth(rect)) * 0.5f);
  84. rect.origin.y = offsetY;
  85. self.doneButton.frame = rect;
  86. self.switchBackgroundView.frame = CGRectMake(-1,
  87. CGRectGetMinY(self.touchIDSwitch.frame) - 12,
  88. CGRectGetWidth(self.frame) + 2,
  89. CGRectGetHeight(self.touchIDSwitch.frame) + 24);
  90. }
  91. - (void)doneButtonPressed:(id)sender
  92. {
  93. if ([self.delegate respondsToSelector:@selector(touchIDSwitchViewDidPressDoneButton:)]) {
  94. [self.delegate touchIDSwitchViewDidPressDoneButton:self];
  95. }
  96. }
  97. @end