From 3c0eb9fa5d971e31aadab47578c8b5cf1c70cc97 Mon Sep 17 00:00:00 2001 From: Ben Nicholas Date: Fri, 8 Aug 2014 11:42:07 -0400 Subject: [PATCH] Truncate the end of long token titles MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit By changing the autoresize rules on the token label, we can force the label to appropriately size itself to the token title. Before, a token’s label was sized to its content, meaning that if it was actually longer than a line in the token field, it freely overflowed and was clipped by the outer VENToken’s view. I also exposed the line break mode for VENTokens as a whole, allowing a client application to change the line break mode to one appropriate to their data (breaking in the middle or beginning of the line, for example. --- VENTokenField/VENToken.h | 2 ++ VENTokenField/VENToken.m | 8 ++++++++ VENTokenField/VENToken.xib | 2 +- VENTokenFieldSample/ViewController.m | 4 ++++ 4 files changed, 15 insertions(+), 1 deletion(-) diff --git a/VENTokenField/VENToken.h b/VENTokenField/VENToken.h index 21768cb..6e72f4e 100644 --- a/VENTokenField/VENToken.h +++ b/VENTokenField/VENToken.h @@ -28,6 +28,8 @@ @property (copy, nonatomic) void (^didTapTokenBlock) (void); @property (strong, nonatomic) UIColor *colorScheme; ++ (void)setLineBreakMode:(NSLineBreakMode)lineBreakMode; + - (void)setTitleText:(NSString *)text; @end diff --git a/VENTokenField/VENToken.m b/VENTokenField/VENToken.m index 4435a83..125b3f5 100644 --- a/VENTokenField/VENToken.m +++ b/VENTokenField/VENToken.m @@ -28,8 +28,15 @@ @interface VENToken () @property (strong, nonatomic) IBOutlet UIView *backgroundView; @end +static NSLineBreakMode _lineBreakMode = NSLineBreakByTruncatingTail; + @implementation VENToken ++ (void)setLineBreakMode:(NSLineBreakMode)lineBreakMode +{ + _lineBreakMode = lineBreakMode; +} + - (id)initWithFrame:(CGRect)frame { self = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:self options:nil] firstObject]; @@ -45,6 +52,7 @@ - (void)setUpInit self.tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapToken:)]; self.colorScheme = [UIColor blueColor]; self.titleLabel.textColor = self.colorScheme; + self.titleLabel.lineBreakMode = _lineBreakMode; [self addGestureRecognizer:self.tapGestureRecognizer]; } diff --git a/VENTokenField/VENToken.xib b/VENTokenField/VENToken.xib index f14b7d7..9b891f8 100644 --- a/VENTokenField/VENToken.xib +++ b/VENTokenField/VENToken.xib @@ -17,7 +17,7 @@