2017-02-28 53 views
3

背景:我吸取了核芯顯卡一個UIImageView。我想最終在覈心圖形繪製上繪製一個文本字符串。繪製了核芯顯卡文本斯威夫特3

This (hackingwithswift.com)方法我用文字繪製到的圖像,我的ImageView設置爲圖像,然後畫在與核心顯卡。但我希望文字覆蓋核心圖形繪製。

現在我已經搬到這個https://stackoverflow.com/a/35574171/6337391,但它仍然心不是讓我對我的畫上書寫。

class MapViewController: UIViewController { 

    @IBOutlet var imageView: UIImageView! 

    func drawScale() { 
     // establish 6 points to draw a little bar with 
     let length = CGFloat(80.0) 
     let bottom = imageView.bounds.size.height - 15.0 
     let left = CGFloat(20.0) 
     let top = bottom - 20.0 
     let middle = top + 10.0 
     let right = left + length 
     let topLeft = CGPoint(x: left, y: top) 
     let topRight = CGPoint(x: right, y: top) 
     let bottomRight = CGPoint(x: right, y: bottom) 
     let bottomLeft = CGPoint(x: left, y: bottom) 
     let middleLeft = CGPoint(x: left, y: middle) 
     let middleRight = CGPoint(x: right, y: middle) 

     // draw the bar 
     drawLineFrom(fromPoint: topLeft, toPoint: bottomLeft, color: UIColor.red.cgColor, width: 1.0, alias: false) 
     drawLineFrom(fromPoint: topRight, toPoint: bottomRight, color: UIColor.red.cgColor, width: 1.0, alias: false) 
     drawLineFrom(fromPoint: middleLeft, toPoint: middleRight, color: UIColor.red.cgColor, width: 1.0, alias: false) 

     // draw a text string 
     UIGraphicsBeginImageContext(self.imageView.frame.size) 

     let paragraphStyle = NSMutableParagraphStyle() 
     paragraphStyle.alignment = .center 

     let attrs = [ 
      NSFontAttributeName: UIFont.systemFont(ofSize: 12), 
      NSParagraphStyleAttributeName: paragraphStyle, 
      NSForegroundColorAttributeName: UIColor.red] 

     let scaleString = "45 feet" 
     scaleString.draw(at: CGPoint(x: 0, y: 50), withAttributes: attrs) 

     let newImage = UIGraphicsGetImageFromCurrentImageContext() 
     UIGraphicsEndImageContext() 
    } 

    /* Stroke a line between two CGPoints. Color, width, anti-alias options. */ 
    func drawLineFrom(fromPoint: CGPoint, toPoint: CGPoint, color: CGColor, width: CGFloat, alias: Bool) { 
     // 1 
     UIGraphicsBeginImageContext(self.imageView.frame.size) 
     let context = UIGraphicsGetCurrentContext()! 
     context.setShouldAntialias(alias) 
     self.imageView.image?.draw(in: CGRect(x: 0, y: 0, width: self.imageView.frame.size.width, height: self.imageView.frame.size.height)) 

     // 2 
     context.move(to: fromPoint) 
     context.addLine(to: toPoint) 

     // 3 
     context.setLineWidth(width) 
     context.setStrokeColor(color) 

     // 4 
     context.strokePath() 

     // 5 
     self.imageView.image = UIGraphicsGetImageFromCurrentImageContext() 
     self.imageView.alpha = 1.0 
     UIGraphicsEndImageContext() 
    } 
} 

不工作。繪製文本字符串完全擦除欄。如果將條形圖代碼移動到字符串繪圖代碼下,那麼兩者都是可見的,但當然條形圖會在字符串上顯示。

+0

你能在CATextLayer創建文本,並將其添加到圖像視圖? – dfd

+0

你說的「我吸取了核芯顯卡一個UIImageView」,以及在什麼類,你運行你的代碼是什麼意思? –

+0

我添加了一些contextualizing代碼,希望它有幫助。我認爲答案將存在於核心文本中,我將嘗試自己尋找解決方案。比如像[鏈接](http://stackoverflow.com/questions/27052665/swift-playground-core-graphics-core-text-custom-view)看好 –

回答

0

夫特3.0:繪製文本

func drawMyText(myText:String,textColor:UIColor, FontName:String, FontSize:CGFloat, inRect:CGRect){ 

    let textFont = UIFont(name: FontName, size: FontSize)! 
    let textFontAttributes = [ 
     NSFontAttributeName: textFont, 
     NSForegroundColorAttributeName: textColor, 
     ] as [String : Any] 

    myText.draw(in: inRect, withAttributes: textFontAttributes) 
} 

呼叫此功能,下面是例如

let _rect = CGRect(x: 40, y: 100, width: 40, height: 40) 

drawMyText(myText: "Hello", textColor: UIColor.black, FontName: "Helvetica Bold", FontSize: 11 , inRect:_rect) 
+0

這只是從我的代碼不同之處在於你刪除的UIGraphicsBeginImageContext( self.imageView.frame.size)。您的代碼在我的情況下不會繪製任何文字。 –

+0

把drawMyText功能UIView子類和drawRect中函數中調用它,請讓我知道如果你需要進一步的幫助 – Devesh

+1

我的問題是繪製文本刪除了我以前畫的畫。在我上面的例子中,文本將出現,但它完全擦除了我以前繪製的線條。據我所知,你唯一的建議是我直接寫文本而不是一點,這對我來說沒有任何幫助。 –

2

迅速4

let paragraphStyle = NSMutableParagraphStyle() 
    paragraphStyle.alignment = .center 

    let attributes = [NSAttributedStringKey.paragraphStyle : paragraphStyle, 
         NSAttributedStringKey.font   : UIFont.systemFont(ofSize: 12.0), 
         NSAttributedStringKey.foregroundColor : UIColor.blue, 
         ] 

    let myText = "HELLO" 
    let attrString = NSAttributedString(string: myText, 
           attributes: attributes) 

    let rt = CGRect(x: W/2 - 50, y: border, width: 100, height: H) 
    attrString.draw(in: rt)