This was forked from hsusmita/SwiftResponsiveLabel
A UILabel subclass which responds to touch on specified patterns. It has the following features:
- It can detect pattern specified by regular expression and apply style such as font, color etc.
- It allows to replace default ellipse with tappable attributed string to mark truncation
- Convenience methods are provided to detect hashtags, username handler and URLs
the original of SwiftResponsiveLabel is not good when use in UITableViewCell with autolayout and AutoDimension in UITableView and with a dynamic Label height (not fix height), so, i fix this.
- correct truncation token with new line string
- correct truncation token when use in UITableViewCell with autolayout and AutoDimension in UITableView
#Installation
Add following lines in your pod file
pod 'SwiftResponsiveLabel', '2.1'
#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.
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])
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])
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])
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
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)
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.