Coder Social home page Coder Social logo

travelc / mdhtmllabel Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mattdonnelly/mdhtmllabel

0.0 2.0 0.0 1.42 MB

A lightweight class for rendering text containing HTML tags on iOS 6.0+

License: MIT License

Objective-C 99.42% Ruby 0.58%

mdhtmllabel's Introduction

MDHTMLLabel

MDHTMLLabel is a lightweight, easy to use replacement for UILabel which allows you to fully customize the appearence of the text using HTML (with a few added features thanks to CoreText), as well letting you handle whenever a user taps or holds down on link and automatically detect ones not wrapped in anchor tags.

Features:

  • Fully style text using HTML
  • Link interaction handling
  • Automatic detection of URLs not wrapped in anchor tags
  • Font tag support
  • Bold and italic text styles
  • Color and stroke styles
  • Text shadows
  • Indentation, kerning and line spacing settings
  • Text insets
  • Support for iOS 6.0+

MDHTMLLabel

Getting Started

Installation with CocoaPods

The recommended method of installation is to use CocoaPods which is a dependency manager for Objective-C, that automates and simplifies the process of using 3rd-party libraries in your projects.

Include the following in your Podfile:

platform :ios, '6.0'
pod 'MDHTMLLabel'

You can also choose to simply drag the source files in to your project instead.

How To Use

Just import the header file and create an instance of MDHTMLLabel like you would with UILabel.

#import <MDHTMLLabel/MDHTMLLabel.h>

...

- (void)viewDidLoad
{
    MDHTMLLabel *htmlLabel = [[MDHTMLLabel alloc] initWithFrame:frame];
    htmlLabel.delegate = self;
    htmlLabel.htmlText = htmlText;

    [self.view addSubview:htmlLabel];
}

Link Interaction

MDHTMLLabel automatically creates user interactable links inside the label to represent HTML anchor tags and allows you to detect when a user taps or holds down on a link by implementing an optional delgate. The delegate has two methods for you to implement:

- (void)HTMLLabel:(MDHTMLLabel *)label didSelectLinkWithURL:(NSURL *)URL
- (void)HTMLLabel:(MDHTMLLabel *)label didHoldLinkWithURL:(NSURL *)URL

It will also detect any URLs inside the text that aren't wrapped in anchor tags and create links for them too.

Customizing Appearence

Changing the appearence of MDHTMLLabel can be done similarly to UILabel, but with many more features. Inline styling can be done using HTML font tags which allows you use different combinations of fonts, colors and sizes throughout the text. Changing fonts is done using the face attribute and must be set to a string that's interpretable by +UIFont fontWithName:.

Here's an example of how it's used in the demo app.

NSString *const kDemoText = @"... <font face='Didot-Italic' size='19'>customise</font>  ..."

MDHTMLLabel also allows you to change the appearence of links inside the text using the linkAttributes property which takes an NSDictionary of values representing how links should be styled. You can also set the appearence for highlighted links when the user taps one too using the activeLinkAttributes property and the appearence of links when the tint color of the view changes using the inactiveLinkAttributes.

MDHTMLLabel *htmlLabel = [[MDHTMLLabel alloc] initWithFrame:frame];
htmlLabel.delegate = self;
htmlLabel.htmlText = htmlText;

htmlLabel.linkAttributes = @{ NSForegroundColorAttributeName: [UIColor blueColor],
                              NSFontAttributeName: [UIFont boldSystemFontOfSize:htmlLabel.font.pointSize],
                              NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle) };

htmlLabel.activeLinkAttributes = @{ NSForegroundColorAttributeName: [UIColor redColor],
                                    NSFontAttributeName: [UIFont boldSystemFontOfSize:htmlLabel.font.pointSize],
                                    NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle) };

Special Thanks

MDHTMLLabel is based off of RTLabel and wouldn't have been possible to make without it

License

MDHTMLLabel is available under the MIT license. See the LICENSE file for more info.

mdhtmllabel's People

Contributors

mattdonnelly avatar aprato avatar sspitzer avatar

Watchers

James Cloos avatar  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.