对于给定的NSRange,我想找到一个CGRect在UILabel对应的的字形NSRange.例如,我想CGRect在"快速的棕色狐狸跳过懒狗"这句话中找到包含"狗"字样的字样.

诀窍是,UILabel有多行,而文本确实attributedText如此,所以找到字符串的确切位置有点困难.
我想在我的UILabel子类上写的方法看起来像这样:
- (CGRect)rectForSubstringWithRange:(NSRange)range;
Run Code Online (Sandbox Code Playgroud)
细节,对于有兴趣的人:
我的目标是能够创建一个具有UILabel的确切外观和位置的新UILabel,然后我可以制作动画.我已经把剩下的事情搞清楚了,但特别是这一步让我暂时退缩了.
到目前为止我尝试解决问题的方法是:
UITextView和UITextField而不是UILabel.我敢打赌,对此的正确答案涉及以下其中一项:
NSLayoutManager和textContainerForGlyphAtIndex:effectiveRange更新:这是一个github要点,我已经尝试了迄今为止解决这个问题的三件事:https://gist.github.com/bryanjclark/7036101
我在 iOS 14 上遇到笔触颜色问题。
let attributes: [NSAttributedString.Key: Any] = [
.foregroundColor: UIColor.white,
.strokeColor: UIColor.black,
.font: UIFont.systemFont(ofSize: 65, weight: .black),
.strokeWidth: -1
]
lableNumber.attributedText = NSAttributedString(string: "2", attributes: attributes)
Run Code Online (Sandbox Code Playgroud)
iOS 13
iOS 14
它适用于 iOS 13,但不适用于 iOS 14。谁能帮我解决这个问题?