Drawing text on UIView

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • Drawing text on UIView

    Hello everybody,

    I'm trying to build a small app for iPhone/iPad, for this, I've build already a sub class of UIView using UIGraphicsGetCurrentContext where I can draw (lines, rectangles ...) but I didn't find the way to draw text. Can somebody help?
    I wrote in English aber Sie können ruhig auf Deutsch antworten.

    Thanks

    Philippe Lagarrigue
  • Danke aber es ist leider in ObjectiveC, gibt es irgendwo Beispiele in Swift ?
    Allein diese folgende Anweisung ist nicht einfach zu konvertieren :

    Quellcode

    1. CFDictionaryRef attributes = CFDictionaryCreate(kCFAllocatorDefault,[color=#FFA07A] (const void**)&keys, (const void**)&values, sizeof(keys) / sizeof(keys[0]), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks[/color]);
  • Ich habe's endlich gefunden und zeige wie damit anderen profitieren können:

    Quellcode

    1. override func drawRect(rect: CGRect) {
    2. var context = UIGraphicsGetCurrentContext()
    3. // Drawing code
    4. if self.xPos != -1.0 {
    5. CGContextSetShadow (context, CGSizeMake(5, 5), 5) // Set shadow
    6. CGContextSetLineWidth(context, 1.0)
    7. let rectangle = CGRectMake(self.xPos + 10, self.yPos + 10, 50, 50)
    8. CGContextSetAlpha(context, 1.0)
    9. CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1.0)
    10. CGContextFillRect(context, rectangle)
    11. CGContextAddRect(context, rectangle)
    12. CGContextStrokePath(context)
    13. CGContextSetShadow (context, CGSizeMake(0, 0), 0) // Reset shadow
    14. var myString = "19"
    15. var textStyle = NSMutableParagraphStyle.defaultParagraphStyle().mutableCopy() as NSMutableParagraphStyle
    16. textStyle.alignment = NSTextAlignment.Center
    17. let textFontAttributes = [
    18. NSFontAttributeName: UIFont.boldSystemFontOfSize(32),
    19. NSForegroundColorAttributeName: UIColor.brownColor(),
    20. NSParagraphStyleAttributeName: textStyle
    21. ]
    22. myString.drawInRect(CGRectMake(xPos, yPos, self.squareSide, self.squareSide), withAttributes: textFontAttributes)
    23. }
    24. }
    Alles anzeigen