ALTagInputTextField 0.1.5

ALTagInputTextField 0.1.5

TestsTested
LangLanguage Obj-CObjective C
License MIT
ReleasedLast Release Dec 2014

Maintained by Aziz U. Latypov.



  • By
  • Aziz U. Latypov

Usage

Import to your pch file

#import <ALTagInputTextField/ALTagInputTextField.h>

Create a TagLookupDataSource on top of any subclass of ALCollectionViewDataSource, e.g.

// 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

In storyboard ad a text field and set its class to ALTagInputTextField. Connect the outlet to your 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

Requirements

Installation

Author

Aziz U. Latypov, [email protected]

License

ALTagInputTextField is available under the MIT license. See the LICENSE file for more info.