Code Monkey home page Code Monkey logo

swiftresponsivelabel's Introduction

Version License Documentation

SwiftResponsiveLabel

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 such as 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 'SwiftResponsiveLabel', '2.3'

#Usage

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

import SwiftResponsiveLabel

In interface builder, set the custom class of your UILabel to SwiftResponsiveLabel.

Username Handle Detection

let userHandleTapAction = PatternTapResponder{ (tappedString)-> (Void) in
let messageString = "You have tapped user handle:" + tappedString
self.messageLabel.text = messageString
}
let dict = [NSForegroundColorAttributeName: UIColor.greenColor(), 
NSBackgroundColorAttributeName: UIColor.blackColor()]
self.customLabel.enableUserHandleDetection([NSForegroundColorAttributeName:UIColor.grayColor(),
RLHighlightedAttributesDictionary: dict, RLTapResponderAttributeName:userHandleTapAction])

URL Detection

let URLTapAction = PatternTapResponder{(tappedString)-> (Void) in
let messageString = "You have tapped URL: " + tappedString
self.messageLabel.text = messageString
}
self.customLabel.enableURLDetection([NSForegroundColorAttributeName:UIColor.blueColor(), RLTapResponderAttributeName:URLTapAction])

HashTag Detection

let hashTagTapAction = PatternTapResponder { (tappedString)-> (Void) in
let messageString = "You have tapped hashTag:" + tappedString
self.messageLabel.text = messageString
}
let dict = [NSForegroundColorAttributeName: UIColor.redColor(), NSBackgroundColorAttributeName: UIColor.blackColor()]
customLabel.enableHashTagDetection([RLHighlightedAttributesDictionary : dict, NSForegroundColorAttributeName: UIColor.cyanColor(),
RLTapResponderAttributeName:hashTagTapAction])

Custom Truncation Token

Set attributed string as truncation token
let action = PatternTapResponder {(tappedString)-> (Void) in
print("You have tapped token string")
}
let dict = [RLHighlightedBackgroundColorAttributeName:UIColor.blackColor(),
RLHighlightedForegroundColorAttributeName:UIColor.greenColor(), RLTapResponderAttributeName:action]
let token = NSAttributedString(string: "...More", attributes: [NSFontAttributeName: customLabel.font, 
NSForegroundColorAttributeName:UIColor.brownColor(), RLHighlightedAttributesDictionary: dict])
customLabel.attributedTruncationToken = token
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

let action = PatternTapResponder {(tappedString)-> (Void) in
print("You have tapped token image")
}
self.customLabel.setTruncationIndicatorImage(UIImage(named: "check")!, withSize: CGSize(width: 20.0, height: 20.0), andAction: action)
Set from interface builder

Screenshots

References

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

The following articles were helpful in enhancing the functionalities.

swiftresponsivelabel's People

Contributors

hsusmita avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

swiftresponsivelabel's Issues

Not work with new line string

The label does not work with new line string.
Example: string = "a\nb\nc\nd\n\e\n\f\n" and numberOfLines = 3
For UITableViewAutomaticDimension in UITableView.

swift 4.0 support?

Hello!

Could you please update your control to the latest swift version? Thanks in Advance!

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.