【发布时间】:2014-01-03 11:11:10
【问题描述】:
如果我错了,请纠正我。
我尝试使用 Core Text 计算出字符的确切边界矩形。但是我收到的高度总是大于屏幕上绘制字符的实际高度。在这种情况下,实际高度在 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];
}
【问题讨论】:
-
抱歉,您是说这在 viewDidLoad 中有效吗?不工作时它在哪里?
-
抱歉,刚刚意识到这已经很老了......这仍然是一个问题吗?
-
@GeorgeGreen 嗨,乔治,是的,这对我来说仍然是个问题。我已经编辑了问题以使其更清楚。我的意思是代码可以粘贴到viewDidLoad中进行测试。谢谢
-
对于段落样式,我使用 para.boundingRectWithSize(CGSizeMake(fontBoxWidth,10000), options: options, context: nil) 和 CTLines 我使用 CTLineGetTypographicBounds 和 CTFrameGetLineOrigins 来计算边界框
标签: ios uitextview core-text bounding-box nslayoutmanager