首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往
您找到你想要的搜索结果了吗?
是的
没有找到

IOS 图文新闻文章样式

//在实现图文混排的功能之前,首先往项目中添加一个继承自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.co

02

IOS 改变字体样式 **

1 import UIKit 2 import CoreText 3 4 class ViewController:UIViewController { 5 6 override func viewDidLoad() { 7 super.viewDidLoad() 8 // Do any additional setup after loading the view, typically from a nib. 9 10 let label = UILabel(frame:CGRect(x:10, y:60, width:300, height:40)) 11 let string = NSMutableAttributedString(string:“Interactive tutorials for Xcode”) 12 13 let font = CTFontCreateWithName(“CourierNewPSBoldMT” as CFString?, 22, nil) 14 string.addAttribute(kCTFontAttributeName as String, value:font, range:NSRange(location:0, length:11)) 15 16 var number = 3 17 let cfNumber = CFNumberCreate(kCFAllocatorDefault, CFNumberType.sInt8Type, &number) 18 string.addAttribute(kCTStrokeWidthAttributeName as String, value:cfNumber!, range: NSMakeRange(12, 9)) 19 20 let italicFont = UIFont.italicSystemFont(ofSize:14) 21 let fontValue = CTFontCreateWithName(italicFont.fontName as CFString?, 14, nil) 22 string.addAttribute(kCTFontAttributeName as String, value:fontValue, range: NSRange(location:22, length:3)) 23 24 string.addAttribute(kCTUnderlineStyleAttributeName as String, value:NSNumber(value:1), range: NSRange(location:26, length:5)) 25 26 label.attributedText = string 27 self.view.addSubview(label) 28 } 29 }

03
领券