Skip to content
AliSoftware edited this page Jan 18, 2013 · 20 revisions

How to use OHAttributedLabel

How to use in your project

There are two possible methods to include these classes in your project:

  1. Using Cocoapods:

    • add pod "OHAttributedLabel" to your Podfile
  2. Manually:

    • Include the OHAttributedLabel.xcodeproj project in your Xcode4 workspace
    • Build this OHAttributedLabel.xcodeproj project once for the "iOS Device" (not the simulator) (1)
    • Add libOHAttributedLabel.a and CoreText.framework to your "Link Binary With Libraries" Build Phase of your app project.
      • To do that, select your Application project in the Project Navigator on the left and go to the "Build Phase" tab. Open the "Link Binary With Libraries" section and use the "+" button to add the library + the CoreText framework
    • Select the libOHAttributedLabel.a that has just been added to your app project in your Project Navigator on the left, and change the "Location" dropdown in the File Inspector to "Relative to Build Products" (1)
    • Add the -ObjC flag in the "Other Linker Flags" Build Setting (if not present already)

Then in your application code, when you want to make use of OHAttributedLabel methods, you only need to import the headers with #import <OHAttributedLabel/OHAttributedLabel.h> or #import <OHAttributedLabel/NSAttributedString+Attributes.h> etc.

(1) Note: These two steps are only necessary to avoid a bug in Xcode4 that would otherwise make Xcode fail to detect implicit dependencies between your app and the lib. See below.

Troubleshooting for library linking issues

If you use an Xcode version prior to 4.2 (and LLVM version prior to 3.0)

You will need to add the -all_load flag in addition to the -ObjC flag, for the category to be loaded from the library. This is due to a bug in LLVM which has been fixed in LLVM 3.0 / Xcode 4.2 and is now fixed. Updating your Xcode version is strongly recommended instead of this workaround.

If your workspace can't detect implicit dependencies

If you have link errors saying that the symbols for some OHAttributedLabel classes can't be found, this is probably because Xcode didn't correctly detect the dependency between your application project and the OHAttributedLabel project, and thus did not build the OHAttributedLabel project before your application.

To solve this:

  • Make sure that the libOHAttributedLabel.a present in your application's project (in the Project Navigator on the left) is referenced as "Relative to Build Products" (and not "Relative to Group" or "Relative to Project"). see the File Inspector on the right
  • Make sure that the relative path of this libOHAttributedLabel.a is only "libOHAttributedLabel.a" (as it is relative to your Build Products directory) and not some fancy relative path with a lot of "../../..". If you have some fancy path (known Xcode bug sadly still not fixed by Apple), you will need to edit your project.pbxproj file (inside your .xcodeproj bundle) to fix this ugly path.

If you declared configurations other than the standard "Debug" and "Release" configurations

If you have configurations other than "Debug" and "Release" in your application project (for example say a configuration named "Distribution"), be sure to create configurations with the same name for all your other projects in the same workspace, so that when Xcode build your app with those configurations (e.g. "Distribution") and build the dependent libraries in the process, it uses a corresponding configuration for the dependent projects too

Xcode can't guess than when you build your App using configuration "Distrib" it should compile the lib with configuration "Debug" or "Release", if no configuration "Distrib" is present in the library project!

Sample code & Other documentation

There is no explicit docset or documentation of the class yet sorry (never had time to write one), but

  • The method names should be self-explanatory (hopefully) as I respect the standard ObjC naming conventions.
  • There are doxygen/javadoc-like documentation in the headers that should also help you describe the methods
  • The provided example should also demonstrate quite every typical usages — including justifying the text, dynamically changing the style/attributes of a range of text, adding custom links, make special links with a custom behavior (like catching @mention and #hashtags), and customizing the appearance/color of links.