gpt4 book ai didi

ios - 使用核心文字的矩形包围

转载 作者:行者123 更新时间:2023-12-01 16:45:10 25 4
gpt4 key购买 nike

如果我错了,请纠正我。

我尝试使用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];
}

最佳答案

我目前正在为Core Text渲染进行此工作,但感到惊讶的是没有直接提供此类信息(适用于相关图形,例如适合的背景/轮廓)

这些都是从其他stackoverflow问题以及我自己进行的测试中取得的成果,它们用于获得完美的边界框(紧密)

常用字体属性

    let leading = floor( CTFontGetLeading(fontCT) + 0.5)
let ascent = floor( CTFontGetAscent(fontCT) + 0.5)
let descent = floor( CTFontGetDescent(fontCT) + 0.5)
var lineHeight = ascent + descent + leading
var ascenderDelta = CGFloat(0)
if leading > 0 {
ascenderDelta = 0
}
else {
ascenderDelta = floor( 0.2 * lineHeight + 0.5 )
}
lineHeight = lineHeight + ascenderDelta

用于段落样式
    var para = NSMutableAttributedString()
// append attributed strings and set NSMutableParagraphStyle
/* ... */
let options : NSStringDrawingOptions = .UsesFontLeading | .UsesLineFragmentOrigin | .UsesDeviceMetrics
let rect = para.boundingRectWithSize(CGSizeMake(fontBoxWidth,10000), options: options, context: nil)
var backgroundBounds = CGRectMake(boundingBox.origin.x + point.x, boundingBox.origin.y + point.y + lineHeight, boundingBox.width, boundingBox.height + ascenderDelta)

对于CTFrames
    let lines = CTFrameGetLines(frame) as NSArray
let numLines = CFArrayGetCount(lines)

for var index = 0; index < numLines; index++ {
var ascent = CGFloat(0),
descent = CGFloat(0),
leading = CGFloat(0),
width = CGFloat(0)
let line = lines[index] as! CTLine
width = CGFloat(CTLineGetTypographicBounds(line, &ascent, &descent, &leading))
// adjust with common font property code
var lineOrigin : CGPoint = CGPointMake(0,0)
CTFrameGetLineOrigins(frame, CFRangeMake(index, 1), &lineOrigin)
let bounds = CGRectMake(point.x + lineOrigin.x, point.y + lineOrigin.y - descent, width, ascent + descent)

关于ios - 使用核心文字的矩形包围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20604522/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com