Core Text 系列1 - 初探

Core Text

iOS圖文混排的機制

步驟如下:

1.獲取繪制上下文

 UIGraphicsGetCurrentContext() 
 UIGraphicsBeginImageContextWithOptions(self.bounds.size, true,  UIScreen.mainScreen().scale)

2.坐標系的轉換

//坐標系轉換
CGContextSetTextMatrix(ctx, CGAffineTransformIdentity)
CGContextTranslateCTM(ctx, 0, self.bounds.size.height)
CGContextScaleCTM(ctx, 1.0, -1.0)

3.創建繪制區域

let path = CGPathCreateMutable()
CGPathAddRect(path, nil, self.bounds)

4.創建屬性字符串

let attributed =  NSMutableAttributedString(string: "??我是屬性字符串");
、、、在此添加屬性

5.繪制

 //創建CTFramesetter
  let ctFrameSetter = CTFramesetterCreateWithAttributedString(attributed)
        
 //創建CTFrame
let ctFrame = CTFramesetterCreateFrame(ctFrameSetter, CFRangeMake(0, attributed.length),path , nil)
        
 //根據CTFrame繪制
 CTFrameDraw(ctFrame, ctx)

eg:CoreTextLayer.swft

 override func display() {
        self.contentsScale = UIScreen.mainScreen().scale
        super.display()
    }
    override func drawInContext(ctx: CGContext) {
        super.drawInContext(ctx)
       
        //坐標系轉換
        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????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????這,我是要給兵個??????????");
        
        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))
        
        //創建CTFramesetter
       let ctFrameSetter = CTFramesetterCreateWithAttributedString(attributed)
        
        //創建CTFrame
       let ctFrame = CTFramesetterCreateFrame(ctFrameSetter, CFRangeMake(0, attributed.length),path , nil)
        
        //根據CTFrame繪制
       CTFrameDraw(ctFrame, ctx)
 
    }
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容