Please correct me if I'm wrong.
I tried to work out the exact bounding rectangle of a character using Core Text. But the height I received was always bigger than the actual height of the drawn character on the screen. In this case, the actual height is around 20 but the function just give me 46 no matter what.
Could anyone shed some light on this?
Thanks.
Here is the code
- (void)viewDidLoad{
[super viewDidLoad];
NSString *testString = @"A";
NSAttributedString *textString = [[NSAttributedString alloc] initWithString:testString attributes:@{
NSFontAttributeName: [UIFont fontWithName:@"Helvetica" size:40]
}];
NSTextStorage *textStorage = [[NSTextStorage alloc] initWithAttributedString:textString];
NSLayoutManager *textLayout = [[NSLayoutManager alloc] init];
// Add layout manager to text storage object
[textStorage addLayoutManager:textLayout];
// Create a text container
NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:self.view.bounds.size];
// Add text container to text layout manager
[textLayout addTextContainer:textContainer];
NSRange range = NSMakeRange (0, testString.length);
CGRect boundingBox = [textLayout boundingRectForGlyphRange:range inTextContainer:textContainer];
//BoundingBox:{{5, 0}, {26.679688, 46}}
// Instantiate UITextView object using the text container
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(20,20,self.view.bounds.size.width-20,self.view.bounds.size.height-20)
textContainer:textContainer];
// Add text view to the main view of the view controler
[self.view addSubview:textView];
}
I'm currently working on this for Core Text rendering, and surprised that this type of information isn't supplied directly (for related graphics like fitted backgrounds/outlines)
These are both works in progress from other stackoverflow questions and my own testing to get perfect bounding boxes (tight)
Common font properties
For paragraph styles
For CTFrames