Skip to content

Commit

Permalink
[Designable] opt-out designable and inspectable via subspec
Browse files Browse the repository at this point in the history
  • Loading branch information
jhersh committed Jul 16, 2015
1 parent 31c3531 commit b3cbe57
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 23 deletions.
6 changes: 4 additions & 2 deletions Example/Podfile.lock
Expand Up @@ -5,7 +5,9 @@ PODS:
- KIF/XCTest (= 3.2.3)
- KIF/XCTest (3.2.3)
- OCMock (3.1.2)
- TTTAttributedLabel (2.0.0)
- TTTAttributedLabel (2.0.0):
- TTTAttributedLabel/Default (= 2.0.0)
- TTTAttributedLabel/Default (2.0.0)

DEPENDENCIES:
- Expecta
Expand All @@ -23,6 +25,6 @@ SPEC CHECKSUMS:
FBSnapshotTestCase: 3dc3899168747a0319c5278f5b3445c13a6532dd
KIF: a94bffe9c97e449e44f8fa481c53243d21309e1e
OCMock: a10ea9f0a6e921651f96f78b6faee95ebc813b92
TTTAttributedLabel: 92aaa37822677380b7ec0baff50bd8010eef08f8
TTTAttributedLabel: 0c561e59d48803ba0f87123b218744383d3b1e67

COCOAPODS: 0.37.2
49 changes: 44 additions & 5 deletions README.md
Expand Up @@ -46,15 +46,56 @@ As of version 1.10.0, `TTTAttributedLabel` supports VoiceOver through the `UIAc

[CocoaPods](http://cocoapods.org) is the recommended method of installing `TTTAttributedLabel`. Simply add the following line to your `Podfile`:

#### Podfile

```ruby
# Podfile

pod 'TTTAttributedLabel'

# Alternatively, to explicitly disable IB annotations
# (see IBDesignable below)
pod 'TTTAttributedLabel/NoDesignable'
```

## `IBDesignable`

`TTTAttributedLabel` includes `IBInspectable` and `IB_DESIGNABLE` annotations to enable configuring the label inside Interface Builder. However, if you see these warnings when building...

```
IB Designables: Failed to update auto layout status: Failed to load designables from path (null)
IB Designables: Failed to render instance of TTTAttributedLabel: Failed to load designables from path (null)
```

...then you are likely using `TTTAttributedLabel` as a static library, which does not support IB annotations. Some workarounds include:

- Install `TTTAttributedLabel` as a dynamic framework using CocoaPods with `use_frameworks!` in your `Podfile`, or with Carthage
- Install `TTTAttributedLabel` by dragging its source files to your project
- Install `TTTAttributedLabel` as a static library with its `NoDesignable` subspec, which disables the label's IB annotations. Update your `Podfile` to `pod 'TTTAttributedLabel/NoDesignable'`.

## Usage

``` objective-c
`TTTAttributedLabel` can display both plain and attributed text: just pass an `NSString` or `NSAttributedString` to the `setText:` setter. Never assign to the `attributedText` property.

