Skip to content

Commit

Permalink
added scrollToBottom method, removed [self display] from layoutSubviews
Browse files Browse the repository at this point in the history
  • Loading branch information
Maciej Cupial authored and Maciej Cupial committed Oct 26, 2014
1 parent ae29a14 commit 5335b1c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 34 deletions.
1 change: 1 addition & 0 deletions DWTagList/Classes/DWTagList.h
Expand Up @@ -42,6 +42,7 @@
- (void)setTags:(NSArray *)array;
- (void)display;
- (CGSize)fittedSize;
- (void)scroolToBottomAnimated: (BOOL)animated;

@end

Expand Down
71 changes: 37 additions & 34 deletions DWTagList/Classes/DWTagList.m
Expand Up @@ -87,35 +87,33 @@ - (void)setTags:(NSArray *)array
self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, sizeFit.width, sizeFit.height);
}
else {
[self setNeedsLayout];
[self display];
}
}

- (void)setTagBackgroundColor:(UIColor *)color
{
lblBackgroundColor = color;
[self setNeedsLayout];
[self display];
}

- (void)setTagHighlightColor:(UIColor *)color
{
self.highlightedBackgroundColor = color;
[self setNeedsLayout];
[self display];
}

- (void)setViewOnly:(BOOL)viewOnly
{
if (_viewOnly != viewOnly) {
_viewOnly = viewOnly;
[self setNeedsLayout];
[self display];
}
}

- (void)layoutSubviews
{
[super layoutSubviews];

[self display];
}

- (void)display
Expand All @@ -127,17 +125,17 @@ - (void)display
for (UIGestureRecognizer *gesture in [subview gestureRecognizers]) {
[subview removeGestureRecognizer:gesture];
}

[tagView.button removeTarget:nil action:nil forControlEvents:UIControlEventAllEvents];

[tagViews addObject:subview];
}
[subview removeFromSuperview];
}

CGRect previousFrame = CGRectZero;
BOOL gotPreviousFrame = NO;

NSInteger tag = 0;
for (id text in textArray) {
DWTagView *tagView;
Expand All @@ -148,15 +146,15 @@ - (void)display
else {
tagView = [[DWTagView alloc] init];
}


[tagView updateWithString:text
font:self.font
constrainedToWidth:self.frame.size.width - (self.horizontalPadding * 2)
padding:CGSizeMake(self.horizontalPadding, self.verticalPadding)
minimumWidth:self.minimumWidth
];

if (gotPreviousFrame) {
CGRect newRect = CGRectZero;
if (previousFrame.origin.x + previousFrame.size.width + tagView.frame.size.width + self.labelMargin > self.frame.size.width) {
Expand All @@ -167,10 +165,10 @@ - (void)display
newRect.size = tagView.frame.size;
[tagView setFrame:newRect];
}

previousFrame = tagView.frame;
gotPreviousFrame = YES;

[tagView setBackgroundColor:[self getBackgroundColor]];
[tagView setCornerRadius:self.cornerRadius];
[tagView setBorderColor:self.borderColor];
Expand All @@ -182,17 +180,17 @@ - (void)display
[tagView setDelegate:self];

tag++;

[self addSubview:tagView];

if (!_viewOnly) {
[tagView.button addTarget:self action:@selector(touchDownInside:) forControlEvents:UIControlEventTouchDown];
[tagView.button addTarget:self action:@selector(touchUpInside:) forControlEvents:UIControlEventTouchUpInside];
[tagView.button addTarget:self action:@selector(touchDragExit:) forControlEvents:UIControlEventTouchDragExit];
[tagView.button addTarget:self action:@selector(touchDragInside:) forControlEvents:UIControlEventTouchDragInside];
}
}

sizeFit = CGSizeMake(self.frame.size.width, previousFrame.origin.y + previousFrame.size.height + self.bottomMargin + 1.0f);
self.contentSize = sizeFit;
}
Expand All @@ -202,6 +200,11 @@ - (CGSize)fittedSize
return sizeFit;
}

- (void)scroolToBottomAnimated: (BOOL)animated
{
[self setContentOffset: CGPointMake(0.0, self.contentSize.height - self.bounds.size.height + self.contentInset.bottom) animated: animated];
}

