• 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
Re: NSAttributedString mysteriously truncated too soon
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSAttributedString mysteriously truncated too soon


  • Subject: Re: NSAttributedString mysteriously truncated too soon
  • From: Sixten Otto <email@hidden>
  • Date: Thu, 08 Nov 2012 16:42:08 -0700

Out of curiosity, does it matter if you change this line:
  [content addAttribute:NSParagraphStyleAttributeName value:para
range:NSMakeRange(title.length,1)];

to this?
  [content addAttribute:NSParagraphStyleAttributeName value:para
range:NSMakeRange(title.length,blurb.length)];

I don't know that it would, it just stuck out at me as a little odd to only
add the paragraph style to one character.


On Thu, Nov 8, 2012 at 2:23 PM, Matt Neuburg <email@hidden> wrote:

> 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
_______________________________________________

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: Kyle Sluder <email@hidden>
References: 
 >NSAttributedString mysteriously truncated too soon (From: Matt Neuburg <email@hidden>)

  • Prev by Date: NSAttributedString mysteriously truncated too soon
  • Next by Date: Re: NSAttributedString mysteriously truncated too soon
  • Previous by thread: NSAttributedString mysteriously truncated too soon
  • Next by thread: Re: NSAttributedString mysteriously truncated too soon
  • Index(es):
    • Date
    • Thread