CPDFDropDownView.m 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //
  2. // DropDownView.m
  3. // PDFViewer
  4. //
  5. // Created by kdanmobile_2 on 2022/11/21.
  6. //
  7. #import "CPDFDropDownView.h"
  8. @implementation CPDFDropDownView
  9. - (id)initWithFrame:(CGRect)frame WithString:(NSArray *)str{
  10. if (self = [super initWithFrame:frame]) {
  11. _list = str;
  12. _borderStyle = UITextBorderStyleRoundedRect;
  13. _showList = NO;
  14. _oldFrame = frame;
  15. _newFrame = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, frame.size.height*3);
  16. _lineColor = [UIColor lightGrayColor];
  17. _listBgcolor = [UIColor whiteColor];
  18. _lineWidth = 1;
  19. self.backgroundColor=[UIColor clearColor];
  20. [self drawView];
  21. }
  22. return self;
  23. }
  24. - (void)dropdown {
  25. [_textField resignFirstResponder];
  26. if (_showList) {
  27. return;
  28. } else {
  29. [self.superview bringSubviewToFront:self];
  30. [self setShowList:YES];
  31. }
  32. }
  33. - (void)drawView {
  34. //Textfield
  35. _textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, _oldFrame.size.width, _oldFrame.size.height)];
  36. _textField.backgroundColor = [UIColor clearColor];
  37. _textField.opaque = YES;
  38. _textField.textColor = [UIColor blackColor];
  39. _textField.font = [UIFont fontWithName:@"Arial" size:12.0];
  40. _textField.borderStyle = _borderStyle;
  41. //_textField.textAlignment = UITextAlignmentLeft;
  42. [self addSubview:_textField];
  43. [_textField addTarget:self action:@selector(dropdown)forControlEvents:UIControlEventAllTouchEvents];
  44. //dropdown list
  45. _listView=[[UITableView alloc]initWithFrame:
  46. CGRectMake(_lineWidth,_oldFrame.size.height+_lineWidth,
  47. _oldFrame.size.width-_lineWidth*2,
  48. _oldFrame.size.height*2-_lineWidth*2)];
  49. _listView.dataSource = self;
  50. _listView.delegate = self;
  51. _listView.backgroundColor = _listBgcolor;
  52. _listView.separatorColor = _lineColor;
  53. _listView.hidden = !_showList;
  54. [self addSubview:_listView];
  55. }
  56. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  57. return _list.count;
  58. }
  59. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  60. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"listwid"];
  61. if (cell == nil) {
  62. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"listwid"];
  63. }
  64. cell.textLabel.text = [_list objectAtIndex:indexPath.row];
  65. cell.textLabel.font = _textField.font;
  66. cell.selectionStyle = UITableViewCellSelectionStyleGray;
  67. return cell;
  68. }
  69. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  70. return _oldFrame.size.height;
  71. }
  72. //When selecting a row in the drop-down list, set the value in the text box to hide the drop-down list
  73. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  74. _textField.text = (NSString *)[_list objectAtIndex:indexPath.row];
  75. [self setShowList:NO];
  76. }
  77. - (void)setShowList:(BOOL)b {
  78. _showList = b;
  79. if (_showList) {
  80. self.frame = _newFrame;
  81. } else {
  82. self.frame = _oldFrame;
  83. }
  84. _listView.hidden = !b;
  85. }
  86. @end