Skip to content

Commit

Permalink
update for Swift 1.2, Xcode 6.3
Browse files Browse the repository at this point in the history
  • Loading branch information
mattneub committed Apr 16, 2015
1 parent 724bc3d commit ff496c7
Show file tree
Hide file tree
Showing 37 changed files with 805 additions and 161 deletions.
Expand Up @@ -32,8 +32,8 @@ class MasterViewController: UITableViewController {
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showDetail" {
let indexPath = self.tableView.indexPathForSelectedRow()!
let object = objects[indexPath.row] as NSDate
(segue.destinationViewController as DetailViewController).detailItem = object
let object = objects[indexPath.row] as! NSDate
(segue.destinationViewController as! DetailViewController).detailItem = object
}
}

Expand All @@ -58,9 +58,9 @@ class MasterViewController: UITableViewController {
*/

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell

let object = objects[indexPath.row] as NSDate
let object = objects[indexPath.row] as! NSDate
cell.textLabel!.text = object.description
return cell
}
Expand Down
Expand Up @@ -16,12 +16,12 @@ class ViewController : UIViewController {
NSNotificationCenter.defaultCenter().addObserver(self, selector: "doDynamicType:", name: UIContentSizeCategoryDidChangeNotification, object: nil)

// UIFont.familyNames()
// .map{UIFont.fontNamesForFamilyName($0 as String)}.map(println)
// .map{UIFont.fontNamesForFamilyName($0 as! String)}.map(println)

}

func doDynamicType(n:NSNotification) {
let style = self.lab.font.fontDescriptor().objectForKey(UIFontDescriptorTextStyleAttribute) as String
let style = self.lab.font.fontDescriptor().objectForKey(UIFontDescriptorTextStyleAttribute) as! String
self.lab.font = UIFont.preferredFontForTextStyle(style)
}

Expand Down
Expand Up @@ -15,31 +15,38 @@ class ViewController : UIViewController {
func doDynamicType(n:NSNotification!) {
var fbody : UIFont!
var femphasis : UIFont!
let which = 3
let which = 1
switch which {
case 1:
let body = UIFontDescriptor.preferredFontDescriptorWithTextStyle(UIFontTextStyleBody)
let emphasis = body.fontDescriptorWithSymbolicTraits(.TraitItalic)
fbody = UIFont(descriptor: body, size: 0)
femphasis = UIFont(descriptor: emphasis, size: 0)
if let emphasis = body.fontDescriptorWithSymbolicTraits(.TraitItalic) {
fbody = UIFont(descriptor: body, size: 0)
femphasis = UIFont(descriptor: emphasis, size: 0)
}
case 2:
// this should work but doesn't (bug?)
fbody = UIFont(name: "GillSans", size: 15)
let emphasis = fbody.fontDescriptor().fontDescriptorWithSymbolicTraits(.TraitItalic)
femphasis = UIFont(descriptor: emphasis, size: 0)
// this should work but doesn't (bug?), and we crash later
// whoa, in iOS 8.3 it works!
if let body = UIFont(name: "GillSans", size: 15),
emphasis = body.fontDescriptor().fontDescriptorWithSymbolicTraits(.TraitItalic) {
fbody = body
femphasis = UIFont(descriptor: emphasis, size: 0)
}
case 3:
// the workaround is drop down to Core Text
// unfortunately Swift seems unaware that CTFont and UIFont are now bridged
fbody = UIFont(name: "GillSans", size: 15)
let result = CTFontCreateCopyWithSymbolicTraits(fbody as AnyObject as CTFont, 0, nil, .ItalicTrait, .ItalicTrait)
femphasis = result as AnyObject as UIFont
// whoa, in Swift 1.2 it has heard about this!
if let body = UIFont(name: "GillSans", size: 15),
result = CTFontCreateCopyWithSymbolicTraits(body as CTFont, 0, nil, .ItalicTrait, .ItalicTrait) {
fbody = body
femphasis = result as UIFont
}
default:break
}

let s = self.lab.text! as NSString
let s = self.lab.text!
let mas = NSMutableAttributedString(string: s, attributes: [NSFontAttributeName:fbody])
mas.addAttribute(NSFontAttributeName, value: femphasis, range: s.rangeOfString("wild"))
mas.addAttribute(NSFontAttributeName, value: femphasis, range: (s as NSString).rangeOfString("wild"))
self.lab.attributedText = mas
}

}
@@ -1,6 +1,6 @@

import UIKit
import CoreText
// import CoreText // no longer necessary to import core text