- (void)touchDownInside:(id)sender
{
UIButton *button = (UIButton*)sender;
Expand All @@ -217,11 +220,11 @@ - (void)touchUpInside:(id)sender
if ([self.tagDelegate respondsToSelector:@selector(selectedTag:tagIndex:)]) {
[self.tagDelegate selectedTag:tagView.label.text tagIndex:tagView.tag];
}

if ([self.tagDelegate respondsToSelector:@selector(selectedTag:)]) {
[self.tagDelegate selectedTag:tagView.label.text];
}

if (self.showTagMenu) {
UIMenuController *menuController = [UIMenuController sharedMenuController];
[menuController setTargetRect:tagView.frame inView:self];
Expand Down Expand Up @@ -254,37 +257,37 @@ - (UIColor *)getBackgroundColor
- (void)setCornerRadius:(CGFloat)cornerRadius
{
_cornerRadius = cornerRadius;
[self setNeedsLayout];
[self display];
}

- (void)setBorderColor:(CGColorRef)borderColor
{
_borderColor = borderColor;
[self setNeedsLayout];
[self display];
}

- (void)setBorderWidth:(CGFloat)borderWidth
{
_borderWidth = borderWidth;
[self setNeedsLayout];
[self display];
}

- (void)setTextColor:(UIColor *)textColor
{
_textColor = textColor;
[self setNeedsLayout];
[self display];
}

- (void)setTextShadowColor:(UIColor *)textShadowColor
{
_textShadowColor = textShadowColor;
[self setNeedsLayout];
[self display];
}

- (void)setTextShadowOffset:(CGSize)textShadowOffset
{
_textShadowOffset = textShadowOffset;
[self setNeedsLayout];
[self display];
}

- (void)dealloc
Expand All @@ -300,7 +303,7 @@ - (void)tagViewWantsToBeDeleted:(DWTagView *)tagView {
NSMutableArray *mTextArray = [self.textArray mutableCopy];
[mTextArray removeObject:tagView.label.text];
[self setTags:mTextArray];

if ([self.tagDelegate respondsToSelector:@selector(tagListTagsChanged:)]) {
[self.tagDelegate tagListTagsChanged:self];
}
Expand All @@ -322,12 +325,12 @@ - (id)init
[_label setBackgroundColor:[UIColor clearColor]];
[_label setTextAlignment:NSTextAlignmentCenter];
[self addSubview:_label];

_button = [UIButton buttonWithType:UIButtonTypeCustom];
_button.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
[_button setFrame:self.frame];
[self addSubview:_button];

[self.layer setMasksToBounds:YES];
[self.layer setCornerRadius:CORNER_RADIUS];
[self.layer setBorderColor:BORDER_COLOR];
Expand All @@ -340,25 +343,25 @@ - (void)updateWithString:(id)text font:(UIFont*)font constrainedToWidth:(CGFloat
{
CGSize textSize = CGSizeZero;
BOOL isTextAttributedString = [text isKindOfClass:[NSAttributedString class]];

if (isTextAttributedString) {
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithAttributedString:text];
[attributedString addAttributes:@{NSFontAttributeName: font} range:NSMakeRange(0, ((NSAttributedString *)text).string.length)];

textSize = [attributedString boundingRectWithSize:CGSizeMake(maxWidth, 0) options:NSStringDrawingUsesLineFragmentOrigin context:nil].size;
_label.attributedText = [attributedString copy];
} else {
textSize = [text sizeWithFont:font forWidth:maxWidth lineBreakMode:NSLineBreakByTruncatingTail];
_label.text = text;
}

textSize.width = MAX(textSize.width, minimumWidth);
textSize.height += padding.height*2;

self.frame = CGRectMake(0, 0, textSize.width+padding.width*2, textSize.height);
_label.frame = CGRectMake(padding.width, 0, MIN(textSize.width, self.frame.size.width), textSize.height);
_label.font = font;

[_button setAccessibilityLabel:self.label.text];
}

Expand Down

0 comments on commit 5335b1c

Please sign in to comment.