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

Truncate the end of long token titles #15

Open
wants to merge 1 commit 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
2 changes: 2 additions & 0 deletions VENTokenField/VENToken.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
@property (copy, nonatomic) void (^didTapTokenBlock) (void);
@property (strong, nonatomic) UIColor *colorScheme;

+ (void)setLineBreakMode:(NSLineBreakMode)lineBreakMode;

- (void)setTitleText:(NSString *)text;

@end
8 changes: 8 additions & 0 deletions VENTokenField/VENToken.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,15 @@ @interface VENToken ()
@property (strong, nonatomic) IBOutlet UIView *backgroundView;
@end

static NSLineBreakMode _lineBreakMode = NSLineBreakByTruncatingTail;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personal preference, but can we make this

@property (assign, nonatomic) NSLineBreakMode lineBreakMode;

instead of a static variable?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason I did it this way is so that it's consistent across VENToken instances. By scoping it statically, it avoids situations where some are breaking at the beginning and some at the end. Essentially, this is a class property rather than an instance property. If you would prefer it as an instance property though, I can convert it over and make sure it gets set on initialization.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer instance properties to avoid unintentional side-effects. At least in the Venmo app, we use VENTokenField in two separate places, and it could be the case that we want to use different line break styles for each (or at least have full control of its behavior in each context).

Let me know what you think, and thanks for the pull request! 😸


@implementation VENToken

+ (void)setLineBreakMode:(NSLineBreakMode)lineBreakMode
{
_lineBreakMode = lineBreakMode;
}

- (id)initWithFrame:(CGRect)frame
{
self = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:self options:nil] firstObject];
Expand All @@ -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];
}

Expand Down
2 changes: 1 addition & 1 deletion VENTokenField/VENToken.xib
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
</view>
<label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Octo Cat" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="OfV-hv-jq4">
<rect key="frame" x="0.0" y="6" width="121" height="25"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" name="HelveticaNeue" family="Helvetica Neue" pointSize="15.5"/>
<color key="textColor" red="0.21568627450980393" green="0.58431372549019611" blue="0.82352941176470584" alpha="1" colorSpace="calibratedRGB"/>
<nil key="highlightedColor"/>
Expand Down
4 changes: 4 additions & 0 deletions VENTokenFieldSample/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#import "ViewController.h"
#import "VENTokenField.h"
#import "VENToken.h"

@interface ViewController () <VENTokenFieldDelegate, VENTokenFieldDataSource>
@property (weak, nonatomic) IBOutlet VENTokenField *tokenField;
Expand All @@ -19,6 +20,9 @@ @implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];

[VENToken setLineBreakMode:NSLineBreakByTruncatingMiddle];

self.names = [NSMutableArray array];
self.tokenField.delegate = self;
self.tokenField.dataSource = self;
Expand Down