Skip to content

Visput/VPAttributedFormat

Repository files navigation

VPAttributedFormat v1.2.5

VPAttributedFormat project represents categories:

  • NSAttributedString+VPAttributedFormat
  • UILabel+VPAttributedFormat
  • UITextView+VPAttributedFormat
  • UITextField+VPAttributedFormat
  • UIButton+VPAttributedFormat

These categories provide methods for building attributed string based on attributed format and arguments that should satisfy this format.
The most suitable case of using these categories is text controls with variable attributed text configured in interface builder.
You need set correct string format to attributed text and configure necessary attributes.
Then you need pass necessary arguments in code by using methods of these categories.
All standard controls that work with attributed strings are supported: UILabel, UITextView, UITextField, UIButton.
See Usage and Examples sections for more details.
Full documentation is available on CocoaDocs.

example

Installation

Cocoa Pods

Add to your Podfile:

pod "VPAttributedFormat"
Drag&Drop
  1. Drag and drop VPAttributedFormat.xcodeproj to your project;
  2. Add VPAttributedFormat to Build Settings -> Target Dependencies;
  3. Add VPAttributedFormat.framework to Build Settings -> Link Binary With Libraries;
  4. Add "-all_load" flag to Build Settings -> Other Linker Flags.

Usage

Import framework header or module
// Objective C
// By header
#import <VPAttributedFormat/VPAttributedFormat.h>
// By module
@import VPAttributedFormat;
// Swift
import VPAttributedFormat
Set correct format and attributes for text control in interface builder

usage

Create IBOutlet and link it with text control
// Objective C
@property (nonatomic, weak) IBOutlet UILabel *textLabel;
// Swift
@IBOutlet weak var textLabel: UILabel!
Populate format with necessary arguments
Set arguments multiple times

Use UILabel / UITextView / UITextField / UIButton category methods.
Set keepFormat parameter to YES.

// Objective C
NSString *hot = @"Hot";
NSString *cold = @"Cold";

[self.textLabel vp_setAttributedTextFormatArguments:YES, hot, cold];
// Swift
let hot = "Hot"
let cold = "Cold"

var arguments: [CVarArgType] = [hot, cold]
withVaList(arguments) { pointer in
  textLabel.vp_setAttributedTextFormatArguments(pointer, keepFormat: true);
}
Set arguments only once

Use UILabel / UITextView / UITextField / UIButton category methods.
Set keepFormat parameter to NO.

// Objective C
NSString *hot = @"Hot";
NSString *cold = @"Cold";

[self.textLabel vp_setAttributedTextFormatArguments:NO, hot, cold];
// Swift
let hot = "Hot"
let cold = "Cold"

var arguments: [CVarArgType] = [hot, cold]
withVaList(arguments) { pointer in
  textLabel.vp_setAttributedTextFormatArguments(pointer, keepFormat: false);
}
Set attributed text directly

Use NSAttributedString category methods.
It's suitable for situations when attributed format comes from another part of application.

// Objective C
NSString *hot = @"Hot";
NSString *cold = @"Cold";

self.textLabel.attributedText = [NSAttributedString vp_attributedStringWithAttributedFormat:self.textLabel.attributedText,
                                 hot,
                                 cold];
// Swift
let hot = "Hot"
let cold = "Cold"

var arguments: [CVarArgType] = [hot, cold]
textLabel.attributedText = withVaList(arguments) { pointer in
    NSAttributedString.vp_attributedStringWithAttributedFormat(textLabel.attributedText, arguments: pointer)
}
Enjoy!

result

System Requirements

It requires building with iOS SDK 6.0 and later.
It can be used in Objective C and Swift code.

Examples

VPAttributedFormatExample is an example project. It provides Basic and Pro format examples.

License

VPAttributedFormat is released under the MIT license. See LICENSE for details.

About

NSAttributedString+VPAttributedFormat

Resources

License

Stars

Watchers

Forks

Packages

No packages published