2014-11-21 15 views
5

Frage

Ich versuche, eine benutzerdefinierte UIView-Komponente in Swift Playground zu machen. Ich habe herausgefunden, wie man alles macht, außer um Text korrekt zu rendern. Ich bin mir nicht sicher, ob ich das über Core Graphics oder Core Text machen soll oder wie genau es funktioniert.Swift - Playground - Kerngrafiken/Kerntext/Benutzerdefinierte Ansicht

Wie Sie im Bild sehen, unten ich Text rendern wollen beide den Kopf und die rechte Seite nach oben auf beiden Seiten meiner benutzerdefinierten Ansicht Component

enter image description here

ich verschiedene Weise versucht haben, die zu bekommen Text funktioniert, aber ich streife weiter. Wenn jemand in der Lage wäre, mir zu zeigen, wie man meinen Spielplatz ändert, um ein paar Text-Rendering hinzuzufügen, das großartig wäre.

Spielplatz-Code

// Playground - noun: a place where people can play 

import Foundation 
import UIKit 

class RunwayView : UIView { 


    var northText : String? 
    var southText : String? 


    ///Draws the "runway" 
    override func drawRect(rect: CGRect) { 

     // Setup graphics context 
     var ctx = UIGraphicsGetCurrentContext() 


     // clear context 
     CGContextClearRect(ctx, rect) 
     let parentVieBounds = self.bounds 
     let width = CGRectGetWidth(parentVieBounds) 
     let height = CGRectGetHeight(parentVieBounds) 


     // Setup the heights 
     let endZoneHeight : CGFloat = 40 


     /* Remember y is negative and 0,0 is upper left*/ 


     //Create EndZones 
     CGContextSetRGBFillColor(ctx, 0.8, 0.8, 0.8, 1) 
     CGContextFillRect(ctx, CGRectMake(0, 0, width, endZoneHeight)) 
     CGContextFillRect(ctx, CGRectMake(0, height-endZoneHeight, width, endZoneHeight)) 


     var northString = NSMutableAttributedString(string: "36") 
     var attrs = [NSFontAttributeName : UIFont.systemFontOfSize(16.0)] 
     var gString = NSMutableAttributedString(string:"g", attributes:attrs); 


     var line = CTLineCreateWithAttributedString(gString) 

     CGContextSetTextPosition(ctx, 10, 50); 
     CTLineDraw(line, ctx); 

     // Clean up 

    } 

} 


var outerFrame : UIView = UIView(frame: CGRectMake(20,20,400,400)) 
var runway1 : RunwayView = RunwayView(frame: CGRectMake(0,0,30,260)) 
var runway2 : RunwayView = RunwayView(frame: CGRectMake(80,0,30,340)) 

runway1.transform = CGAffineTransformConcat(CGAffineTransformMakeTranslation(20, 200), 
    CGAffineTransformMakeRotation(-0.785398163)) 
outerFrame.addSubview(runway1) 

runway2.transform = CGAffineTransformConcat(CGAffineTransformMakeTranslation(120, 140), 
    CGAffineTransformMakeRotation(-0.585398163)) 

outerFrame.addSubview(runway2) 



outerFrame.backgroundColor = UIColor.yellowColor() 
outerFrame.clipsToBounds = true 


// View these elements 
runway1 
outerFrame 
runway1 

Antwort

9

Ich schrieb eine Nutzenfunktion für meine app Text mit CoreText zu ziehen. Es gibt die Textgröße für die weitere Verarbeitung:

func drawText(context: CGContextRef, text: NSString, attributes: [String: AnyObject], x: CGFloat, y: CGFloat) -> CGSize { 
    let font = attributes[NSFontAttributeName] as UIFont 
    let attributedString = NSAttributedString(string: text, attributes: attributes) 

    let textSize = text.sizeWithAttributes(attributes) 

    // y: Add font.descender (its a negative value) to align the text at the baseline 
    let textPath = CGPathCreateWithRect(CGRect(x: x, y: y + font.descender, width: ceil(textSize.width), height: ceil(textSize.height)), nil) 
    let frameSetter = CTFramesetterCreateWithAttributedString(attributedString) 
    let frame  = CTFramesetterCreateFrame(frameSetter, CFRange(location: 0, length: attributedString.length), textPath, nil) 

    CTFrameDraw(frame, context) 

    return textSize 
} 

Nennen Sie es wie folgt:

var textAttributes: [String: AnyObject] = [ 
    NSForegroundColorAttributeName : UIColor(white: 1.0, alpha: 1.0).CGColor, 
    NSFontAttributeName : UIFont.systemFontOfSize(17) 
] 

drawText(ctx, text: "Hello, World!", attributes: textAttributes, x: 50, y: 50) 

Hoffnung, das Ihnen hilft.

+2

Warum zeichnet dies auf den Kopf? –

+0

@Alexander: In iOS müssen Sie zuerst das Koordinatensystem spiegeln. – zisoft

+0

'CGRect.integral' (Swift 3) oder' CGRectIntegeral (rect) '(Swift 2) ist besser als' ceil' –