Adding IGLabel to a UIView
Send Feedback

Glossary Item Box

Topic Overview

Purpose

This topic provides basic information about creating an instance of the IGLabel and styling its text to help you get up and running with this control.

Required background

The following table lists the topics required as a prerequisite to understanding this topic.

Topic

Purpose

Adding the Label Framework File

This topic explains the requirements for adding the label framework file to an Xcode project, a requirement for using the IGLabel control.


In this topic


Introduction

Adding IGLabel summary

To get started using IGLabel add it to a UIView by allocating the class object using one of the common initialization constructor methods to instantiate the control. Shown below is an example of instantiating the control with a frame size equal to that of the device’s screen.

In C#:

IGLabel labelView = new IGLabel();
labelView.Frame = new RectangleF(0, 0, this.View.Frame.Size.Width, this.View.Frame.Size.Height);

In Objective-C:

IGLabel* labelView = [[IGLabel alloc]initWithFrame:
        CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.width)];

After allocating an instance of the IGLabel, you can call its properties and methods. For example, you may want to configure the label’s display text using the text property.

In C#:

labelView.text = “The quick brown fox jumped over the lazy dogs.”;

In Objective-C:

labelView.text = @”The quick brown fox jumped over the lazy dogs.”;

With text now set on the IGLabel, you may be required to stylize the text using either of these two label text styling methods.

  • -(void)addAttributes:(NSDictionary *)attributes range:(NSRange)styleRange;
  • -(void)addAttributes:(NSDictionary *)attributes forRegEx:(NSString *)expression error:(NSError **)error;

The IGLabel has considerable functionality beyond these basic capabilities, be sure to look at the IGLabel topic and the Configuring IGLabel topic for more in-depth feature specific information.


Instantiating and Styling the IGLabel – Code Example

Description

To start using the IGLabel, it is first necessary to create an instance of it and add it to your view. After setting the text property, use the addAttributes method found on the IGLabel with a NSDictionary to add styling key/value pairs. Additional information about using text style attributes is available in the Configuring Text Styling Attributes topic.

Code

The code below overrides the UIViewController’s viewDidLoad method to instantiate the IGLabel, adds attributes to the word “The” that change its font size to 28 points and the text color to red.

In C#:

public override void ViewDidLoad ()
{
      base.ViewDidLoad ();
      
      IGLabel labelView = new IGLabel();
      labelView.Frame = new RectangleF(0, 0, this.View.Frame.Size.Width, this.View.Frame.Size.Height);
      labelView.Text = "The quick brown fox jumped over the lazy dogs.";
      NSMutableDictionary attributes = new NSMutableDictionary();
      attributes.Add(IGLabelConstants.IGTextStyleFontAttributeName, UIFont.SystemFontOfSize(28.0f));
      attributes.Add(IGLabelConstants.IGTextStyleForegroundColorAttributeName, UIColor.Red);
      labelView.AddAttributes(attributes, new NSRange(0, 3));
      (this.View).AddSubview(labelView);
}

In Objective-C:


-(void)viewDidLoad) { IGLabel* labelView = [[IGLabel alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.width)];
labelView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
labelView.text = @”The quick brown fox jumped over the lazy dogs.”;
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys: [UIFont systemFontOfSize:28], IGTextStyleFontAttributeName, [UIColor redColor], IGTextStyleForegroundColorAttributeName, nil];
[label addAttributes:attributes range:NSMakeRange(0, 3)]; [self.view addSubview:labelView]; }

Related Content

Topics

The following topics provide additional information related to this topic.

Topic

Purpose

This section serves as an introduction to the IGLabel’s key features and functionalities.