Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed For Xcode4.6,customize abbreviation and token height #72

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@
*/build/*

*.xcworkspace
xcuserdata
xcuserdata
*.pbxproj
7 changes: 6 additions & 1 deletion TITokenField.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,19 @@
@property (nonatomic, assign) BOOL forcePickSearchResult;
@property (nonatomic, assign) BOOL shouldSortResults;
@property (nonatomic, assign) BOOL shouldSearchInBackground;
@property (nonatomic, assign) BOOL stopAutoSearch;
@property (nonatomic, readonly) TITokenField * tokenField;
@property (nonatomic, assign)float tokenFieldHeight;
@property (nonatomic, readonly) UIView * separator;
@property (nonatomic, readonly) UITableView * resultsTable;
@property (nonatomic, readonly) UIView * contentView;
@property (nonatomic, copy) NSArray * sourceArray;
@property (weak, nonatomic, readonly) NSArray * tokenTitles;

- (void)updateContentSize;

-(id)initWithFrame:(CGRect)frame withFieldHeight:(float)_height;
-(void)manualSearch:(NSString*)searchString;
-(void)transparentizeBackground;
@end

//==========================================================
Expand All @@ -90,6 +94,7 @@ typedef enum {
@property (nonatomic, assign) BOOL removesTokensOnEndEditing;
@property (nonatomic, readonly) int numberOfLines;
@property (nonatomic, strong) NSCharacterSet * tokenizingCharacters;
@property (nonatomic, strong) NSString* abbr;

- (void)addToken:(TIToken *)title;
- (TIToken *)addTokenWithTitle:(NSString *)title;
Expand Down
101 changes: 82 additions & 19 deletions TITokenField.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

#import "TITokenField.h"
#import <QuartzCore/QuartzCore.h>
#import "NSString+truncateToSize.h"
#import <objc/runtime.h>

@interface TITokenField ()
@property (nonatomic, assign) BOOL forcePickSearchResult;
Expand All @@ -30,6 +32,7 @@ @implementation TITokenFieldView {
UIView * _contentView;
NSMutableArray * _resultsArray;
UIPopoverController * _popoverController;
float _tokenFieldHeight;
}
@dynamic delegate;
@synthesize showAlreadyTokenized = _showAlreadyTokenized;
Expand All @@ -44,15 +47,21 @@ @implementation TITokenFieldView {
@synthesize sourceArray = _sourceArray;

#pragma mark Init
- (instancetype)initWithFrame:(CGRect)frame {
- (instancetype)initWithFrame:(CGRect)frame{

if ((self = [super initWithFrame:frame])){
[self setup];
}

return self;
}

-(id)initWithFrame:(CGRect)frame withFieldHeight:(float)_height{
if ((self=[super initWithFrame:frame])) {
_tokenFieldHeight=_height;
[self setup];
}
return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {

if ((self = [super initWithCoder:aDecoder])){
Expand All @@ -75,7 +84,7 @@ - (void)setup {
_shouldSearchInBackground = NO;
_resultsArray = [NSMutableArray array];

_tokenField = [[TITokenField alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, 42)];
_tokenField = [[TITokenField alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, _tokenFieldHeight?_tokenFieldHeight:42)];
[_tokenField addTarget:self action:@selector(tokenFieldDidBeginEditing:) forControlEvents:UIControlEventEditingDidBegin];
[_tokenField addTarget:self action:@selector(tokenFieldDidEndEditing:) forControlEvents:UIControlEventEditingDidEnd];
[_tokenField addTarget:self action:@selector(tokenFieldTextDidChange:) forControlEvents:UIControlEventEditingChanged];
Expand Down Expand Up @@ -247,10 +256,13 @@ - (void)tokenFieldDidEndEditing:(TITokenField *)field {
}

- (void)tokenFieldTextDidChange:(TITokenField *)field {
[self resultsForSearchString:_tokenField.text];

if (_forcePickSearchResult) [self setSearchResultsVisible:YES];
else [self setSearchResultsVisible:(_resultsArray.count > 0)];
if (!_stopAutoSearch) {
[self resultsForSearchString:_tokenField.text];

if (_forcePickSearchResult) [self setSearchResultsVisible:YES];
else [self setSearchResultsVisible:(_resultsArray.count > 0)];

}
}

- (void)tokenFieldFrameWillChange:(TITokenField *)field {
Expand Down Expand Up @@ -379,11 +391,28 @@ - (void)presentpopoverAtTokenFieldCaretAnimated:(BOOL)animated {

UITextPosition * position = [_tokenField positionFromPosition:_tokenField.beginningOfDocument offset:2];

position=position?position:_tokenField.beginningOfDocument;
[_popoverController presentPopoverFromRect:[_tokenField caretRectForPosition:position] inView:_tokenField
permittedArrowDirections:UIPopoverArrowDirectionUp animated:animated];
}

#pragma mark Other
-(void)manualSearch:(NSString*)searchString{
if (_stopAutoSearch) {
[self resultsForSearchString:searchString];
[self setSearchResultsVisible:YES];
}
}
-(void)transparentizeBackground{
self.backgroundColor=[UIColor clearColor];
self.tokenField.backgroundColor=[UIColor clearColor];
self.tokenField.leftView.backgroundColor=[UIColor clearColor];

[self.tokenField.layer setShadowColor:[[UIColor clearColor] CGColor]];
[self.tokenField.layer setShadowOpacity:1];
[self.tokenField.layer setShadowRadius:0];

}
- (NSString *)description {
return [NSString stringWithFormat:@"<TITokenFieldView %p; Token count = %d>", self, self.tokenTitles.count];
}
Expand Down Expand Up @@ -431,11 +460,13 @@ @implementation TITokenField {
@synthesize selectedToken = _selectedToken;
@synthesize tokenizingCharacters = _tokenizingCharacters;
@synthesize forcePickSearchResult = _forcePickSearchResult;

@synthesize abbr=_abbr;
static float _height;
#pragma mark Init
- (instancetype)initWithFrame:(CGRect)frame {

_height=frame.size.height;
if ((self = [super initWithFrame:frame])){
_height=frame.size.height;
[self setup];
}

Expand Down Expand Up @@ -531,7 +562,12 @@ - (NSArray *)tokenObjects {
- (UIScrollView *)scrollView {
return ([self.superview isKindOfClass:[UIScrollView class]] ? (UIScrollView *)self.superview : nil);
}

-(NSString*)abbr{
if (_abbr) {
return _abbr;
}else
return @"联系人";
}
#pragma mark Event Handling
- (BOOL)becomeFirstResponder {
return (_editable ? [super becomeFirstResponder] : NO);
Expand Down Expand Up @@ -562,7 +598,22 @@ - (void)didEndEditing {
CGFloat availableWidth = self.bounds.size.width - self.leftView.bounds.size.width - self.rightView.bounds.size.width;

if (_tokens.count > 1 && untokSize.width > availableWidth){
untokenized = [NSString stringWithFormat:@"%d recipients", titles.count];
// untokenized = [NSString stringWithFormat:@"%d recipients", titles.count];
NSString* endString= [NSString stringWithFormat:@"... %d %@", titles.count,self.abbr];
CGSize endSize=[endString sizeWithFont:[UIFont systemFontOfSize:14]];

availableWidth-=endSize.width;

NSString* truncatedString=[untokenized truncateToSize:CGSizeMake(availableWidth, 0)
withFont:[UIFont systemFontOfSize:14]
lineBreakMode:UILineBreakModeTailTruncation];
truncatedString=[untokenized truncateWordsToFit:CGSizeMake(availableWidth, 20)
withInset:0.0
usingFont:[UIFont systemFontOfSize:14]
wordSplitter:@","];

untokenized=[NSString stringWithFormat:@"%@%@",truncatedString,endString];

}

}
Expand All @@ -571,9 +622,9 @@ - (void)didEndEditing {
}

[self setResultsModeEnabled:NO];
if (_tokens.count < 1 && self.forcePickSearchResult) {
[self becomeFirstResponder];
}
// if (_tokens.count < 1 && self.forcePickSearchResult) {
// [self becomeFirstResponder];
// }
}

- (void)didChangeText {
Expand Down Expand Up @@ -628,7 +679,12 @@ - (void)addToken:(TIToken *)token {
//[self becomeFirstResponder];

[token addTarget:self action:@selector(tokenTouchDown:) forControlEvents:UIControlEventTouchDown];
[token addTarget:self action:@selector(tokenTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
if ([delegate respondsToSelector:@selector(tokenTouchUpInside:)]) {
[token addTarget:delegate action:@selector(tokenTouchUpInside:)
forControlEvents:UIControlEventTouchUpInside];
}else{
[token addTarget:self action:@selector(tokenTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
}
[self addSubview:token];

if (![_tokens containsObject:token]) {
Expand Down Expand Up @@ -725,7 +781,8 @@ - (void)tokenTouchUpInside:(TIToken *)token {

- (CGFloat)layoutTokensInternal {

CGFloat topMargin = floor(self.font.lineHeight * 4 / 7);
// CGFloat topMargin = floor(self.font.lineHeight * 4 / 7);
CGFloat topMargin = floor(_height-self.font.lineHeight)/2;
CGFloat leftMargin = self.leftViewWidth + 12;
CGFloat hPadding = 8;
CGFloat rightMargin = self.rightViewWidth + hPadding;
Expand Down Expand Up @@ -836,6 +893,7 @@ - (void)setPlaceholder:(NSString *)placeholder {
if (!label || ![label isKindOfClass:[UILabel class]]){
label = [[UILabel alloc] initWithFrame:CGRectMake(_tokenCaret.x + 3, _tokenCaret.y + 2, self.rightView.bounds.size.width, self.rightView.bounds.size.height)];
[label setTextColor:[UIColor colorWithWhite:0.75 alpha:1]];
label.backgroundColor=[UIColor clearColor];
_placeHolderLabel = label;
[self addSubview: _placeHolderLabel];
}
Expand Down Expand Up @@ -873,12 +931,17 @@ - (CGRect)placeholderRectForBounds:(CGRect)bounds {
}

- (CGRect)leftViewRectForBounds:(CGRect)bounds {
return ((CGRect){{8, ceilf(self.font.lineHeight * 4 / 7)}, self.leftView.bounds.size});
// return ((CGRect){{8, ceilf(self.font.lineHeight * 4 / 7)}, self.leftView.bounds.size});
return ((CGRect){{10,floor((_height-self.font.lineHeight)/2)},self.leftView.bounds.size});
}

- (CGRect)rightViewRectForBounds:(CGRect)bounds {
return ((CGRect){{bounds.size.width - self.rightView.bounds.size.width - 6,
bounds.size.height - self.rightView.bounds.size.height - 6}, self.rightView.bounds.size});
// return ((CGRect){{bounds.size.width - self.rightView.bounds.size.width - 6,
// bounds.size.height - self.rightView.bounds.size.height - 6}, self.rightView.bounds.size});
float bottomLineY=bounds.size.height-_height;
return ((CGRect){{bounds.size.width - self.rightView.bounds.size.width - 6,
bottomLineY+(_height - self.rightView.bounds.size.height )/2}, self.rightView.bounds.size});

}

- (CGFloat)leftViewWidth {
Expand Down
29 changes: 29 additions & 0 deletions TokenFieldExample/Classes/NSString+truncateToSize.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//
// NSString+FitRect.h
// MyContentView
//
// Created by chen neng on 13-8-16.
//
//

#import <Foundation/Foundation.h>

//
// NSString-truncateToSize
// Fast Fonts
//
// Created by Stuart Shelton on 28/03/2010.
// Copyright 2010 Stuart Shelton. All rights reserved.
//

@interface NSString (truncateToSize)

- (NSString *)truncateToSize: (CGSize)size withFont: (UIFont *)font lineBreakMode: (UILineBreakMode)lineBreakMode;
- (NSString *)truncateToSize: (CGSize)size withFont: (UIFont *)font lineBreakMode: (UILineBreakMode)lineBreakMode withAnchor: (NSString *)anchor;
- (NSString *)truncateToSize: (CGSize)size withFont: (UIFont *)font lineBreakMode: (UILineBreakMode)lineBreakMode withStartingAnchor: (NSString *)startingAnchor withEndingAnchor: (NSString *)endingAnchor;
- (NSString *)truncateWordsToFit:(CGSize)fitSize
withInset:(CGFloat)inset
usingFont:(UIFont *)font
wordSplitter:(NSString*)splitter;
@end

Loading