class ViewController : UIViewController {

Expand Down
Expand Up @@ -26,7 +26,7 @@ class ViewController : UIViewController {
var content : NSMutableAttributedString!
var content2 : NSMutableAttributedString!

let which = 5 // 0 ... 5
let which = 0 // 0 ... 5
switch which {
case 0, 1, 4, 5:
let s1 = "The Gettysburg Address, as delivered on a certain occasion " +
Expand Down Expand Up @@ -70,14 +70,15 @@ class ViewController : UIViewController {
NSFontAttributeName: UIFont(name:"HoeflerText-Black", size:24)!,
NSExpansionAttributeName: 0.3,
NSKernAttributeName: -4 // negative kerning bug fixed in iOS 8
// but they broke it again in iOS 8.3!
], range:NSMakeRange(0,1))
self.lab.attributedText = content2
self.tv.attributedText = content2
self.tv.contentInset = UIEdgeInsetsMake(20,0,0,0)
if which > 2 {fallthrough}
case 3, 4, 5:
content2.addAttribute(NSParagraphStyleAttributeName,
value:lend(){
value:lend {
(para:NSMutableParagraphStyle) in
para.headIndent = 10
para.firstLineHeadIndent = 10
Expand Down Expand Up @@ -106,9 +107,9 @@ class ViewController : UIViewController {
inRange:NSMakeRange(0,content.length),
options:opts,
usingBlock: {
(value:AnyObject!, range:NSRange, stop:UnsafeMutablePointer<ObjCBool>) -> Void in
value, range, stop in
println(range)
let font = value as UIFont
let font = value as! UIFont
if font.pointSize == 15 {
content.addAttribute(NSFontAttributeName,
value:UIFont(name: "Arial-BoldMT", size:20)!,
Expand Down
10 changes: 5 additions & 5 deletions bk2ch10p507tabStops/TextTabTest/ViewController.swift
Expand Up @@ -18,7 +18,7 @@ class ViewController : UIViewController {
let s = "Onions\t$2.34\nPeppers\t$15.2\n"
let mas = NSMutableAttributedString(string:s, attributes:[
NSFontAttributeName:UIFont(name:"GillSans", size:15)!,
NSParagraphStyleAttributeName:lend() {
NSParagraphStyleAttributeName:lend {
(p:NSMutableParagraphStyle) in
var tabs = [NSTextTab]()
let terms = NSTextTab.columnTerminatorsForLocale(NSLocale.currentLocale())
Expand Down Expand Up @@ -78,10 +78,10 @@ class ViewController : UIViewController {
let src = CGImageSourceCreateWithURL(url, nil)
let scale = UIScreen.mainScreen().scale
let w : CGFloat = 20 * scale
let d : [String:AnyObject] = [
kCGImageSourceShouldAllowFloat : kCFBooleanTrue,
kCGImageSourceCreateThumbnailWithTransform: kCFBooleanTrue,
kCGImageSourceCreateThumbnailFromImageAlways: kCFBooleanTrue,
let d : [NSObject:AnyObject] = [
kCGImageSourceShouldAllowFloat : true,
kCGImageSourceCreateThumbnailWithTransform: true,
kCGImageSourceCreateThumbnailFromImageAlways: true,
kCGImageSourceThumbnailMaxPixelSize: Int(w)
]
let imref =
Expand Down
Expand Up @@ -6,19 +6,17 @@ class ViewController: UIViewController {
@IBOutlet var lab : UILabel!

@IBAction func doUpdateLabel(sender:AnyObject?) {
let mas = self.lab.attributedText.mutableCopy() as NSMutableAttributedString
let mas = self.lab.attributedText.mutableCopy() as! NSMutableAttributedString
let r = (mas.string as NSString).rangeOfString("^0")
if r.length > 0 {
mas.addAttribute("HERE", value: 1, range: r)
mas.replaceCharactersInRange(r, withString: NSDate().description)
} else {
mas.enumerateAttribute("HERE", inRange: NSMakeRange(0, mas.length), options: nil) {
value, r, stop in
if let value = value as? Int {
if value == 1 {
mas.replaceCharactersInRange(r, withString: NSDate().description)
stop.memory = true
}
if let value = value as? Int where value == 1 {
mas.replaceCharactersInRange(r, withString: NSDate().description)
stop.memory = true
}
}
}
Expand Down
Expand Up @@ -14,6 +14,11 @@
"idiom" : "iphone",
"size" : "60x60",
"scale" : "2x"
},
{
"idiom" : "iphone",
"size" : "60x60",
"scale" : "3x"
}
],
"info" : {
Expand Down
Expand Up @@ -4,19 +4,14 @@ import UIKit

class StringDrawer : UIView {
@NSCopying var attributedText : NSAttributedString! {
didSet {
self.setNeedsDisplay()
}
didSet {
self.setNeedsDisplay()
}
}

override func drawRect(rect: CGRect) {
let r = rect.rectByOffsetting(dx: 0, dy: 2)
// unfortunately there's a huge bug in Swift:
// we can't "or" NSStringDrawingOptions values together
// I've resorted to the assistance of Objective-C
// let opts = NSString.combine(.TruncatesLastVisibleLine, with:.UsesLineFragmentOrigin)
// let opts = NSStringDrawingOptions.UsesLineFragmentOrigin

// bug trying to "or" NSStringDrawingOptions values together...
// fixed in Swift 1.2 / Xcode 6.3!
let opts : NSStringDrawingOptions = .TruncatesLastVisibleLine | .UsesLineFragmentOrigin

Expand Down

This file was deleted.

This file was deleted.

Expand Up @@ -4,9 +4,9 @@ import UIKit

class StringDrawer : UIView {
@NSCopying var attributedText : NSAttributedString! {
didSet {
self.setNeedsDisplay()
}
didSet {
self.setNeedsDisplay()
}
}

override func drawRect(rect: CGRect) {
Expand All @@ -15,13 +15,7 @@ class StringDrawer : UIView {
switch which {
case 1:
let r = rect.rectByOffsetting(dx: 0, dy: 2)
// unfortunately there's a huge bug in Swift:
// we can't "or" NSStringDrawingOptions values together
// you can say this:
// let options : NSStringDrawingOptions = NSStringDrawingOptions(rawValue: NSStringDrawingOptions.TruncatesLastVisibleLine.rawValue | NSStringDrawingOptions.UsesLineFragmentOrigin.rawValue)!
// but it crashes at runtime as being nil
// I've resorted to the assistance of Objective-C
let options = NSString.combine(.TruncatesLastVisibleLine, with:.UsesLineFragmentOrigin)
let options : NSStringDrawingOptions = .TruncatesLastVisibleLine | .UsesLineFragmentOrigin
self.attributedText.drawWithRect(r, options: options, context: nil)
case 2:
let lm = NSLayoutManager()
Expand Down
Expand Up @@ -2,4 +2,3 @@
// Use this file to import your target's public headers that you would like to expose to Swift.
//

#import "StringDrawer.h"
Expand Up @@ -17,7 +17,6 @@
C9A50D79180F835200F5B6CE /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = C9A50D78180F835200F5B6CE /* AppDelegate.m */; };
C9A50D7C180F835200F5B6CE /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = C9A50D7A180F835200F5B6CE /* Main.storyboard */; };
C9A50D81180F835300F5B6CE /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = C9A50D80180F835300F5B6CE /* Images.xcassets */; };
C9A50D9F1810085000F5B6CE /* StringDrawer.m in Sources */ = {isa = PBXBuildFile; fileRef = C9A50D9E1810085000F5B6CE /* StringDrawer.m */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -37,8 +36,6 @@
C9A50D7B180F835200F5B6CE /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; };
C9A50D80180F835300F5B6CE /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Images.xcassets; sourceTree = "<group>"; };
C9A50D87180F835300F5B6CE /* XCTest.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = XCTest.framework; path = Library/Frameworks/XCTest.framework; sourceTree = DEVELOPER_DIR; };
C9A50D9D1810085000F5B6CE /* StringDrawer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = StringDrawer.h; sourceTree = "<group>"; };
C9A50D9E1810085000F5B6CE /* StringDrawer.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = StringDrawer.m; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -90,8 +87,6 @@
C9A50D78180F835200F5B6CE /* AppDelegate.m */,
C9A50D7A180F835200F5B6CE /* Main.storyboard */,
32C95B71198BC18100708AC3 /* ViewController.swift */,
C9A50D9D1810085000F5B6CE /* StringDrawer.h */,
C9A50D9E1810085000F5B6CE /* StringDrawer.m */,
32C95B73198BC1E700708AC3 /* StringDrawer.swift */,
C9A50D80180F835300F5B6CE /* Images.xcassets */,
C9A50D6F180F835200F5B6CE /* Supporting Files */,
Expand Down Expand Up @@ -177,7 +172,6 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
C9A50D9F1810085000F5B6CE /* StringDrawer.m in Sources */,
32C95B74198BC1E700708AC3 /* StringDrawer.swift in Sources */,
C9A50D79180F835200F5B6CE /* AppDelegate.m in Sources */,
32C95B72198BC18100708AC3 /* ViewController.swift in Sources */,
Expand Down
Expand Up @@ -61,11 +61,11 @@ class ViewController : UIViewController {

let mas = NSMutableAttributedString(string:s, attributes:[
NSFontAttributeName:f,
NSParagraphStyleAttributeName:lend({
NSParagraphStyleAttributeName: lend {
(para : NSMutableParagraphStyle) in
para.alignment = align
para.lineBreakMode = brk
})
}
])
mas.addAttribute(NSForegroundColorAttributeName,
value:UIColor.blueColor(),
Expand Down

0 comments on commit ff496c7

Please sign in to comment.