Skip to content
This repository has been archived by the owner on May 24, 2024. It is now read-only.

Commit

Permalink
Introduce an attributesForTailTruncationString property.
Browse files Browse the repository at this point in the history
This property will allow callers to add custom text attributes to the `tailTruncationString` property in `NIAttributedLabel`.

PiperOrigin-RevId: 413440335
  • Loading branch information
Nobody authored and material-automation committed Dec 1, 2021
1 parent 182731f commit be91576
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/attributedlabel/src/NIAttributedLabel.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ extern NSString* const NIAttributedLabelLinkAttributeName; // Value is an NSText
@property (nonatomic, strong) UIColor* highlightedLinkBackgroundColor; // Default: [UIColor colorWithWhite:0.5 alpha:0.5
@property (nonatomic) BOOL linksHaveUnderlines; // Default: NO
@property (nonatomic, copy) NSDictionary* attributesForLinks; // Default: nil
@property (nonatomic, copy) NSDictionary* attributesForTailTruncationString; // Default: nil
@property (nonatomic, copy) NSDictionary* attributesForHighlightedLink; // Default: nil
@property (nonatomic) CGFloat lineHeight;

Expand Down
21 changes: 17 additions & 4 deletions src/attributedlabel/src/NIAttributedLabel.m
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,14 @@ - (void)setLinksHaveUnderlines:(BOOL)linksHaveUnderlines {
}
}

- (void)setAttributesForTailTruncationString:(NSDictionary *)attributesForTailTruncationString {
if (_attributesForTailTruncationString != attributesForTailTruncationString) {
_attributesForTailTruncationString = attributesForTailTruncationString;

[self attributedTextDidChange];
}
}

- (void)setAttributesForLinks:(NSDictionary *)attributesForLinks {
if (_attributesForLinks != attributesForLinks) {
_attributesForLinks = attributesForLinks;
Expand Down Expand Up @@ -1419,7 +1427,7 @@ - (NSMutableAttributedString *)mutableAttributedStringWithAdditions {
NSInteger index = MAX((NSInteger)0, MIN((NSInteger)(attributedString.length - 1), labelImage.index));
attributes = [attributedString attributesAtIndex:index effectiveRange:NULL];
}

UIFont *font = attributes[NSFontAttributeName];
if (font) {
labelImage.fontAscent = font.ascender;
Expand All @@ -1438,7 +1446,7 @@ - (NSMutableAttributedString *)mutableAttributedStringWithAdditions {
if (font) {
[space addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, space.length)];
}

CFRange range = CFRangeMake(0, 1);
CFMutableAttributedStringRef spaceString = (__bridge_retained CFMutableAttributedStringRef)space;
CFAttributedStringSetAttribute(spaceString, range, kCTRunDelegateAttributeName, delegate);
Expand Down Expand Up @@ -1622,12 +1630,17 @@ - (void)drawAttributedString:(NSAttributedString *)attributedString rect:(CGRect

NSAttributedString* tokenAttributedString;
{
NSMutableDictionary *mutableTokenAttributes = [NSMutableDictionary new];
NSDictionary *tokenAttributes = [attributedString attributesAtIndex:truncationAttributePosition
effectiveRange:NULL];
[mutableTokenAttributes addEntriesFromDictionary:tokenAttributes];
[mutableTokenAttributes addEntriesFromDictionary:_attributesForTailTruncationString];
NSString* tokenString = ((nil == self.tailTruncationString)
? kEllipsesCharacter
: self.tailTruncationString);
tokenAttributedString = [[NSAttributedString alloc] initWithString:tokenString attributes:tokenAttributes];
tokenAttributedString =
[[NSAttributedString alloc] initWithString:tokenString
attributes:mutableTokenAttributes];
}

CTLineRef truncationToken = CTLineCreateWithAttributedString((__bridge CFAttributedStringRef)tokenAttributedString);
Expand Down Expand Up @@ -1728,7 +1741,7 @@ - (void)drawStrikethroughOverGlyphRun:(CTRunRef)run
UIFont *font = CFDictionaryGetValue(attributes, kFontAttributeKey);
font = font ?: self.font;
CGFloat strikeHeight = font.xHeight / 2.f + (*firstGlyphPosition).y;

// Adjustment for multiline elements.
CGPoint pt = CGContextGetTextPosition(ctx);
strikeHeight += pt.y;
Expand Down

0 comments on commit be91576

Please sign in to comment.