1
votes

I've noticed that, in some fonts, some symbols are displayed incorrectly in UILabel if the frame size for them is determined by NSAttributedString.size or boundingRectWithSize. For example, I chose font AvenirNext-Bold, symbol "A", and set pointSize to 300. Calculated the frame(width = 218.69999999999999, height = 409.80000000000001). I put that character in the frame of the above size.

enter image description here

As you can see in the picture, the parts that cut off of displayed Label are highlighted in the red boxes. I checked several other fonts and noticed that it happens all the time. Can I get rid of this without having to change the frame for the UILabel and correcting margins for symbols in fonts? Using a third-party program, I found out that, for the font AvenirNext-Bold, for the character "A", left and right margins are equal to -1.39.

enter image description here

Does anybody know any techniques to resolve this?

Additional:

enter image description here

If you open Fonts application on your MAC OS, and find Avenir Next Bold font, you can see this cutting off.

Code:

NSString *text = @"A";
NSString *fontName = @"AvenirNext-Bold";

CGFloat pointSize = 300.f;

UIFont *font = [UIFont fontWithName:fontName size:pointSize];

CGSize size = [text boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil].size;

self.label.text = text;
self.label.font = font;

self.label.frame = CGRectMake((self.view.bounds.size.width - size.width) / 2.f, (self.view.bounds.size.height - size.height) / 2.f, size.width, size.height);

Additional font: You can change font to BodoniSvtyTwoITCTT-BookIta.

1
Have you tried 'label.clipToBounds = false'David Williames
David Williames, yes I tried.Mikhail S
Please show actual code needed to reproduce.matt
matt, here the code to reproduce.Mikhail S

1 Answers

1
votes

I think what you've noticed here is essentially the same phenomenon that I note in my book, that these size calculations, such as boundingRectWithSize:options:context: and NSAttributedString's size, ignore margins.

You can probably work around the problem in this case by supplying your own extra margin (though how you can calculate the amount, other than arbitrarily, escapes me):

NSString *text = @"A";
NSString *fontName = @"AvenirNext-Bold";
CGFloat pointSize = 300.f;
UIFont *font = [UIFont fontWithName:fontName size:pointSize];
CGFloat margin = 10;

NSMutableAttributedString* s = [[NSMutableAttributedString alloc] initWithString:text];
[s addAttribute:NSFontAttributeName value:font range:NSMakeRange(0,s.length)];
NSMutableParagraphStyle* p = [NSMutableParagraphStyle new];
p.firstLineHeadIndent = margin;
p.tailIndent = -margin; // in case of right alignment
[s addAttribute:NSParagraphStyleAttributeName value:p range:NSMakeRange(0,s.length)];
CGSize size = [s size];

self.label.attributedText = s;
self.label.frame = CGRectMake((self.view.bounds.size.width - size.width) / 2.f, (self.view.bounds.size.height - size.height) / 2.f, size.width+margin*2, size.height);