Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Update TTTAttributedLabel to 1.8.1

  • Loading branch information...
commit 9aa58968a01074ded737b105ac561df82716b0eb 1 parent 2d94ab0
Rousseau Quentin authored
View
5 TTTAttributedLabel/TTTAttributedLabel.h
@@ -134,6 +134,11 @@ extern NSString * const kTTTBackgroundCornerRadiusAttributeName;
*/
@property (nonatomic, strong) NSDictionary *activeLinkAttributes;
+/**
+ A dictionary containing the `NSAttributedString` attributes to be applied to links when they are in the inactive state, which is triggered a change in `tintColor` in iOS 7. Supply `nil` or an empty dictionary to opt out of inactive link styling. The default inactive link style is gray and unadorned.
+ */
+@property (nonatomic, strong) NSDictionary *inactiveLinkAttributes;
+
///---------------------------------------
/// @name Acccessing Text Style Attributes
///---------------------------------------
View
101 TTTAttributedLabel/TTTAttributedLabel.m
@@ -38,7 +38,7 @@
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 60000
const NSTextAlignment TTTTextAlignmentLeft = NSTextAlignmentLeft;
const NSTextAlignment TTTTextAlignmentCenter = NSTextAlignmentCenter;
-const NSTextAlignment TTTTextAlignmentRigth = NSTextAlignmentRight;
+const NSTextAlignment TTTTextAlignmentRight = NSTextAlignmentRight;
const NSTextAlignment TTTTextAlignmentJustified = NSTextAlignmentJustified;
const NSTextAlignment TTTTextAlignmentNatural = NSTextAlignmentNatural;
@@ -54,7 +54,7 @@
#else
const UITextAlignment TTTTextAlignmentLeft = NSTextAlignmentLeft;
const UITextAlignment TTTTextAlignmentCenter = NSTextAlignmentCenter;
-const UITextAlignment TTTTextAlignmentRigth = NSTextAlignmentRight;
+const UITextAlignment TTTTextAlignmentRight = NSTextAlignmentRight;
const UITextAlignment TTTTextAlignmentJustified = NSTextAlignmentJustified;
const UITextAlignment TTTTextAlignmentNatural = NSTextAlignmentNatural;
@@ -162,7 +162,6 @@ static inline CGFLOAT_TYPE CGFloat_round(CGFLOAT_TYPE cgfloat) {
#endif
}
-
static inline NSDictionary * NSAttributedStringAttributesFromLabel(TTTAttributedLabel *label) {
NSMutableDictionary *mutableAttributes = [NSMutableDictionary dictionary];
@@ -174,10 +173,10 @@ static inline CGFLOAT_TYPE CGFloat_round(CGFLOAT_TYPE cgfloat) {
paragraphStyle.alignment = label.textAlignment;
paragraphStyle.lineSpacing = label.leading;
paragraphStyle.lineHeightMultiple = label.lineHeightMultiple;
- paragraphStyle.firstLineHeadIndent = label.firstLineIndent;
paragraphStyle.paragraphSpacingBefore = label.textInsets.top;
paragraphStyle.paragraphSpacing = label.textInsets.bottom;
- paragraphStyle.headIndent = label.textInsets.left;
+ paragraphStyle.firstLineHeadIndent = label.firstLineIndent + label.textInsets.left;
+ paragraphStyle.headIndent = paragraphStyle.firstLineHeadIndent;
paragraphStyle.tailIndent = -label.textInsets.right;
if (label.numberOfLines == 1) {
@@ -215,9 +214,9 @@ static inline CGFLOAT_TYPE CGFloat_round(CGFLOAT_TYPE cgfloat) {
{.spec = kCTParagraphStyleSpecifierLineSpacing, .valueSize = sizeof(CGFloat), .value = (const void *)&lineSpacing},
{.spec = kCTParagraphStyleSpecifierLineSpacingAdjustment, .valueSize = sizeof (CGFloat), .value = (const void *)&lineSpacingAdjustment},
{.spec = kCTParagraphStyleSpecifierLineHeightMultiple, .valueSize = sizeof(CGFloat), .value = (const void *)&lineHeightMultiple},
- {.spec = kCTParagraphStyleSpecifierFirstLineHeadIndent, .valueSize = sizeof(CGFloat), .value = (const void *)&firstLineIndent},
{.spec = kCTParagraphStyleSpecifierParagraphSpacingBefore, .valueSize = sizeof(CGFloat), .value = (const void *)&topMargin},
{.spec = kCTParagraphStyleSpecifierParagraphSpacing, .valueSize = sizeof(CGFloat), .value = (const void *)&bottomMargin},
+ {.spec = kCTParagraphStyleSpecifierFirstLineHeadIndent, .valueSize = sizeof(CGFloat), .value = (const void *)&firstLineIndent},
{.spec = kCTParagraphStyleSpecifierHeadIndent, .valueSize = sizeof(CGFloat), .value = (const void *)&leftMargin},
{.spec = kCTParagraphStyleSpecifierTailIndent, .valueSize = sizeof(CGFloat), .value = (const void *)&rightMargin}
};
@@ -349,18 +348,24 @@ - (void)commonInit {
NSMutableDictionary *mutableActiveLinkAttributes = [NSMutableDictionary dictionary];
[mutableActiveLinkAttributes setObject:[NSNumber numberWithBool:NO] forKey:(NSString *)kCTUnderlineStyleAttributeName];
+ NSMutableDictionary *mutableInactiveLinkAttributes = [NSMutableDictionary dictionary];
+ [mutableInactiveLinkAttributes setObject:[NSNumber numberWithBool:NO] forKey:(NSString *)kCTUnderlineStyleAttributeName];
+
if ([NSMutableParagraphStyle class]) {
[mutableLinkAttributes setObject:[UIColor blueColor] forKey:(NSString *)kCTForegroundColorAttributeName];
[mutableActiveLinkAttributes setObject:[UIColor redColor] forKey:(NSString *)kCTForegroundColorAttributeName];
+ [mutableInactiveLinkAttributes setObject:[UIColor grayColor] forKey:(NSString *)kCTForegroundColorAttributeName];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
[mutableLinkAttributes setObject:paragraphStyle forKey:(NSString *)kCTParagraphStyleAttributeName];
[mutableActiveLinkAttributes setObject:paragraphStyle forKey:(NSString *)kCTParagraphStyleAttributeName];
+ [mutableInactiveLinkAttributes setObject:paragraphStyle forKey:(NSString *)kCTParagraphStyleAttributeName];
} else {
[mutableLinkAttributes setObject:(__bridge id)[[UIColor blueColor] CGColor] forKey:(NSString *)kCTForegroundColorAttributeName];
[mutableActiveLinkAttributes setObject:(__bridge id)[[UIColor redColor] CGColor] forKey:(NSString *)kCTForegroundColorAttributeName];
+ [mutableInactiveLinkAttributes setObject:(__bridge id)[[UIColor grayColor] CGColor] forKey:(NSString *)kCTForegroundColorAttributeName];
CTLineBreakMode lineBreakMode = kCTLineBreakByWordWrapping;
CTParagraphStyleSetting paragraphStyles[1] = {
@@ -370,12 +375,14 @@ - (void)commonInit {
[mutableLinkAttributes setObject:(__bridge id)paragraphStyle forKey:(NSString *)kCTParagraphStyleAttributeName];
[mutableActiveLinkAttributes setObject:(__bridge id)paragraphStyle forKey:(NSString *)kCTParagraphStyleAttributeName];
-
+ [mutableInactiveLinkAttributes setObject:(__bridge id)paragraphStyle forKey:(NSString *)kCTParagraphStyleAttributeName];
+
CFRelease(paragraphStyle);
}
self.linkAttributes = [NSDictionary dictionaryWithDictionary:mutableLinkAttributes];
self.activeLinkAttributes = [NSDictionary dictionaryWithDictionary:mutableActiveLinkAttributes];
+ self.inactiveLinkAttributes = [NSDictionary dictionaryWithDictionary:mutableInactiveLinkAttributes];
}
- (void)dealloc {
@@ -444,33 +451,32 @@ - (CTFramesetterRef)framesetter {
return _framesetter;
}
-- (void)setFramesetter:(CTFramesetterRef)framesetter
-{
- CTFramesetterRef oldFramesetter = _framesetter;
- _framesetter = framesetter;
- if (_framesetter) {
- CFRetain(_framesetter);
+- (void)setFramesetter:(CTFramesetterRef)framesetter {
+ if (framesetter) {
+ CFRetain(framesetter);
}
- if (oldFramesetter) {
- CFRelease(oldFramesetter);
+
+ if (_framesetter) {
+ CFRelease(_framesetter);
}
+
+ _framesetter = framesetter;
}
-- (CTFramesetterRef)highlightFramesetter
-{
+- (CTFramesetterRef)highlightFramesetter {
return _highlightFramesetter;
}
-- (void)setHighlightFramesetter:(CTFramesetterRef)highlightFramesetter
-{
- CTFramesetterRef oldHighlightFramesetter = _highlightFramesetter;
- _highlightFramesetter = highlightFramesetter;
- if (_highlightFramesetter) {
- CFRetain(_highlightFramesetter);
+- (void)setHighlightFramesetter:(CTFramesetterRef)highlightFramesetter {
+ if (highlightFramesetter) {
+ CFRetain(highlightFramesetter);
}
- if (oldHighlightFramesetter) {
- CFRelease(oldHighlightFramesetter);
+
+ if (_highlightFramesetter) {
+ CFRelease(_highlightFramesetter);
}
+
+ _highlightFramesetter = highlightFramesetter;
}
- (NSAttributedString *)renderedAttributedText {
@@ -516,6 +522,7 @@ - (void)addLinksWithTextCheckingResults:(NSArray *)results
for (NSTextCheckingResult *result in results) {
[mutableAttributedString addAttributes:attributes range:result.range];
}
+
self.attributedText = mutableAttributedString;
[self setNeedsDisplay];
}
@@ -748,7 +755,7 @@ - (void)drawFramesetter:(CTFramesetterRef)framesetter
case TTTTextAlignmentCenter:
flushFactor = 0.5f;
break;
- case TTTTextAlignmentRigth:
+ case TTTTextAlignmentRight:
flushFactor = 1.0f;
break;
case TTTTextAlignmentLeft:
@@ -1008,9 +1015,11 @@ - (void)setActiveLink:(NSTextCheckingResult *)activeLink {
}
NSMutableAttributedString *mutableAttributedString = [self.inactiveAttributedText mutableCopy];
- [mutableAttributedString addAttributes:self.activeLinkAttributes range:_activeLink.range];
- self.attributedText = mutableAttributedString;
+ if (NSLocationInRange(NSMaxRange(self.activeLink.range), NSMakeRange(0, [self.inactiveAttributedText length]))) {
+ [mutableAttributedString addAttributes:self.activeLinkAttributes range:self.activeLink.range];
+ }
+ self.attributedText = mutableAttributedString;
[self setNeedsDisplay];
} else if (self.inactiveAttributedText) {
self.attributedText = self.inactiveAttributedText;
@@ -1171,6 +1180,27 @@ - (CGSize)intrinsicContentSize {
return [self sizeThatFits:[super intrinsicContentSize]];
}
+- (void)tintColorDidChange {
+ BOOL isInactive = (CGColorSpaceGetModel(CGColorGetColorSpace([self.tintColor CGColor])) == kCGColorSpaceModelMonochrome);
+
+ NSDictionary *attributesToRemove = isInactive ? self.linkAttributes : self.inactiveLinkAttributes;
+ NSDictionary *attributesToAdd = isInactive ? self.inactiveLinkAttributes : self.linkAttributes;
+
+ NSMutableAttributedString *mutableAttributedString = [self.attributedText mutableCopy];
+ for (NSTextCheckingResult *result in self.links) {
+ [attributesToRemove enumerateKeysAndObjectsUsingBlock:^(NSString *name, __unused id value, __unused BOOL *stop) {
+ [mutableAttributedString removeAttribute:name range:result.range];
+ }];
+
+ if (attributesToAdd) {
+ [mutableAttributedString addAttributes:attributesToAdd range:result.range];
+ }
+ }
+
+ self.attributedText = mutableAttributedString;
+ [self setNeedsDisplay];
+}
+
#pragma mark - UIResponder
- (BOOL)canBecomeFirstResponder {
@@ -1288,6 +1318,7 @@ - (void)encodeWithCoder:(NSCoder *)coder {
if ([NSMutableParagraphStyle class]) {
[coder encodeObject:self.linkAttributes forKey:NSStringFromSelector(@selector(linkAttributes))];
[coder encodeObject:self.activeLinkAttributes forKey:NSStringFromSelector(@selector(activeLinkAttributes))];
+ [coder encodeObject:self.inactiveLinkAttributes forKey:NSStringFromSelector(@selector(inactiveLinkAttributes))];
}
[coder encodeObject:@(self.shadowRadius) forKey:NSStringFromSelector(@selector(shadowRadius))];
[coder encodeObject:@(self.highlightedShadowRadius) forKey:NSStringFromSelector(@selector(highlightedShadowRadius))];
@@ -1326,14 +1357,18 @@ - (id)initWithCoder:(NSCoder *)coder {
if ([coder containsValueForKey:NSStringFromSelector(@selector(activeLinkAttributes))]) {
self.activeLinkAttributes = [coder decodeObjectForKey:NSStringFromSelector(@selector(activeLinkAttributes))];
}
+
+ if ([coder containsValueForKey:NSStringFromSelector(@selector(inactiveLinkAttributes))]) {
+ self.inactiveLinkAttributes = [coder decodeObjectForKey:NSStringFromSelector(@selector(inactiveLinkAttributes))];
+ }
}
if ([coder containsValueForKey:NSStringFromSelector(@selector(shadowRadius))]) {
- self.shadowRadius = [coder decodeFloatForKey:NSStringFromSelector(@selector(shadowRadius))];
+ self.shadowRadius = [[coder decodeObjectForKey:NSStringFromSelector(@selector(shadowRadius))] floatValue];
}
if ([coder containsValueForKey:NSStringFromSelector(@selector(highlightedShadowRadius))]) {
- self.highlightedShadowRadius = [coder decodeFloatForKey:NSStringFromSelector(@selector(highlightedShadowRadius))];
+ self.highlightedShadowRadius = [[coder decodeObjectForKey:NSStringFromSelector(@selector(highlightedShadowRadius))] floatValue];
}
if ([coder containsValueForKey:NSStringFromSelector(@selector(highlightedShadowOffset))]) {
@@ -1345,15 +1380,15 @@ - (id)initWithCoder:(NSCoder *)coder {
}
if ([coder containsValueForKey:NSStringFromSelector(@selector(firstLineIndent))]) {
- self.firstLineIndent = [coder decodeFloatForKey:NSStringFromSelector(@selector(firstLineIndent))];
+ self.firstLineIndent = [[coder decodeObjectForKey:NSStringFromSelector(@selector(firstLineIndent))] floatValue];
}
if ([coder containsValueForKey:NSStringFromSelector(@selector(leading))]) {
- self.leading = [coder decodeFloatForKey:NSStringFromSelector(@selector(leading))];
+ self.leading = [[coder decodeObjectForKey:NSStringFromSelector(@selector(leading))] floatValue];
}
if ([coder containsValueForKey:NSStringFromSelector(@selector(lineHeightMultiple))]) {
- self.lineHeightMultiple = [coder decodeFloatForKey:NSStringFromSelector(@selector(lineHeightMultiple))];
+ self.lineHeightMultiple = [[coder decodeObjectForKey:NSStringFromSelector(@selector(lineHeightMultiple))] floatValue];
}
if ([coder containsValueForKey:NSStringFromSelector(@selector(textInsets))]) {
Please sign in to comment.
Something went wrong with that request. Please try again.