美文网首页
Core Text系列2 - 嵌入图片

Core Text系列2 - 嵌入图片

作者: 离子来了 | 来源:发表于2016-05-11 22:12 被阅读74次

    步骤

    1.将一个属性字符串,添加上代理和独一无二的属性,让其称为一个单独的CTRun,这个CTRun的Rect由回调返回。
    直接上代码,太晚了

    //坐标系转换
            CGContextSetTextMatrix(ctx, CGAffineTransformIdentity)
            CGContextTranslateCTM(ctx, 0, self.bounds.size.height)
            CGContextScaleCTM(ctx, 1.0, -1.0)
            
            //创建绘制的区域
            let path = CGPathCreateMutable()
            CGPathAddRect(path, nil, self.bounds)
            
            // 4.创建需要绘制的文字
            let attributed =  NSMutableAttributedString(string: "估后共和国开不开vbdkaph估后共和国开不开vbdkaph😢😊😊😢⬇️😢😊😊😢⬇️😢😊😊😢⬇️😢😊😊😢⬇️😢😊😊😢⬇️😢😊😊😢⬇️😢😊😊😢⬇️😢😊😊😢⬇️😢😊😊😢⬇️😢😊😊😢⬇️😢😊😊😢⬇️😢😊😊😢⬇️😢😊😊😢⬇️😢😊😊😢⬇️😢😊😊😢⬇️😢😊😊😢⬇️😢😊😊😢⬇️😢😊😊😢⬇️😢😊😊😢⬇️😢😊😊😢⬇️😢😊😊😢⬇️😢😊😊😢⬇️😢😊😊😢⬇️😢😊😊😢⬇️😢😊😊😢⬇️😢😊😊😢⬇️😢😊😊😢⬇️😢😊😊😢⬇️😢😊😊😢⬇️😢😊😊😢⬇️😢😊😊😢⬇️😢😊😊😢⬇️😢😊😊😢⬇️😢😊😊😢⬇️😢😊😊😢⬇️😢😊😊😢⬇️😢😊😊😢⬇️😢😊😊😢⬇️😢😊😊😢⬇️😢😊😊😢⬇️😢😊😊😢⬇️😢😊😊😢⬇️😢😊😊😢⬇️😢😊😊😢⬇️😢😊😊😢⬇️😢😊😊😢⬇️😢😊😊😢⬇️😢😊😊😢⬇️😢😊😊😢⬇️😢😊😊😢⬇️😢😊😊😢⬇️😢😊😊😢⬇️😢😊😊😢⬇️😢😊😊😢⬇️😢😊😊😢⬇️😢😊😊😢⬇️😢😊😊😢⬇️😢😊😊😢⬇️😢😊😊😢⬇️😢😊😊😢⬇️😢😊😊😢⬇️😢😊😊😢⬇️这是我的第一个coreText demo,我是要给兵来自老白干I型那个饿哦个呢给个I类回滚igkhpwfh 评估后共和国开不开vbdkaphphohghg 的分工额好几个辽宁省更怕hi维护你不看hi好人佛【井柏然把饿哦个😢😊😊😢⬇️");
            
            attributed.addAttribute(NSFontAttributeName, value: UIFont.systemFontOfSize(20), range: NSMakeRange(0, 5));
            
            attributed.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: NSMakeRange(3, 10));
            
            attributed.addAttribute(NSForegroundColorAttributeName, value: UIColor.orangeColor(), range: NSMakeRange(0, 2));
            
            //创建段落属性
            let paraStyle = NSMutableParagraphStyle()
            
            attributed.addAttribute(NSParagraphStyleAttributeName, value: paraStyle, range: NSMakeRange(0, attributed.length))
            
            
            //添加图片的逻辑 start
            let drawImageName = "page1.jpg"
           
            var runCallBack = CTRunDelegateCallbacks(version: kCTRunDelegateVersion1, dealloc: { (r) in
                print("ctRunDelegate is die")
                }, getAscent: { (r) -> CGFloat in
                    return 100
                }, getDescent: { (r) -> CGFloat in
                    return 0
                }) { (width) -> CGFloat in
                    return 50
            }
            
        
            var runDelegateFlag = "runDelegateDlag"
            let ctRunDelegate =  CTRunDelegateCreate(&runCallBack, &runDelegateFlag)
            
            let picAttribute  = NSMutableAttributedString(string: " ")
            
            picAttribute.addAttribute((kCTRunDelegateAttributeName as String), value: ctRunDelegate!, range: NSMakeRange(0, 1))
            picAttribute.addAttribute("imageName", value: drawImageName, range: NSMakeRange(0, 1))
            
            attributed.insertAttributedString(picAttribute, atIndex: 60)
            //添加图片的逻辑end
            
           let ctFrameSetter = CTFramesetterCreateWithAttributedString(attributed)
            
            let ctFrame = CTFramesetterCreateFrame(ctFrameSetter, CFRangeMake(0, attributed.length), path, nil)
            
            CTFrameDraw(ctFrame, ctx)
            
            let lines = CTFrameGetLines(ctFrame) as NSArray
            var originsArray = Array<CGPoint>(count: lines.count, repeatedValue: CGPointZero)//用于存储每一行的坐标
            
            CTFrameGetLineOrigins(ctFrame, CFRangeMake(0, 0), &originsArray)
        
            
            for line in lines {
                var lineAscent:CGFloat      = 0
                var lineDescent:CGFloat     = 0
                var lineLeading:CGFloat     = 0
                CTLineGetTypographicBounds(line as! CTLine, &lineAscent, &lineDescent, &lineLeading)
                
                
               let runs = CTLineGetGlyphRuns(line as! CTLine) as NSArray
                
                for (index,run) in runs.enumerate() {
                    var runAscent:CGFloat      = 0
                    var runDescent:CGFloat     = 0
                    var runLeading:CGFloat     = 0
                    let lineOrigin             = originsArray[index]
                    
                   let width =  CTRunGetTypographicBounds(run as! CTRun, CFRangeMake(0, 0), &runAscent, &runDescent, &runLeading)
                    /*
                     下面这个计算runRect的记住就行
                     */
                    let runX = lineOrigin.x + CTLineGetOffsetForStringIndex(line as! CTLine, CTRunGetStringRange(run as! CTRun).location, nil)
                    let runY = lineOrigin.y - runDescent
                    let runRect = CGRect(x: runX, y: runY, width: CGFloat(width), height: runDescent+runAscent)
                    
                    let runAttributes = CTRunGetAttributes(run as! CTRun) as NSDictionary
                    
                    let imagename = runAttributes.objectForKey("imageName")
                    
                    if let imageN = imagename {
                        let image = UIImage(named: imageN as! String)
                        let imagebouns = CGRect(x: runRect.origin.x, y: runRect.origin.y, width: runRect.size.width, height: runRect.size.height)
                        CGContextDrawImage(ctx, imagebouns, image?.CGImage)
                    }
                    
                    
                }
            }
    

    相关文章

      网友评论

          本文标题:Core Text系列2 - 嵌入图片

          本文链接:https://www.haomeiwen.com/subject/ywvurttx.html