Core Text使用Swift在iOS中计算字母和红宝石注释框架

问题描述

如何在iOS中由CoreText和CoreGraphics呈现的CTFrame文本中,为每个红宝石注释字符(或假名)提供边界框? (iOS 11、12或13)

enter image description here

基于这个问题Core Text calculate letter frame in iOS,我制作了一个快速版本的程序,将每个字符都包围在一个红色框中,但我无法获得红宝石注释字符的边框 aka furigana 。出于这个问题的目的,我将红宝石字符框涂成绿色。

我该如何实现?有任何想法吗?

编辑:为清楚起见,绿色框是在插图软件中手工绘制的。我需要通过代码绘制绿色框。

完整源代码在GitHub https://github.com/huse360/LetterFrame中 这是主要的源代码

 override func draw(_ rect: CGRect) {
        guard let context = UIGraphicsGetCurrentContext() else { return }
   
    
       context.textMatrix = .identity
       context.translateBy(x: 0,y: self.bounds.size.height)
       context.scaleBy(x: 1.0,y: -1.0)
   
    
       let string = "|優勝《ゆうしょう》の|懸《か》かった|試合《しあい》。|Test《テスト》.\nThe quick brown fox jumps over the lazy dog. 12354567890 @#-+"

    
       let attributedString = Utility.sharedInstance.furigana(String: string)
   
       let range = attributedString.mutableString.range(of: attributedString.string)

       attributedString.addAttribute(.font,value: font,range: range)

   
       let framesetter = attributedString.framesetter()
   
       let textBounds = self.bounds.insetBy(dx: 20,dy: 20)
       let frame = framesetter.createFrame(textBounds)
    
//Draw the frame text:
   
       frame.draw(in: context)
           
       let origins = frame.lineOrigins()
   
       let lines = frame.lines()

        context.setstrokeColor(UIColor.red.cgColor)
        context.setlinewidth(0.7)

        for i in 0 ..< origins.count {

            let line = lines[i]
         
            for run in line.glyphRuns() {
            
                let font = run.font
                let glyPHPositions = run.glyPHPositions()
                let glyphs = run.glyphs()
            
                let glyphsBoundingRects =  font.boundingRects(of: glyphs)
            
//DRAW the bounding Box for each glyph:
            
                for k in 0 ..< glyPHPositions.count {
                    let point = glyPHPositions[k]
                    let gRect = glyphsBoundingRects [k]
            
                    var Box = gRect
                    Box.origin +=  point + origins[i] + textBounds.origin
                    context.stroke(Box)
                        
                }// for k
        
            }//for run
        
       }//for i
    
    }//func draw

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...