在UILabel中的draw#rect中获取字形boundingRect [英] Getting a glyph boundingRect in draw#rect in UILabel

查看:171
本文介绍了在UILabel中的draw#rect中获取字形boundingRect的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Swift,我想在UILabel中的draw#rect中获得字形的boundingRect。

Using Swift, I want to get the boundingRect of a glyph, in draw#rect in a UILabel.

UILabel已经具有尺寸(例如,

The UILabel already has a size (say 300x300 in the example) and qualities such as the text being centered.

class RNDLabel: UILabel {

    override func draw(_ rect: CGRect) {

        let manager = NSLayoutManager()

        let store = NSTextStorage(attributedString: NSAttributedString(
            string: text!,
            attributes: [NSAttributedString.Key.font: font]))
        store.addLayoutManager(manager)

        let textContainer = NSTextContainer(size: rect.size)
        // note, intrinsicContentSize is identical there, no difference
        manager.addTextContainer(textContainer)

        let glyphRange = manager.glyphRange(
           forCharacterRange: NSRange(location: 0, length: 1),
           actualCharacterRange: nil)
        let glyphRect = manager.boundingRect(
           forGlyphRange: glyphRange, in: textContainer)

        print("glyphRect \(glyphRect)")         
        ...context?.addRect(glyphRect), context?.drawPath(using: .stroke)

        super.draw(rect)
    }

绿色方块不正确-它应该更像这些红色方块!

The green square is not correct - it should be more like these red squares!

似乎存在许多问题:


  • 确定我制作的layoutManager是否应该具有UILabel的质量,例如居中文本? (不过,我相信您实际上不能直接访问UILabel的layoutManager?)

  • surely the layoutManager I make, should get the qualities of the UILabel, eg "centered text"? (I believe you can't actually directly access the layoutManager of a UILabel, though?)

我们应该使用类似CTLineGetOffsetForStringIndex这样的东西吗?即使在draw#rect

should we be using something like CTLineGetOffsetForStringIndex? Is that even possible in draw#rect

通知中甚至没有正确的偏移量也是可能的,无论如何,绿色框似乎都是错误的高度。 (它看起来更像是一个普通的旧的internalContentSize而不是一个字形边界框。)

notice as well as not having the correct offset, the green box seems the wrong height anyway. (It looks more like a plain old intrinsicContentSize rather than a glyph bounding box.)

操作方法?

出于记录目的,我的总体目标是根据实际字形框(当然, y, X等有所不同)来移动字形。但总的来说,有很多有用的原因来了解字形的框。

For the record, my overall aim is to move the glyph around, based on the actual glyph box (which of course is different for "y", "X", etc). But in general there are many useful reasons to know the box of a glyph.

推荐答案

事实证明,此问题的答案似乎成为:

It turns out the answer to this question seems to be:

尤其是RobN。指出调查表明,在UILabel中,_drawTextInRect:baselineCalculationOnly可以完成工作,并且这是一堆临时代码。

In particular RobN. has pointed out that an investigation showed that in UILabel, _drawTextInRect:baselineCalculationOnly does the work and that is a big pile of ad hoc code.

情况的摘要似乎是UILabel只是早于NSLayoutManager / Core Text,并且(到目前为止)仅使用普通版本就不会使用这些现代系统。

A summary of the situation would seem to be that UILabel simply predates NSLayoutManager / Core Text and (as yet) just plain does not use those modern systems.

只有那些现代系统才能为您提供此功能...

Only those modern systems give you this ...

...可以逐字形地访问字形。

... sort of access to glyph-by-glyph shapes.

这篇关于在UILabel中的draw#rect中获取字形boundingRect的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