Coder Social home page Coder Social logo

responsivelabel's Introduction

Version License Platform

#ResponsiveLabel A UILabel subclass which responds to touch on specified patterns. It has the following features:

  1. It can detect pattern specified by regular expression and apply style like font, color etc.
  2. It allows to replace default ellipse with tappable attributed string to mark truncation
  3. Convenience methods are provided to detect hashtags, username handler and URLs

#Installation

Add following lines in your pod file
pod 'ResponsiveLabel', '~> 1.0.8'

#Usage

The following snippets explain the usage of public methods. These snippets assume an instance of ResponsiveLabel named "customLabel".

Pattern Detection

//Detects email in text
NSString *emailRegexString = @"[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}";
NSError *error;
NSRegularExpression *regex = [[NSRegularExpression alloc]initWithPattern:emailRegexString
options:0
error:&error];
PatternDescriptor *descriptor = [[PatternDescriptor alloc]initWithRegex:regex withSearchType:PatternSearchTypeAll 
withPatternAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]}];
[self.customLabel enablePatternDetection:descriptor];

String Detection

self.customLabel.userInteractionEnabled = YES;
PatternTapResponder tapResponder = ^(NSString *string) {
    NSLog(@"tapped = %@",string);
};
[self.customLabel enableStringDetection:@"text" withAttributes:@{NSForegroundColorAttributeName:[UIColor redColor],
                                                                 RLTapResponderAttributeName: tapResponder}];

Array of String Detection

self.customLabel.userInteractionEnabled = YES;
PatternTapResponder stringTapAction = ^(NSString *tappedString) {
    NSLog(@"tapped string = %@",tappedString);
  };
[self.customLabel enableDetectionForStrings:@[@"text",@"long"] withAttributes:@{NSForegroundColorAttributeName:[UIColor brownColor],
                                                                                  RLTapResponderAttributeName:stringTapAction}];

HashTag Detection

self.customLabel.userInteractionEnabled = YES;
PatternTapResponder hashTagTapAction = ^(NSString *tappedString) {
NSLog(@"HashTag Tapped = %@",tappedString);
};
[self.customLabel enableHashTagDetectionWithAttributes:
@{NSForegroundColorAttributeName:[UIColor redColor], RLTapResponderAttributeName:hashTagTapAction}];

Username Handle Detection

self.customLabel.userInteractionEnabled = YES;
PatternTapResponder userHandleTapAction = ^(NSString *tappedString){
NSLog(@"Username Handler Tapped = %@",tappedString);
};
[self.customLabel enableUserHandleDetectionWithAttributes:
@{NSForegroundColorAttributeName:[UIColor grayColor],RLTapResponderAttributeName:userHandleTapAction}];

URL Detection

self.customLabel.userInteractionEnabled = YES;
PatternTapResponder urlTapAction = ^(NSString *tappedString) {
NSLog(@"URL Tapped = %@",tappedString);
};
[self.customLabel enableURLDetectionWithAttributes:
@{NSForegroundColorAttributeName:[UIColor cyanColor],NSUnderlineStyleAttributeName:[NSNumber
numberWithInt:1],RLTapResponderAttributeName:urlTapAction}];

Highlight Patterns On Tap

To highlight patterns, one can set the attributes:

  • RLHighlightedForegroundColorAttributeName
  • RLHighlightedBackgroundColorAttributeName
self.customLabel.userInteractionEnabled = YES;
PatternTapResponder userHandleTapAction = ^(NSString *tappedString){
NSLog(@"Username Handler Tapped = %@",tappedString);
};
[self.customLabel enableUserHandleDetectionWithAttributes:
@{NSForegroundColorAttributeName:[UIColor grayColor],RLHighlightedForegroundColorAttributeName:[UIColor greenColor],RLHighlightedBackgroundColorAttributeName:[UIColor blackColor],RLTapResponderAttributeName:userHandleTapAction}];

Custom Truncation Token

Set attributed string as truncation token
NSString *expansionToken = @"Read More ...";
NSString *str = @"Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";
NSMutableAttributedString *attribString = [[NSMutableAttributedString alloc]initWithString:kExpansionToken attributes:@{NSForegroundColorAttributeName:[UIColor blueColor],NSFontAttributeName:self.customLabel.font}];
[self.customLabel setAttributedTruncationToken:attribString withAction:^(NSString *tappedString) {
NSLog(@"Tap on truncation text");
}];
[self.customLabel setText:str withTruncation:YES];
Set image as truncation token

The height of image size should be approximately equal to or less than the font height. Otherwise the image will not be rendered properly

[self.customLabel setTruncationIndicatorImage:[UIImage imageNamed:@"more_image"] withSize:CGSizeMake(25, 5) andAction:^(NSString *tappedString) {
    NSLog(@"tapped on image");
 }];
Set from interface builder

Screenshots

References

The underlying implementation of ResponsiveLabel is based on KILabel(https://github.com/Krelborn/KILabel). ResponsiveLabel is made flexible to enable detection of any pattern specified by regular expression.

The following articles were helpful in enhancing the functionalities.

responsivelabel's People

Contributors

hsusmita avatar thelastjedi avatar

Watchers

James Cloos avatar Peter.Zhang (张骋) 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.