Skip to content

About these classes

AliSoftware edited this page Jan 5, 2013 · 17 revisions

About these classes

OHAttributedLabel

This class allows you to use a UILabel with NSAttributedStrings, in order to display styled text with various style (mixed fonts, color, size, ...) in a unique label. It is a subclass of UILabel which adds an attributedText property. Use this property, instead of the text property, to set and get the NSAttributedString to display.

Note: This class is compatible with iOS4.3+ and has been developped before the release of the iOS6 SDK (before Apple added support for NSAttributedLabel in the UILabel class itself). It can still be used with the iOS6 SDK (the attributedText property hopefully match the one chosen by Apple) if you need support for eariler iOS versions or for the additional features it provides.

This class also support hyperlinks and URLs. It can automatically detect links in your text, color them and make them touchable; you can also add "custom links" in your text by attaching an URL to a range of your text and thus make it touchable, and even then catch the event of a touch on a link to act as you wish to.

NSAttributedString and NSTextChecking additions

In addition to this OHAttributedLabel class, you will also find a category of NS(Mutable)AttributedString to ease creation and manipulation of common attributes of NSAttributedString (to easily change the font, style, color, ... of a range of the string). See the header file NSAttributedString+Attributes.h for a list of those comodity methods.

Example:

// Build an NSAttributedString easily from a NSString
NSMutableAttributedString* attrStr = [NSMutableAttributedString attributedStringWithString:txt];
// Change font, text color, paragraph style
[attrStr setFont:[UIFont fontWithName:@"Helvetica" size:18]];
[attrStr setTextColor:[UIColor grayColor]];

OHParagraphStyle* paragraphStyle = [OHParagraphStyle defaultParagraphStyle];
paragraphStyle.textAlignment = kCTJustifiedTextAlignment;
paragraphStyle.lineBreakMode = kCTLineBreakByWordWrapping;
paragraphStyle.firstLineHeadIndent = 30.f; // indentation for first line
paragraphStyle.lineSpacing = 3.f; // increase space between lines by 3 points
[attrStr setParagraphStyle:paragraphStyle];

// Change the color and bold of only one part of the string
[attrStr setTextColor:[UIColor redColor] range:NSMakeRange(10,3)];
[attrStr setTextBold:YES range:NSMakeRange(10,8)];

// Add a link to a given portion of the string
[attrStr setLink:someNSURL range:NSMakeRange(8,20)];

There is also a category for NSTextCheckingResult that adds the extendedURL property. This property returns the same value as the URL value for standard link cases, and return a formatted Maps URL for NSTextCheckingTypeAddress link types, that will open Google Maps in iOS version before 6.0 and the Apple's Maps application in iOS 6.0 and later.

OHASMarkupParsers and simple markup to build your attributed strings easily

The library also comes with very simple tag parsers to help you build NSAttributedStrings easily using very simple tags.

  • the class OHASBasicHTMLParser can parse simple HTML tags like <b> and <u> to make bold and underlined text, change the font color using <font color='…'>, etc

  • the class OHASBasicMarkupParser can parse simple markup like *bold text*, _underlined text_ and change the font color using markup like {red|some red text} or {#ff6600|Yeah}.

      // Example 1: parse HTML in attributed string
      basicMarkupLabel.attributedText = [OHASBasicHTMLParser attributedStringByProcessingMarkupInAttributedString:basicMarkupLabel.attributedText];
    
      // Example 2: parse basic markup in string
      NSAttributedString* as = [OHASBasicMarkupParser attributedStringByProcessingMarkupInString:@"Hello *you*!"];
    
      // Example 3: process markup in-place in a mutable attributed string
      NSMutableAttributedString* mas = [NSMutableAttributedString attributedStringWithString:@"Hello *you*!"];
      [OHASBasicMarkupParser processMarkupInAttributedString:mas];
    

Note that OHASBasicHTMLParser is intended to be a very simple tool only to help you build attributed string more easily: this is not intended to be a real and complete HTML interpreter, and will never be. For improvements of this feature, like adding other tags or markup languages, refer to issue #88)

UIApperance support

The OHAttributedLabel class support the UIAppearance proxy API (available since iOS5). See selectors and properties marked using the UI_APPEARANCE_SELECTOR in the header.

This means that if you are targetting iOS5, you can customize all of your OHAttributedLabel links color and underline style to fit your application design, only in one call at the beginning of your application, instead of having to customize these for each instance.

For example, your could implement this in your application:didFinishLoadingWithOptions: delegate method to make all your OHAttributedLabel instances in your whole app display links in green and without underline instead of the default underlined blue:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [ [OHAttributedLabel appearance] setLinkColor:[UIColor colorWithRed:0.0 green:0.4 blue:0.0 alpha:1.0] ];
    [ [OHAttributedLabel appearance] setLinkUnderlineStyle:kCTUnderlineStyleNone ];
    return YES;
}