Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// Source for the Accidental Tech Podcast (ATP) T-Shirt:
// http://www.marco.org/2014/04/29/atp-shirts
//
// By Troy Gaul, June 2, 2014.
//
// Based on Obj-C veresion by Marco Arment, April 28, 2014. MIT license.
// https://gist.github.com/marcoarment/2542cd28cb5df0aa97d8
import UIKit
class ATPLogoView: UIView {
override func drawRect(rectIgnored: CGRect)
{
let fontName = "MyriadPro-Semibold"
let title = NSLocalizedString("Accidental Tech Podcast", comment: "")
let initials = NSLocalizedString("ATP", comment: "") as NSString
let rect = CGRect(origin: CGPointZero, size: self.bounds.size)
let w = rect.size.width
let offsetY = w * -0.07
var ringRect = CGRectInset(rect, w * 0.08, w * 0.08)
ringRect.origin.y += offsetY
let ring = UIBezierPath(ovalInRect: ringRect)
ring.lineWidth = ceil(w * 0.012)
UIColor(white: 0.3, alpha: 1.0).setStroke()
ring.stroke()
var innerRingRect = CGRectInset(rect, w * 0.21, w * 0.21)
innerRingRect.origin.y += offsetY
let inner = UIBezierPath(ovalInRect: innerRingRect)
inner.lineWidth = ceil(w * 0.007)
UIColor(white: 0.7, alpha: 1.0).setStroke()
inner.stroke()
let pStyle = NSMutableParagraphStyle()
pStyle.alignment = .Center
let titleAttributes = [
NSFontAttributeName: UIFont(name: fontName, size: w * 0.068),
NSParagraphStyleAttributeName: pStyle,
NSForegroundColorAttributeName: UIColor.whiteColor(),
NSKernAttributeName: w * 0.0037
]
let t = title.uppercaseString as NSString
let titleRect = CGRect(x: 0, y: w * 0.91, width: w, height: w * 0.09)
let o = NSStringDrawingOptions.UsesLineFragmentOrigin
t.drawWithRect(titleRect, options: o, attributes: titleAttributes, context: nil)
let bs = CGSize(width: w * 0.44, height: w * 0.19)
var bOrigin = CGPoint(x: (w - bs.width) / 2.0, y: (w - bs.height) / 2.0)
bOrigin.y += offsetY
let b = CGRect(origin: bOrigin, size: bs)
let cX = CGRectGetMidX(b), cY = CGRectGetMidY(b)
var transform = CGAffineTransformMakeTranslation(cX, cY)
transform = CGAffineTransformRotate(transform, 0.5236)
transform = CGAffineTransformTranslate(transform, -cX, -cY)
CGContextConcatCTM(UIGraphicsGetCurrentContext(), transform)
UIColor(red: 0.12, green: 0.25, blue: 0.4, alpha: 1.0).setFill()
UIBezierPath(roundedRect: b, cornerRadius: bs.height * 0.15).fill()
let a = titleAttributes.mutableCopy() as NSMutableDictionary
a[NSFontAttributeName] = UIFont(name: fontName, size: bs.height * 0.84)
a[NSKernAttributeName] = bs.height * 0.04
let r = CGRectInset(b, 0, 0.17 * bs.height)
initials.drawWithRect(r, options: o, attributes: a, context: nil)
}
}
@felixjung

This comment has been minimized.

Copy link

commented Jun 3, 2014

I can't get this to draw anything in Playground (I'm a complete iOS beginner). Tried the following bit right underneath the class definition in an iOS playground:

let point = CGPoint(x: 0, y: 0)
let size = CGSize(width: 200, height: 200)

var myRect = CGRect(origin: point, size: size)
var logo = ATPLogoView()
logo.drawRect(myRect)
@shmidt

This comment has been minimized.

Copy link

commented Jun 3, 2014

@feju It seems that playground works only with Foundation.

@ZevEisenberg

This comment has been minimized.

Copy link

commented Jun 3, 2014

@feju I haven't used swift yet, but just based on UIKit, I don't think that's right. You're basically initializing a UIView subclass with no frame, which is to say, CGRrctZero. Then you're calling drawRect. What you should be doing is initializing the view with myRect. drawRect will be called for you the first time the view needs to be drawn. If the state of your app changes later, and you need to update the view, you call setNeedsDisplay or setNeedsDisplayInRect, and the region will be marked as dirty and redrawn on the next iteration of the run loop.

@tgaul

This comment has been minimized.

Copy link
Owner Author

commented Jun 3, 2014

I wrote this by creating a starter project using the Xcode iOS Single View Application template with the Swift language. I then added a UIView to the storyboard, made it square (by setting up auto-layout as appropriate), and set its class to ATPLogoView. Finally, since this uses MyriadPro-Semibold which isn't on iOS, I found that file in Font Book, added it to the project, and created an entry in the Info.plist under UIAppFonts listing its filename.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.