Giter Club home page Giter Club logo

mtllinklabel's Introduction

#MTLLinkLabel

Badge w/ Version Carthage compatible MIT License

MTLLinkLabel is linkable UILabel. Written in Swift.

Requirements

  • iOS 8.0+
  • Xcode 7.3+

Installation

You can install Carthage with Homebrew.

$ brew update
$ brew install carthage

specify it in your Cartfile

github "recruit-mtl/MTLLinkLabel"

And run carthage

$ carthage update --platform ios
$ pod init

specify it in your Podfile

platform :ios, '8.0'

target 'MTLLinkLabelExample' do

  use_frameworks!

  pod 'MTLLinkLabel', '~> 0.1.6'

end

And run CocoaPods

$ pod install

Usage

You can use MTLLinkLabel in Storyboard or XIB.

#####Drag and drop UILabel in your view.

#####Change UILabels custom class to 'LinkLabel', and Change module to 'MTLLinkLabel'.

You must change labels userInteractionEnabled property to true. Because, this labels properties default value is false.

label.userInteractionEnabled = true

#####Assign String to LinkLabels 'text' property.

label.text = "Hello. https://github.com/recruit-mtl/MTLLinkLabel"

Hello. https://github.com/recruit-mtl/MTLLinkLabel

--

Add custom link

You can add custom link in LinkLabel with range and Action at the time of the link selection.

let range = (text as NSString).rangeOfString("1829")
        
cell.label.addLink(
	NSURL(string: "https://www.google.co.jp/#q=1829")!, 
	range: range, 
	linkAttribute: [
		NSForegroundColorAttributeName: UIColor.redColor(),
		NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleSingle.rawValue
	]
) { (url) -> Void in
	
	let alert = UIAlertController(title: nil, message: url.absoluteString, preferredStyle: UIAlertControllerStyle.ActionSheet)
	alert.addAction(UIAlertAction(title: "Go", style: .Default, handler: { (action) -> Void in
		UIApplication.sharedApplication().openURL(url)
	}))
	alert.addAction(UIAlertAction(title: "Cancel", style: .Default, handler: nil))
	self.presentViewController(alert, animated: true, completion: nil)
}

--

Delegate

LinkLabelDelegate methods are optional method.

The default implementation is implemented in protocol extension.

public extension LinkLabelDelegate {
    
    func linkLabelExecuteLink(linkLabel: LinkLabel, text: String, result: NSTextCheckingResult) -> Void {
        
        if result.resultType.contains(.Link) {
            
            let pattern = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]+"
            if NSPredicate(format: "SELF MATCHES '\(pattern)'").evaluateWithObject(text) {
                UIApplication.sharedApplication().openURL(NSURL(string: "mailto:" + text)!)
                return
            }
            
            let httpText = !text.hasPrefix("http://") && !text.hasPrefix("https://") ? "http://" + text : text
            
            guard let url = NSURL(string: httpText) else { return }
            UIApplication.sharedApplication().openURL(url)
            
        }
        else if result.resultType.contains(.PhoneNumber) {
            let telURLString = "tel:" + text
            UIApplication.sharedApplication().openURL(NSURL(string: telURLString)!)
        }
    }
    
    func linkAttributeForLinkLabel(linkLabel: LinkLabel, checkingType: NSTextCheckingType) -> [String: AnyObject] {
        return [
            NSForegroundColorAttributeName: linkLabel.tintColor,
            NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleSingle.rawValue
        ]
    }
    
    func linkDefaultAttributeForCustomeLink(linkLabel: LinkLabel) -> [String: AnyObject] {
        return [
            NSForegroundColorAttributeName: linkLabel.tintColor,
            NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleSingle.rawValue
        ]
    }
    
    func linkLabelCheckingLinkType() -> NSTextCheckingTypes {
        return NSTextCheckingType.Link.rawValue
            | NSTextCheckingType.PhoneNumber.rawValue
    }
}

Licence

MIT

Author

mtllinklabel's People

Contributors

kokoron avatar ohida 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.