4

How can I replace the truncation ellipsis ("…") of a UILabel in iOS 7 with another attributed character? For example, with a colored ">".

I was hoping Text Kit's NSLayoutManager would make this possible, but it appears UILabel doesn't make it public if it uses it.

Also, can I safely assume that an ellipsis is used as the truncation character in every localisation? Maybe different languages have different truncation characters.

hpique
  • 119,096
  • 131
  • 338
  • 476
  • The open source TTTAttributedLabel project (a UILabel subclass) has this functionality built in. – Aaron Brager Jan 24 '14 at 13:46
  • Nice! I didn't know that. @mattt does think of everything! – hpique Jan 24 '14 at 14:04
  • possible duplicate of [How to change truncate characters in UILabel?](http://stackoverflow.com/questions/4793420/how-to-change-truncate-characters-in-uilabel) – Bhavin Jan 30 '14 at 12:27

4 Answers4

2

I recommend you use TTTAttributedLabel, just set property "attributedTruncationToken" to your custom string.

moligaloo
  • 1,083
  • 13
  • 27
0

I don't think it gives you access to this. I think you would have do handle it manually. For example, use TextKit to determine the size of your string, if it doesn't fit in the available area, truncate it yourself and append a ">" and then put your new string in the label.

NSAttributedString has methods for getting the size of the string.

Let me know if you need any more detail on this..?

George Green
  • 4,807
  • 5
  • 31
  • 45
  • Thanks. I have an inkling on how to do it manually, but more detail is always appreciated. Specially if you use Text Kit, as most other code examples around are not updated to the latests iOS 7 APIs. – hpique Jan 24 '14 at 12:59
  • To be honest, I'll try to knock up an example this afternoon for you... Are you opposed to using CoreText? – George Green Jan 24 '14 at 14:56
  • No rush. I would prefer Text Kit if the same functionality is available at a higher level of abstraction. – hpique Jan 24 '14 at 15:10
0

I think you can do some customization in -replaceElipsesForLabel method provided by Fonix to get your desired result.

Community
  • 1
  • 1
Bhavin
  • 27,155
  • 11
  • 55
  • 94
  • Thanks. I hadn't seen that question. Kinda makes this a duplicate, but I wanted to focus on iOS 7. Feel free to vote close if you think it's not worth having the two around. Regarding your suggested solution, I was hoping to avoid replicating the truncation logic, as there are quite a few different cases to consider (and that Fonix's answer doesn't.) – hpique Jan 24 '14 at 13:05
-1

I have written a method to do it, and works in iOS7

-(void)setCustomEllipsis:(NSString*)customEllipsis inLabel:(UILabel*)label with:(NSString*)string{

//Replace the ellipsis
NSMutableString* result = [[NSMutableString alloc] initWithString:@""];
NSArray* strings = [string componentsSeparatedByString:@" "];
for (NSString* s in strings) {
    CGRect newSize = [[NSString stringWithFormat:@"%@%@%@",result,s,customEllipsis] boundingRectWithSize:CGSizeMake(label.frame.size.width,0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:label.font} context:nil];
    if (newSize.size.height < label.frame.size.height) {
        [result appendString:s];
        [result appendString:@" "];
    }else{
        [result appendString:customEllipsis];
        break;
    }
}
[label setText:result];

//Set different font to the ellipsis
const CGFloat fontSize = 13;
UIFont *boldFont = [UIFont boldSystemFontOfSize:fontSize];
UIFont *regularFont = [UIFont systemFontOfSize:fontSize];
UIColor *foregroundColor = [UIColor lightGrayColor];

NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:regularFont, NSFontAttributeName,foregroundColor, NSForegroundColorAttributeName, nil];
NSDictionary *subAttrs = [NSDictionary dictionaryWithObjectsAndKeys:boldFont, NSFontAttributeName, nil];
const NSRange range = [label.text rangeOfString:customEllipsis];

NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:result
                                           attributes:attrs];
[attributedText setAttributes:subAttrs range:range];


[label setAttributedText:attributedText];
}
Erick Silva
  • 499
  • 5
  • 9