• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
NSAttributedString mysteriously truncated too soon
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

NSAttributedString mysteriously truncated too soon


  • Subject: NSAttributedString mysteriously truncated too soon
  • From: Matt Neuburg <email@hidden>
  • Date: Thu, 08 Nov 2012 13:23:23 -0800

I have a tall UILabel with numberOfLines 0 (meaning infinite). The problem remains the same even if numberOfLines is some large finite number such as 20, so that's not the source of the problem.

I am creating an attributed string like this:

    NSString *s1 = @"The Gettysburg Address, as given by A. Lincoln on a certain occasion\n";
    NSString *s2 = @"Four score and seven years ago, our fathers brought forth upon this continent a new nation, conceived in liberty, and dedicated to the proposition that all men are created equal.";

    NSString* title = s1;
    NSMutableAttributedString* content = [[NSMutableAttributedString alloc]
                                          initWithString:title
                                          attributes:@{NSFontAttributeName:[UIFont fontWithName:@"Arial-BoldMT" size:15],
                                          NSForegroundColorAttributeName:[UIColor colorWithRed:0.251 green:0.000 blue:0.502 alpha:1],
                                          NSKernAttributeName:[NSNull null]}];

    NSString* blurb = s2;
    NSMutableAttributedString* content2 = [[NSMutableAttributedString alloc]
                                           initWithString:blurb
                                           attributes:@{NSFontAttributeName:[UIFont fontWithName:@"Georgia" size:14],
                                           NSKernAttributeName:[NSNull null]}];
    [content appendAttributedString:content2];

    // additional code will go here

    self.lab.attributedText = content;

It works fine; I see the title and the paragraph. Now, you see where it says "additional code will go here"? In that spot, I add paragraph styles to my attributed string, like this:

    NSMutableParagraphStyle* para = [NSMutableParagraphStyle new];
    para.headIndent = 10;
    para.firstLineHeadIndent = 10;
    para.paragraphSpacingBefore = 5;
    para.tailIndent = -1;
    para.lineBreakMode = NSLineBreakByWordWrapping;
    [content addAttribute:NSParagraphStyleAttributeName value:para range:NSMakeRange(0,title.length)];

    para = [NSMutableParagraphStyle new];
    para.headIndent = 10;
    para.firstLineHeadIndent = 10;
    para.tailIndent = -1;
    para.lineBreakMode = NSLineBreakByTruncatingTail;
    para.paragraphSpacing = 5;
    [content addAttribute:NSParagraphStyleAttributeName value:para range:NSMakeRange(title.length,1)];

    NSStringDrawingContext* con = [NSStringDrawingContext new];
    CGRect r = [content boundingRectWithSize:CGSizeMake(280,10000) options:NSStringDrawingUsesLineFragmentOrigin context:con];
    NSLog(@"%f", r.size.height);

The result (and this is the problem) is that the label truncates after the **first line** of the second paragraph ("Four score and seven years ago, our fa…"). Why? There's plenty of room in my label for more lines!

Moreover, I am trying to predict the height that my text will occupy. Right now I'm just logging the result, as shown above. Without those paragraph styles, I get 121, which looks right. With the paragraph styles, I get 53, which suggests that the truncation is taking place here too!

Now, I know that I can fix the problem by changing NSLineBreakByTruncatingTail to NSLineBreakByWordWrapping for the second paragraph. But I don't want to! Because if in fact the text is too long for the height of the actual label, I do want ellipses at the end!

So how can I get tail truncation **when the label is too short**, without getting **unnecessary** truncation after the first line of the second paragraph?

Thx - m.

--
matt neuburg, phd = email@hidden, http://www.apeth.net/matt/
pantes anthropoi tou eidenai oregontai phusei
Programming iOS 5! http://shop.oreilly.com/product/0636920023562.do
RubyFrontier! http://www.apeth.com/RubyFrontierDocs/default.html
TidBITS, Mac news and reviews since 1990, http://www.tidbits.com


_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden


  • Follow-Ups:
    • Re: NSAttributedString mysteriously truncated too soon
      • From: Quincey Morris <email@hidden>
    • Re: NSAttributedString mysteriously truncated too soon
      • From: Sixten Otto <email@hidden>
  • Prev by Date: Re: OS_OBJECT_USE_OBJC_RETAIN_RELEASE and xpc_release()
  • Next by Date: Re: NSAttributedString mysteriously truncated too soon
  • Previous by thread: Re: [SOLVED] Re: What does this mean?
  • Next by thread: Re: NSAttributedString mysteriously truncated too soon
  • Index(es):
    • Date
    • Thread