Coder Social home page Coder Social logo

cttokenfield's Introduction

CTTokenField

CTTokenField is a token filed component for iOS6+.

CTTokenField

Sample

Prepare data container

self.texts = [NSMutableArray array];

Create token field view

self.tokenField = [[CTTokenField alloc] initWithFrame:CGRectMake(0.0, 0.0, CGRectGetWidth(self.view.frame), 0.0)];
self.tokenField.dataSource = self;
self.tokenField.delegate = self;
[self.view addSubview:self.tokenField];

Implement data source methods

- (NSUInteger)numberOfTokensInTokenField:(CTTokenField *)tokenField
{
    return self.texts.count;
}

- (CTTokenView *)tokenField:(CTTokenField *)tokenField tokenViewAtIndex:(NSUInteger)index
{
    CTTokenView *tokenView = [[CTTokenView alloc] initWithFrame:CGRectZero];
    tokenView.textLabel.text = self.texts[index];
    return tokenView;
}

- (BOOL)tokenField:(CTTokenField *)tokenField shouldAddTokenViewWithText:(NSString *)text
{
    return YES;
}

- (void)tokenField:(CTTokenField *)tokenField willAddTokenViewWithText:(NSString *)text atIndex:(NSUInteger)index
{
    [self.texts insertObject:text atIndex:index];
}

- (void)tokenField:(CTTokenField *)tokenField willRemoveTokenViewAtIndex:(NSUInteger)index
{
    [self.texts removeObjectAtIndex:index];
}

- (void)tokenField:(CTTokenField *)tokenField willMoveTokenViewFromIndex:(NSUInteger)fromIndex
           toIndex:(NSUInteger)toIndex
{
    NSString *string = [self.texts objectAtIndex:fromIndex];
    [self.texts removeObjectAtIndex:fromIndex];
    [self.texts insertObject:string atIndex:toIndex];
}

Implement delegate methods

- (NSString *)labelTextInTokenField:(CTTokenField *)tokenField
{
    return @"Sample:";
}

- (void)addButtonDidTappedInTokenField:(CTTokenField *)tokenField
{
    [self.texts addObject:@"Foo"];
    [tokenField reloadData];
}

License

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

cttokenfield's People

Contributors

rizumita avatar zhanleewo avatar

Watchers

James Cloos avatar Younghwan Kim avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.