前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >IOS 图文新闻文章样式

IOS 图文新闻文章样式

作者头像
用户5760343
发布2019-07-10 15:46:10
5590
发布2019-07-10 15:46:10
举报
文章被收录于专栏:sktjsktj

//在实现图文混排的功能之前,首先往项目中添加一个继承自UIView 父类的子类CTImageView.swift。使用该类的draw方法,并在该方法中使 用Core Text框架渲染富文本, 1 let picWidth = CGFloat(200.0) 2 let picHeight = CGFloat(133.0) 3 UIColor.brown.setFill() 4 UIRectFill(rect) 5 var ctRunCallback = CTRunDelegateCallbacks(version: kCTRunDelegateVersion1, dealloc: { (refCon) -> Void in 6 }, getAscent:{ ( refCon) -> CGFloat in 7 return picHeight 8 }, getDescent:{ (refCon) -> CGFloat in 9 return 0 10 }) { (refCon) -> CGFloat in 11 return picWidth 12 } 13 var picture = “coffee “ 14 let ctRunDelegate = CTRunDelegateCreate(&ctRunCallback, &picture) 15 let placeHolder = NSMutableAttributedString(string:” “) 16 placeHolder.addAttribute(kCTRunDelegateAttributeName as String, value:ctRunDelegate!, range: NSMakeRange(0, 1)) 17 placeHolder.addAttribute(“pictureName”, value: picture, range:NSMakeRange(0, 1)) 18 let article = “咖啡(coffee)是采用经过烘焙的咖啡豆所 制作出来\n\n的饮料,通常为热饮,但也有 作为冷饮的冰咖啡。 咖啡是人类社会流行范围最为广泛的饮料之一,也是重要经济作 物。在繁忙的工作生活 之余,我们可以去尝试做自己的咖啡。” 19 let attributedStr = NSMutableAttributedString(string: article) 20 attributedStr.insert(placeHolder, at:27) 21 attributedStr.addAttribute(kCTUnderlineStyleAttributeName as String, value:NSNumber(value:1), range: NSRange(location:0, length:attributedStr.length)) 22 let framesetter = CTFramesetterCreateWithAttributedString(attributedStr) 23 let path = UIBezierPath(rect:rect) 24 let ctFrame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, attributedStr.length), path.cgPath, nil) 25 26 let crtContext = UIGraphicsGetCurrentContext() 27 crtContext!.textMatrix = CGAffineTransform.identity 28 crtContext?.scaleBy(x:1.0, y:-1.0) 29 crtContext?.translateBy(x:0, y: self.bounds.size.height * -1) 30 CTFrameDraw(ctFrame, crtContext!) 31 let ctLines = CTFrameGetLines(ctFrame) as NSArray 32 var originsOfLines = CGPoint 33 for _ in 0..<ctLines.count{ 34 originsOfLines.append(CGPoint.zero) 35 } 36 let range:CFRange = CFRangeMake(0, 0) 37 CTFrameGetLineOrigins(ctFrame, range, &originsOfLines) 38 for i in 0..<ctLines.count 39 { 40 let ctLineOrigin = originsOfLines[i] 41 let ctRuns = CTLineGetGlyphRuns(ctLines[i] as! CTLine) as NSArray 42 43 for ctRun in ctRuns 44 { 45 let ctAttributes = CTRunGetAttributes(ctRun as! CTRun) as NSDictionary 46 let pictureName = ctAttributes.object(forKey:“pictureName”) 47 if pictureName!= nil 48 { 49 let offset = CTLineGetOffsetForStringIndex(ctLines[i] as!CTLine, CTRunGetStringRange(ctRun as! CTRun).location, nil) 50 let picturePosX = ctLineOrigin.x + offset 51 52 let pictureFrame = CGRect(x:picturePosX, y: ctLineOrigin.y, width:picWidth, height: picHeight) 53 let image = UIImage(named:pictureName as!String) 54 crtContext?.draw((image?.cgImage)!, in: pictureFrame) 55 } 56 } 57 }

//在viewcontrol.swift 1 let imageView = CTImageView() 2 imageView.frame = CGRect(x:20, y:80, width:280, height:280) 3 self.view.addSubview(imageView)

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019.07.06 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档