├── .gitignore ├── AutoCompleteTextView.podspec ├── AutoCompleteTextView ├── AutoCompleteShowList.h ├── AutoCompleteShowList.m ├── AutoCompleteTextView.h └── AutoCompleteTextView.m ├── AutoCompleteTextViewDemo.zip ├── LICENSE └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 4 | 5 | ## Build generated 6 | build/ 7 | DerivedData/ 8 | 9 | ## Various settings 10 | *.pbxuser 11 | !default.pbxuser 12 | *.mode1v3 13 | !default.mode1v3 14 | *.mode2v3 15 | !default.mode2v3 16 | *.perspectivev3 17 | !default.perspectivev3 18 | xcuserdata/ 19 | 20 | ## Other 21 | *.moved-aside 22 | *.xccheckout 23 | *.xcscmblueprint 24 | 25 | ## Obj-C/Swift specific 26 | *.hmap 27 | *.ipa 28 | *.dSYM.zip 29 | *.dSYM 30 | 31 | # CocoaPods 32 | # 33 | # We recommend against adding the Pods directory to your .gitignore. However 34 | # you should judge for yourself, the pros and cons are mentioned at: 35 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 36 | # 37 | # Pods/ 38 | 39 | # Carthage 40 | # 41 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 42 | # Carthage/Checkouts 43 | 44 | Carthage/Build 45 | 46 | # fastlane 47 | # 48 | # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the 49 | # screenshots whenever they are needed. 50 | # For more information about the recommended setup visit: 51 | # https://docs.fastlane.tools/best-practices/source-control/#source-control 52 | 53 | fastlane/report.xml 54 | fastlane/Preview.html 55 | fastlane/screenshots/**/*.png 56 | fastlane/test_output 57 | 58 | # Code Injection 59 | # 60 | # After new code Injection tools there's a generated folder /iOSInjectionProject 61 | # https://github.com/johnno1962/injectionforxcode 62 | 63 | iOSInjectionProject/ 64 | -------------------------------------------------------------------------------- /AutoCompleteTextView.podspec: -------------------------------------------------------------------------------- 1 | Pod::Spec.new do |s| 2 | s.name = "AutoCompleteTextView" 3 | s.version = "1.0.0" 4 | s.license = "MIT" 5 | s.summary = "AutoCompleteTextView 是一个实时刷选控件,根据输入框中的文本信息进行筛选,View的部分都已经封装好,业务部分自己实现。都以以Block的形式回掉,简单实用!现在已经支持CocoaPods管理,如果对你有所帮助,请Star和Fork。" 6 | 7 | s.homepage = "https://github.com/baishiyun/AutoCompleteTextView" # 你的主页 8 | s.source = { :git => "https://github.com/baishiyun/AutoCompleteTextView.git", :tag => "#{s.version}" } 9 | s.source_files = "AutoCompleteTextView/*.{h,m}" 10 | s.requires_arc = true 11 | s.platform = :ios, "7.0" 12 | s.frameworks = "UIKit", "Foundation" 13 | s.author = { "白仕云" => "baishiyun@163.com" } 14 | s.social_media_url = "https://github.com/baishiyun" 15 | 16 | end -------------------------------------------------------------------------------- /AutoCompleteTextView/AutoCompleteShowList.h: -------------------------------------------------------------------------------- 1 | // 2 | // AutoCompleteShowList.h 3 | // AutoCompleteTextViewDemo 4 | // 5 | // Created by 白仕云 on 2018/6/13. 6 | // Copyright © 2018年 BSY.com. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | typedef enum : NSUInteger { 12 | BSYTitleType, 13 | BSYTitleAndIcon, 14 | } BSYCellType; 15 | 16 | @interface AutoCompleteShowListCell : UITableViewCell 17 | @property (nonatomic ,strong)UILabel *showText; 18 | @end 19 | 20 | 21 | @interface AutoCompleteShowList : UITableView 22 | 23 | 24 | @property (nonatomic ,strong)NSMutableArray *dataArray; 25 | @property (nonatomic ,copy)void(^BSYAutoCompleteShowTextBlock)(NSIndexPath *indexPath,AutoCompleteShowListCell *cell,id cellObj); 26 | 27 | @property (nonatomic ,copy)void(^BSYDidSelectRowAtIndexPathBlock)(NSIndexPath *indexPath,AutoCompleteShowListCell *cell,id cellObj); 28 | 29 | @end 30 | -------------------------------------------------------------------------------- /AutoCompleteTextView/AutoCompleteShowList.m: -------------------------------------------------------------------------------- 1 | // 2 | // AutoCompleteShowList.m 3 | // AutoCompleteTextViewDemo 4 | // 5 | // Created by 白仕云 on 2018/6/13. 6 | // Copyright © 2018年 BSY.com. All rights reserved. 7 | // 8 | 9 | #import "AutoCompleteShowList.h" 10 | 11 | @interface AutoCompleteShowListCell() 12 | @property (nonatomic ,strong)UIView *bottomLine; 13 | @end 14 | @implementation AutoCompleteShowListCell 15 | 16 | /** 17 | 根据不同的cell类型显示不同的cell模版 18 | */ 19 | -(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 20 | { 21 | self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 22 | if (self) { 23 | self.selectionStyle = 0; 24 | [self addSubview:self.showText]; 25 | [self addSubview:self.bottomLine]; 26 | 27 | } 28 | return self; 29 | } 30 | 31 | -(void)layoutSubviews 32 | { 33 | [super layoutSubviews]; 34 | self.showText.frame = CGRectMake(10, 0, self.frame.size.width-20, self.frame.size.height); 35 | self.bottomLine.frame = CGRectMake(0, self.frame.size.height-1, self.frame.size.width, 1); 36 | } 37 | 38 | -(UILabel *)showText 39 | { 40 | if (!_showText) { 41 | _showText = [[UILabel alloc]init]; 42 | _showText.textColor= [UIColor blackColor]; 43 | _showText.textAlignment = NSTextAlignmentLeft; 44 | _showText.font = [UIFont systemFontOfSize:13]; 45 | _showText.numberOfLines = 2; 46 | _showText.lineBreakMode = 0; 47 | } 48 | return _showText; 49 | } 50 | 51 | -(UIView *)bottomLine 52 | { 53 | if (!_bottomLine) { 54 | _bottomLine = [[UIView alloc]init]; 55 | _bottomLine.backgroundColor=[UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:0.3]; 56 | } 57 | return _bottomLine; 58 | } 59 | @end 60 | 61 | 62 | static NSString *const AutoCompleteShowListCellID = @"AutoCompleteShowListCell"; 63 | 64 | @interface AutoCompleteShowList() 65 | @end 66 | 67 | @implementation AutoCompleteShowList 68 | 69 | -(instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style 70 | { 71 | self = [super initWithFrame:frame style:style]; 72 | 73 | if (self) { 74 | self.separatorStyle = 0; 75 | self.dataSource = self; 76 | self.delegate = self; 77 | self.tableFooterView = [[UIView alloc]init]; 78 | [self registerClass:[AutoCompleteShowListCell class] forCellReuseIdentifier:AutoCompleteShowListCellID]; 79 | } 80 | return self; 81 | } 82 | 83 | -(void)reloadDataWithIndexPath:(NSIndexPath *)indexPath cell:(AutoCompleteShowListCell *)cell obj:(id)obj 84 | { 85 | if (self.BSYAutoCompleteShowTextBlock) { 86 | self.BSYAutoCompleteShowTextBlock(indexPath, cell, obj); 87 | } 88 | } 89 | -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 90 | { 91 | return 1; 92 | } 93 | -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 94 | { 95 | return self.dataArray.count; 96 | } 97 | -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 98 | { 99 | AutoCompleteShowListCell *cell = [tableView dequeueReusableCellWithIdentifier:AutoCompleteShowListCellID]; 100 | [self reloadDataWithIndexPath:indexPath cell:cell obj:self.dataArray[indexPath.row]]; 101 | return cell; 102 | } 103 | 104 | -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 105 | { 106 | AutoCompleteShowListCell *cell = (AutoCompleteShowListCell *)[tableView cellForRowAtIndexPath:indexPath]; 107 | if (self.BSYDidSelectRowAtIndexPathBlock) { 108 | self.BSYDidSelectRowAtIndexPathBlock(indexPath, cell, self.dataArray[indexPath.row]); 109 | } 110 | } 111 | 112 | -(void)setDataArray:(NSMutableArray *)dataArray 113 | { 114 | _dataArray = dataArray; 115 | if (!dataArray) { 116 | _dataArray = [NSMutableArray array]; 117 | } 118 | dispatch_async(dispatch_get_main_queue(), ^{ 119 | [self reloadData]; 120 | }); 121 | } 122 | @end 123 | -------------------------------------------------------------------------------- /AutoCompleteTextView/AutoCompleteTextView.h: -------------------------------------------------------------------------------- 1 | // 2 | // AutoCompleteTextView.h 3 | // AutoCompleteTextViewDemo 4 | // 5 | // Created by 白仕云 on 2018/6/13. 6 | // Copyright © 2018年 BSY.com. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "AutoCompleteShowList.h" 11 | 12 | // ################# 文本输入框 ########################## 13 | 14 | @interface AutoCompleteTextView : UITextField 15 | 16 | /** 17 | 重置list的高度 18 | */ 19 | @property (nonatomic ,assign)CGFloat ShowListHight; 20 | 21 | /** 22 | 重新init方法 23 | @param frame 位置大小 24 | @param superView list的父视图(也就是list add 的View) 25 | */ 26 | -(instancetype)initWithFrame:(CGRect)frame superView:(UIView *)superView; 27 | 28 | /** 29 | 筛选数据 30 | */ 31 | @property (nonatomic ,strong)NSMutableArray *dataArray; 32 | 33 | /** 34 | 监听输入文本框内容变化 35 | */ 36 | @property (nonatomic ,copy)void(^AutoCompleteTextViewTextFieldTextChange)(NSString *searchTextString); 37 | 38 | 39 | /** 40 | 列表上内容赋值 41 | */ 42 | @property (nonatomic ,copy)void(^BSYAutoCompleteShowChange)(NSIndexPath *indexPath,AutoCompleteShowListCell *cell,id cellObj); 43 | ; 44 | 45 | /** 46 | 点击列表内容获取点击的数据 47 | */ 48 | @property (nonatomic ,copy)void(^didSelectRowAtIndexPathChange)(NSIndexPath *indexPath,AutoCompleteShowListCell *cell,id cellObj); 49 | @end 50 | -------------------------------------------------------------------------------- /AutoCompleteTextView/AutoCompleteTextView.m: -------------------------------------------------------------------------------- 1 | // 2 | // AutoCompleteTextView.m 3 | // AutoCompleteTextViewDemo 4 | // 5 | // Created by 白仕云 on 2018/6/13. 6 | // Copyright © 2018年 BSY.com. All rights reserved. 7 | // 8 | 9 | #import "AutoCompleteTextView.h" 10 | #import "AutoCompleteShowList.h" 11 | 12 | 13 | 14 | // ################# 文本输入框 ########################## 15 | 16 | 17 | @interface AutoCompleteTextView() 18 | /** 19 | 显示的list 20 | */ 21 | @property (nonatomic ,strong)AutoCompleteShowList *completeShowList; 22 | @property (nonatomic ,strong)UIView *superView; 23 | @end 24 | 25 | @implementation AutoCompleteTextView 26 | 27 | -(instancetype)initWithFrame:(CGRect)frame superView:(UIView *)superView 28 | { 29 | self = [super initWithFrame:frame]; 30 | if (self) { 31 | 32 | self.delegate = self; 33 | self.superView = superView; 34 | [self searchTextFieldAttribute]; 35 | [self addTarget:self action:@selector(textField1TextChange:) forControlEvents:UIControlEventEditingChanged]; 36 | 37 | __weak typeof(self)selfWeak =self; 38 | [self.completeShowList setBSYAutoCompleteShowTextBlock:^(NSIndexPath *indexPath, AutoCompleteShowListCell *cell, id cellObj) { 39 | if (selfWeak.BSYAutoCompleteShowChange) { 40 | selfWeak.BSYAutoCompleteShowChange(indexPath, cell, cellObj); 41 | } 42 | }]; 43 | [self.completeShowList setBSYDidSelectRowAtIndexPathBlock:^(NSIndexPath *indexPath, AutoCompleteShowListCell *cell, id cellObj) { 44 | if (selfWeak.didSelectRowAtIndexPathChange) { 45 | selfWeak.didSelectRowAtIndexPathChange(indexPath, cell, cellObj); 46 | } 47 | selfWeak.completeShowList.hidden = true; 48 | [selfWeak resignFirstResponder]; 49 | }]; 50 | } 51 | return self; 52 | } 53 | 54 | -(void)textFieldDidBeginEditing:(UITextField *)textField 55 | { 56 | 57 | for (AutoCompleteShowList *list in self.superView.subviews) { 58 | if (![list isKindOfClass:[AutoCompleteShowList class]]) { 59 | [self.superView addSubview:self.completeShowList]; 60 | } 61 | } 62 | if ([textField.text isEqualToString:@""]) { 63 | self.completeShowList.hidden = true; 64 | }else{ 65 | self.completeShowList.hidden = false; 66 | [self textField1TextChange:textField]; 67 | } 68 | 69 | } 70 | -(void)textFieldDidEndEditing:(UITextField *)textField 71 | { 72 | self.completeShowList.hidden = true; 73 | [self resignFirstResponder]; 74 | 75 | } 76 | 77 | -(void)layoutSubviews 78 | { 79 | [super layoutSubviews]; 80 | 81 | self.ShowListHight =(self.ShowListHight==0.00)?200.00:self.ShowListHight; 82 | self.completeShowList.frame = CGRectMake(self.frame.origin.x,CGRectGetMaxY(self.frame)+1, self.frame.size.width,self.ShowListHight); 83 | } 84 | 85 | /** 86 | 监听输入框边框 87 | 88 | @param textChange 变化后的文本 89 | */ 90 | -(void)textField1TextChange:(UITextField *)textChange 91 | { 92 | if ([textChange.text isEqualToString:@""]) { 93 | self.completeShowList.hidden = true; 94 | }else{ 95 | self.completeShowList.hidden = false; 96 | if (self.AutoCompleteTextViewTextFieldTextChange) { 97 | self.AutoCompleteTextViewTextFieldTextChange(textChange.text); 98 | } 99 | } 100 | } 101 | 102 | 103 | -(void)setShowListHight:(CGFloat)ShowListHight 104 | { 105 | _ShowListHight = ShowListHight; 106 | } 107 | 108 | -(void)setDataArray:(NSMutableArray *)dataArray 109 | { 110 | _dataArray = dataArray; 111 | dispatch_async(dispatch_get_main_queue(), ^{ 112 | self.completeShowList.dataArray = dataArray; 113 | }); 114 | } 115 | 116 | -(AutoCompleteShowList *)completeShowList 117 | { 118 | if (!_completeShowList) { 119 | _completeShowList = [[AutoCompleteShowList alloc]initWithFrame:CGRectZero style:UITableViewStylePlain]; 120 | _completeShowList.layer.borderColor = [UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:0.6].CGColor; 121 | _completeShowList.layer.borderWidth = 1; 122 | _completeShowList.layer.cornerRadius = 5; 123 | _completeShowList.layer.masksToBounds = YES; 124 | } 125 | return _completeShowList; 126 | } 127 | 128 | -(void )searchTextFieldAttribute 129 | { 130 | self.textColor = [UIColor blackColor]; 131 | self.font = [UIFont systemFontOfSize:13]; 132 | self.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 30)]; 133 | self.leftViewMode = UITextFieldViewModeAlways; 134 | self.borderStyle = UITextBorderStyleRoundedRect; 135 | } 136 | 137 | @end 138 | -------------------------------------------------------------------------------- /AutoCompleteTextViewDemo.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/baishiyun/AutoCompleteTextView/9f8ef6358cc90494db8361178c83a925b4bde596/AutoCompleteTextViewDemo.zip -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 白仕云 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AutoCompleteTextView 2 | AutoCompleteTextView 是一个实时刷选控件,根据输入框中的文本信息进行筛选,View的部分都已经封装好,业务部分自己实现。都以以Block的形式回掉,简单实用!现在已经支持CocoaPods管理,如果对你有所帮助,请Star和Fork。 3 | --------------------------------------------------------------------------------