```objc
// NSAttributedString

TTTAttributedLabel *attributedLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectZero];

NSAttributedString *attString = [[NSAttributedString alloc] initWithString:@"Tom Bombadil"
attributes:@{
(id)kCTForegroundColorAttributeName : (id)[UIColor redColor].CGColor,
NSFontAttributeName : [UIFont boldSystemFontOfSize:16],
NSKernAttributeName : [NSNull null],
(id)kTTTBackgroundFillColorAttributeName : (id)[UIColor greenColor].CGColor
}];

// The attributed string is directly set, without inheriting any other text
// properties of the label.
attributedLabel.text = attString;
```
```objc
// NSString
TTTAttributedLabel *label = [[TTTAttributedLabel alloc] initWithFrame:CGRectZero];
label.font = [UIFont systemFontOfSize:14];
label.textColor = [UIColor darkGrayColor];
Expand Down Expand Up @@ -83,8 +124,6 @@ NSString *text = @"Lorem ipsum dolor sit amet";

First, we create and configure the label, the same way you would instantiate `UILabel`. Any text properties that are set on the label are inherited as the base attributes when using the `-setText:afterInheritingLabelAttributesAndConfiguringWithBlock:` method. In this example, the substring "ipsum dolar", would appear in bold, such that the label would read "Lorem **ipsum dolar** sit amet", in size 14 Helvetica, with a dark gray color.

The normal `setText:` setter accepts both `NSString` and `NSAttributedString`; in the latter case, the attributed string is directly set, without inheriting the base style of the label.
### Links and Data Detection

In addition to supporting rich text, `TTTAttributedLabel` can automatically detect links for dates, addresses, URLs, phone numbers, transit information, and allows you to embed your own links.
Expand Down
12 changes: 11 additions & 1 deletion TTTAttributedLabel.podspec
Expand Up @@ -8,8 +8,18 @@ Pod::Spec.new do |s|
s.source = { :git => 'https://github.com/TTTAttributedLabel/TTTAttributedLabel.git', :tag => s.version.to_s }
s.license = 'MIT'
s.frameworks = 'UIKit', 'CoreText', 'CoreGraphics', 'QuartzCore'
s.source_files = 'TTTAttributedLabel'
s.requires_arc = true
s.ios.deployment_target = '4.3'
s.social_media_url = 'https://twitter.com/mattt'

s.subspec 'Default' do |ss|
ss.source_files = 'TTTAttributedLabel'
end

s.subspec 'NoDesignable' do |ss|
ss.dependency 'TTTAttributedLabel/Default'
ss.xcconfig = { 'GCC_PREPROCESSOR_DEFINITIONS' => 'TTT_NO_DESIGNABLE=1' }
end

s.default_subspec = 'Default'
end
45 changes: 30 additions & 15 deletions TTTAttributedLabel/TTTAttributedLabel.h
Expand Up @@ -20,6 +20,21 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

/**
* IB_DESIGNABLE and IBInspectable do not function from within a static library.
* If you're keen on using those annotations, you should either
* - Import TTTAttributedLabel as a dynamic framework with either CocoaPods or Carthage
* - Import TTTAttributedLabel by dragging its source files to your project.
* - Switch to the TTTAttributedLabel/NoDesignable subspec, which disables designable and inspectable annotations.
*/
#ifdef TTT_NO_DESIGNABLE
# define TTTDesignable
# define TTTInspectable
#else
# define TTTDesignable IB_DESIGNABLE
# define TTTInspectable IBInspectable
#endif

#import <UIKit/UIKit.h>
#import <CoreText/CoreText.h>

Expand Down Expand Up @@ -74,10 +89,10 @@ extern NSString * const kTTTBackgroundCornerRadiusAttributeName;

// Override UILabel @property to accept both NSString and NSAttributedString
@protocol TTTAttributedLabel <NSObject>
@property (nonatomic, copy) IBInspectable id text;
@property (nonatomic, copy) TTTInspectable id text;
@end

IB_DESIGNABLE
TTTDesignable

/**
`TTTAttributedLabel` is a drop-in replacement for `UILabel` that supports `NSAttributedString`, as well as automatically-detected and manually-added links to URLs, addresses, phone numbers, and dates.
Expand Down Expand Up @@ -180,25 +195,25 @@ IB_DESIGNABLE
/**
The shadow blur radius for the label. A value of 0 indicates no blur, while larger values produce correspondingly larger blurring. This value must not be negative. The default value is 0.
*/
@property (nonatomic, assign) IBInspectable CGFloat shadowRadius;
@property (nonatomic, assign) TTTInspectable CGFloat shadowRadius;

/**
The shadow blur radius for the label when the label's `highlighted` property is `YES`. A value of 0 indicates no blur, while larger values produce correspondingly larger blurring. This value must not be negative. The default value is 0.
*/
@property (nonatomic, assign) IBInspectable CGFloat highlightedShadowRadius;
@property (nonatomic, assign) TTTInspectable CGFloat highlightedShadowRadius;
/**
The shadow offset for the label when the label's `highlighted` property is `YES`. A size of {0, 0} indicates no offset, with positive values extending down and to the right. The default size is {0, 0}.
*/
@property (nonatomic, assign) IBInspectable CGSize highlightedShadowOffset;
@property (nonatomic, assign) TTTInspectable CGSize highlightedShadowOffset;
/**
The shadow color for the label when the label's `highlighted` property is `YES`. The default value is `nil` (no shadow color).
*/
@property (nonatomic, strong) IBInspectable UIColor *highlightedShadowColor;
@property (nonatomic, strong) TTTInspectable UIColor *highlightedShadowColor;

/**
The amount to kern the next character. Default is standard kerning. If this attribute is set to 0.0, no kerning is done at all.
*/
@property (nonatomic, assign) IBInspectable CGFloat kern;
@property (nonatomic, assign) TTTInspectable CGFloat kern;

///--------------------------------------------
/// @name Acccessing Paragraph Style Attributes
Expand All @@ -207,32 +222,32 @@ IB_DESIGNABLE
/**
The distance, in points, from the leading margin of a frame to the beginning of the paragraph's first line. This value is always nonnegative, and is 0.0 by default.
*/
@property (nonatomic, assign) IBInspectable CGFloat firstLineIndent;
@property (nonatomic, assign) TTTInspectable CGFloat firstLineIndent;

/**
@deprecated Use `lineSpacing` instead.
*/
@property (nonatomic, assign) IBInspectable CGFloat leading DEPRECATED_ATTRIBUTE;
@property (nonatomic, assign) TTTInspectable CGFloat leading DEPRECATED_ATTRIBUTE;

/**
The space in points added between lines within the paragraph. This value is always nonnegative and is 0.0 by default.
*/
@property (nonatomic, assign) IBInspectable CGFloat lineSpacing;
@property (nonatomic, assign) TTTInspectable CGFloat lineSpacing;

/**
The minimum line height within the paragraph. If the value is 0.0, the minimum line height is set to the line height of the `font`. 0.0 by default.
*/
@property (nonatomic, assign) IBInspectable CGFloat minimumLineHeight;
@property (nonatomic, assign) TTTInspectable CGFloat minimumLineHeight;

/**
The maximum line height within the paragraph. If the value is 0.0, the maximum line height is set to the line height of the `font`. 0.0 by default.
*/
@property (nonatomic, assign) IBInspectable CGFloat maximumLineHeight;
@property (nonatomic, assign) TTTInspectable CGFloat maximumLineHeight;

/**
The line height multiple. This value is 1.0 by default.
*/
@property (nonatomic, assign) IBInspectable CGFloat lineHeightMultiple;
@property (nonatomic, assign) TTTInspectable CGFloat lineHeightMultiple;

/**
The distance, in points, from the margin to the text container. This value is `UIEdgeInsetsZero` by default.
Expand All @@ -247,7 +262,7 @@ IB_DESIGNABLE
- `right`: `kCTParagraphStyleSpecifierTailIndent`
*/
@property (nonatomic, assign) IBInspectable UIEdgeInsets textInsets;
@property (nonatomic, assign) TTTInspectable UIEdgeInsets textInsets;

/**
The vertical text alignment for the label, for when the frame size is greater than the text rect size. The vertical alignment is `TTTAttributedLabelVerticalAlignmentCenter` by default.
Expand All @@ -271,7 +286,7 @@ IB_DESIGNABLE
/**
The attributed string to apply to the truncation token at the end of a truncated line. Overrides `truncationTokenStringAttributes` and `truncationTokenString`. If unspecified, attributes will fallback to `truncationTokenStringAttributes` and `truncationTokenString`.
*/
@property (nonatomic, strong) IBInspectable NSAttributedString *attributedTruncationToken;
@property (nonatomic, strong) TTTInspectable NSAttributedString *attributedTruncationToken;

///--------------------------
/// @name Long press gestures
Expand Down

0 comments on commit b3cbe57

Please sign in to comment.