Пожалуйста, поправьте меня, если я ошибаюсь.
Я попытался определить точный ограничивающий прямоугольник символа, используя основной текст. Но высота, которую я получал, всегда была больше реальной высоты нарисованного персонажа на экране. В этом случае фактическая высота составляет около 20, но функция просто дает мне 46, несмотря ни на что.
Может ли кто-нибудь пролить свет на это?
Спасибо.
Вот код
- (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];
}