123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- //
- // DropDownView.m
- // PDFViewer
- //
- // Created by kdanmobile_2 on 2022/11/21.
- //
- #import "CPDFDropDownView.h"
- @implementation CPDFDropDownView
- - (id)initWithFrame:(CGRect)frame WithString:(NSArray *)str{
- if (self = [super initWithFrame:frame]) {
- _list = str;
- _borderStyle = UITextBorderStyleRoundedRect;
- _showList = NO;
- _oldFrame = frame;
- _newFrame = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, frame.size.height*3);
- _lineColor = [UIColor lightGrayColor];
- _listBgcolor = [UIColor whiteColor];
- _lineWidth = 1;
- self.backgroundColor=[UIColor clearColor];
- [self drawView];
- }
- return self;
- }
- - (void)dropdown {
- [_textField resignFirstResponder];
- if (_showList) {
- return;
- } else {
- [self.superview bringSubviewToFront:self];
- [self setShowList:YES];
- }
-
- }
- - (void)drawView {
- //Textfield
- _textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, _oldFrame.size.width, _oldFrame.size.height)];
- _textField.backgroundColor = [UIColor clearColor];
- _textField.opaque = YES;
- _textField.textColor = [UIColor blackColor];
- _textField.font = [UIFont fontWithName:@"Arial" size:12.0];
- _textField.borderStyle = _borderStyle;
- //_textField.textAlignment = UITextAlignmentLeft;
- [self addSubview:_textField];
- [_textField addTarget:self action:@selector(dropdown)forControlEvents:UIControlEventAllTouchEvents];
- //dropdown list
- _listView=[[UITableView alloc]initWithFrame:
- CGRectMake(_lineWidth,_oldFrame.size.height+_lineWidth,
- _oldFrame.size.width-_lineWidth*2,
- _oldFrame.size.height*2-_lineWidth*2)];
- _listView.dataSource = self;
- _listView.delegate = self;
- _listView.backgroundColor = _listBgcolor;
- _listView.separatorColor = _lineColor;
- _listView.hidden = !_showList;
- [self addSubview:_listView];
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
- return _list.count;
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"listwid"];
- if (cell == nil) {
- cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"listwid"];
- }
- cell.textLabel.text = [_list objectAtIndex:indexPath.row];
- cell.textLabel.font = _textField.font;
- cell.selectionStyle = UITableViewCellSelectionStyleGray;
- return cell;
- }
- - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
- return _oldFrame.size.height;
- }
- //When selecting a row in the drop-down list, set the value in the text box to hide the drop-down list
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
- _textField.text = (NSString *)[_list objectAtIndex:indexPath.row];
- [self setShowList:NO];
- }
- - (void)setShowList:(BOOL)b {
- _showList = b;
- if (_showList) {
- self.frame = _newFrame;
- } else {
- self.frame = _oldFrame;
- }
- _listView.hidden = !b;
- }
- @end
|