测试测试过的 | ✗ |
语言语言 | Obj-CObjective C |
许可协议 | MIT |
发布时间最后发布时间 | 2014年12月 |
由 Aziz U. Latypov 维护。
导入到您的 pch 文件中
#import <ALTagInputTextField/ALTagInputTextField.h>
在 ALCollectionViewDataSource 的任何子类上创建一个 TagLookupDataSource,例如
// ALTagLookupDataSource.h
#import <ALDataSource/ALCollectionViewDataSourceWithFetchedResultsController.h>
@interface ALTagLookupDataSource : ALCollectionViewDataSourceWithFetchedResultsController
- (instancetype)initWithManagedObjectContext:(NSManagedObjectContext*)managedObjectContext
cellConfigurationBlock:(ALCollectionViewCellConfigurationBlock)cellConfigurationBlock
andReuseIdentiferBlock:(ALCollectionViewCellReuseIdentiferBlock)reuseIdentifierBlock;
@end
// ALTagLookupDataSource.m
#import "ALTagLookupDataSource.h"
@implementation ALTagLookupDataSource
- (instancetype)initWithManagedObjectContext:(NSManagedObjectContext*)managedObjectContext
cellConfigurationBlock:(ALCollectionViewCellConfigurationBlock)cellConfigurationBlock
andReuseIdentiferBlock:(ALCollectionViewCellReuseIdentiferBlock)reuseIdentifierBlock
{
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Tag" // Entity Name
inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"title" // SortField
ascending:YES];
[fetchRequest setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
self = [super initWithFetchRequest:fetchRequest
managedObjectContext:managedObjectContext
cellConfigurationBlock:cellConfigurationBlock
andReuseIdentiferBlock:reuseIdentifierBlock];
if (self){
}
return self;
}
@end
在 Storyboard 中添加一个文本字段并设置其类为 ALTagInputTextField。将输出连接到您的 ViewController。
#import "ALViewController.h"
#import <ALTagInputTextField/ALTagInputTextField.h>
#import <ALCoreDataManager/ALCoreDataManager+Singleton.h>
#import <ALManagedObjectFactory/ALManagedObjectFactory+Singleton.h>
#import "ALManagedObjectFactory+CreateTag.h"
#import "ALTagLookupDataSource.h"
@interface ALViewController () <ALTagInputTextFieldDelegate>
@property (nonatomic, strong) ALTagLookupDataSource *tagLookupDataSource;
@property (weak, nonatomic) IBOutlet ALTagInputTextField *tagInputTextField;
@end
@implementation ALViewController
- (void)setTagInputTextField:(ALTagInputTextField *)tagInputTextField
{
_tagInputTextField = tagInputTextField;
NSManagedObjectContext *managedObjectContext = [ALCoreDataManager defaultManager].managedObjectContext;
self.tagLookupDataSource = [[ALTagLookupDataSource alloc] initWithManagedObjectContext:managedObjectContext
cellConfigurationBlock:nil
andReuseIdentiferBlock:nil];
_tagInputTextField.lookupDataSource = self.tagLookupDataSource;
_tagInputTextField.tagInputDelegate = self;
// customize appearance
_tagInputTextField.cellBackgroundColor = [UIColor grayColor];
_tagInputTextField.cellTextFont = [UIFont fontWithName:@"HelveticaNeue-Light" size:14.f];
_tagInputTextField.cellTextColor = [UIColor whiteColor];
}
- (id<ALTag>)createTagWithText:(NSString *)text
{
return (id<ALTag>)[[ALManagedObjectFactory defaultFactory] createTagWithTitle:text];
}
-(void)tagInputTextField:(ALTagInputTextField *)tagInputTextField lookupDidSelectTag:(id<ALTag>)tag
{
NSLog(@"Select: %@",tag);
}
@end
Aziz U. Latypov,[email protected]
ALTagInputTextField 在 MIT 许可协议下可用。有关更多信息,请参阅 LICENSE 文件。