Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion CharacterText.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,9 @@
AF7BB317195C749600E46AF2 /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 0600;
LastSwiftMigration = 0730;
LastSwiftUpdateCheck = 0730;
LastUpgradeCheck = 0730;
ORGANIZATIONNAME = "Swift Yeti";
TargetAttributes = {
AF7BB31E195C749600E46AF2 = {
Expand Down Expand Up @@ -406,6 +408,7 @@
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
COPY_PHASE_STRIP = NO;
ENABLE_STRICT_OBJC_MSGSEND = YES;
ENABLE_TESTABILITY = YES;
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_DYNAMIC_NO_PIC = NO;
GCC_OPTIMIZATION_LEVEL = 0;
Expand Down Expand Up @@ -472,6 +475,7 @@
CLANG_ENABLE_MODULES = YES;
INFOPLIST_FILE = CharacterText/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "SwiftYeti.${PRODUCT_NAME:rfc1034identifier}";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "CharacterText-Bridging-Header.h";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
Expand All @@ -487,6 +491,7 @@
CLANG_ENABLE_MODULES = YES;
INFOPLIST_FILE = CharacterText/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "SwiftYeti.${PRODUCT_NAME:rfc1034identifier}";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "CharacterText-Bridging-Header.h";
};
Expand All @@ -507,6 +512,7 @@
INFOPLIST_FILE = CharacterTextTests/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
METAL_ENABLE_DEBUG_INFO = YES;
PRODUCT_BUNDLE_IDENTIFIER = "SwiftYeti.${PRODUCT_NAME:rfc1034identifier}";
PRODUCT_NAME = "$(TARGET_NAME)";
TEST_HOST = "$(BUNDLE_LOADER)";
};
Expand All @@ -523,6 +529,7 @@
INFOPLIST_FILE = CharacterTextTests/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
METAL_ENABLE_DEBUG_INFO = NO;
PRODUCT_BUNDLE_IDENTIFIER = "SwiftYeti.${PRODUCT_NAME:rfc1034identifier}";
PRODUCT_NAME = "$(TARGET_NAME)";
TEST_HOST = "$(BUNDLE_LOADER)";
};
Expand Down
36 changes: 19 additions & 17 deletions CharacterText/Classes/CharacterLabel/CharacterLabel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ class CharacterLabel: UILabel, NSLayoutManagerDelegate {
}

set {
let wordRange = NSMakeRange(0, count(newValue))
var attributedText = NSMutableAttributedString(string: newValue)
let wordRange = NSMakeRange(0, newValue.characters.count)
let attributedText = NSMutableAttributedString(string: newValue)
attributedText.addAttribute(NSForegroundColorAttributeName , value:self.textColor, range:wordRange)
attributedText.addAttribute(NSFontAttributeName , value:self.font, range:wordRange)

var paragraphStyle = NSMutableParagraphStyle()
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = self.textAlignment
attributedText.addAttribute(NSParagraphStyleAttributeName, value:paragraphStyle, range: wordRange)

Expand All @@ -74,20 +74,20 @@ class CharacterLabel: UILabel, NSLayoutManagerDelegate {

}

override var attributedText: NSAttributedString! {
override var attributedText: NSAttributedString? {
get {
return super.attributedText
}

set {

if textStorage.string == newValue.string {
if textStorage.string == newValue!.string {
return
}

cleanOutOldCharacterTextLayers()
oldCharacterTextLayers = Array<CATextLayer>(characterTextLayers)
textStorage.setAttributedString(newValue)
textStorage.setAttributedString(newValue!)
}

}
Expand All @@ -97,7 +97,7 @@ class CharacterLabel: UILabel, NSLayoutManagerDelegate {
setupLayoutManager()
}

required init(coder aDecoder: NSCoder) {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setupLayoutManager()
}
Expand All @@ -124,21 +124,23 @@ class CharacterLabel: UILabel, NSLayoutManagerDelegate {
characterTextLayers.removeAll(keepCapacity: false)
let attributedText = textStorage.string

let wordRange = NSMakeRange(0, count(attributedText));
let wordRange = NSMakeRange(0, attributedText.characters.count);
let attributedString = self.internalAttributedText();
let layoutRect = layoutManager.usedRectForTextContainer(textContainer);

for var index = wordRange.location; index < wordRange.length+wordRange.location; index += 0 {
var index = wordRange.location

while index < wordRange.length + wordRange.location {
let glyphRange = NSMakeRange(index, 1);
let characterRange = layoutManager.characterRangeForGlyphRange(glyphRange, actualGlyphRange:nil);
let textContainer = layoutManager.textContainerForGlyphAtIndex(index, effectiveRange: nil);
var glyphRect = layoutManager.boundingRectForGlyphRange(glyphRange, inTextContainer: textContainer!);
var location = layoutManager.locationForGlyphAtIndex(index);
var kerningRange = layoutManager.rangeOfNominallySpacedGlyphsContainingIndex(index);
let location = layoutManager.locationForGlyphAtIndex(index);
let kerningRange = layoutManager.rangeOfNominallySpacedGlyphsContainingIndex(index);

if kerningRange.length > 1 && kerningRange.location == index {
if count(characterTextLayers) > 0 {
var previousLayer = characterTextLayers[characterTextLayers.endIndex-1]
if characterTextLayers.count > 0 {
let previousLayer = characterTextLayers[characterTextLayers.endIndex-1]
var frame = previousLayer.frame
frame.size.width += CGRectGetMaxX(glyphRect)-CGRectGetMaxX(frame)
previousLayer.frame = frame
Expand All @@ -149,7 +151,7 @@ class CharacterLabel: UILabel, NSLayoutManagerDelegate {
glyphRect.origin.y += location.y-(glyphRect.height/2)+(self.bounds.size.height/2)-(layoutRect.size.height/2);


var textLayer = CATextLayer(frame: glyphRect, string: attributedString.attributedSubstringFromRange(characterRange));
let textLayer = CATextLayer(frame: glyphRect, string: attributedString.attributedSubstringFromRange(characterRange));
initialTextLayerAttributes(textLayer)

layer.addSublayer(textLayer);
Expand All @@ -164,12 +166,12 @@ class CharacterLabel: UILabel, NSLayoutManagerDelegate {
}

func internalAttributedText() -> NSMutableAttributedString! {
let wordRange = NSMakeRange(0, count(textStorage.string));
var attributedText = NSMutableAttributedString(string: textStorage.string);
let wordRange = NSMakeRange(0, textStorage.string.characters.count);
let attributedText = NSMutableAttributedString(string: textStorage.string);
attributedText.addAttribute(NSForegroundColorAttributeName , value: self.textColor.CGColor, range:wordRange);
attributedText.addAttribute(NSFontAttributeName , value: self.font, range:wordRange);

var paragraphStyle = NSMutableParagraphStyle()
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = self.textAlignment
attributedText.addAttribute(NSParagraphStyleAttributeName, value:paragraphStyle, range: wordRange)

Expand Down
22 changes: 12 additions & 10 deletions CharacterText/Classes/CharacterLabel/CharacterTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class CharacterTextView: UITextView, NSLayoutManagerDelegate {
set {
cleanOutOldCharacterTextLayers();
oldCharacterTextLayers = Array<CALayer>(characterTextLayers);
var newAttributedText = NSMutableAttributedString(attributedString: newValue);
let newAttributedText = NSMutableAttributedString(attributedString: newValue);
newAttributedText.addAttribute(NSForegroundColorAttributeName, value:UIColor.clearColor(), range:NSMakeRange(0, newValue.length));
super.attributedText = newAttributedText;
}
Expand All @@ -46,7 +46,7 @@ class CharacterTextView: UITextView, NSLayoutManagerDelegate {
setupLayoutManager();
}

required init(coder aDecoder: NSCoder) {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setupLayoutManager()
}
Expand All @@ -69,24 +69,26 @@ class CharacterTextView: UITextView, NSLayoutManagerDelegate {
let wordRange = NSMakeRange(0, self.attributedText.length);
let attributedString = self.internalAttributedText();

for var index = wordRange.location; index < wordRange.length+wordRange.location; index += 0 {
var index = wordRange.location

while index < wordRange.length + wordRange.location {
let glyphRange = NSMakeRange(index, 1);
let characterRange = layoutManager.characterRangeForGlyphRange(glyphRange, actualGlyphRange:nil);
let textContainer = layoutManager.textContainerForGlyphAtIndex(index, effectiveRange: nil);
var glyphRect = layoutManager.boundingRectForGlyphRange(glyphRange, inTextContainer: textContainer!);
var location = layoutManager.locationForGlyphAtIndex(index);
var kerningRange = layoutManager.rangeOfNominallySpacedGlyphsContainingIndex(index);
let location = layoutManager.locationForGlyphAtIndex(index);
let kerningRange = layoutManager.rangeOfNominallySpacedGlyphsContainingIndex(index);

if kerningRange.length > 1 && kerningRange.location == index {
var previousLayer = self.characterTextLayers[self.characterTextLayers.endIndex]
let previousLayer = self.characterTextLayers[self.characterTextLayers.endIndex]
var frame = previousLayer.frame
frame.size.width += (CGRectGetMaxX(glyphRect)+location.x)-CGRectGetMaxX(frame)
previousLayer.frame = frame
}


glyphRect.origin.y += location.y-(glyphRect.height/2);
var textLayer = CATextLayer(frame: glyphRect, string: attributedString.attributedSubstringFromRange(characterRange));
let textLayer = CATextLayer(frame: glyphRect, string: attributedString.attributedSubstringFromRange(characterRange));

layer.addSublayer(textLayer);
characterTextLayers.append(textLayer);
Expand All @@ -98,9 +100,9 @@ class CharacterTextView: UITextView, NSLayoutManagerDelegate {

func internalAttributedText() -> NSMutableAttributedString! {
let wordRange = NSMakeRange(0, self.attributedText.length);
var attributedText = NSMutableAttributedString(string: self.text);
attributedText.addAttribute(NSForegroundColorAttributeName , value:self.textColor.CGColor, range:wordRange);
attributedText.addAttribute(NSFontAttributeName , value:self.font, range:wordRange);
let attributedText = NSMutableAttributedString(string: self.text);
attributedText.addAttribute(NSForegroundColorAttributeName , value:self.textColor!.CGColor, range:wordRange);
attributedText.addAttribute(NSFontAttributeName , value:self.font!, range:wordRange);
return attributedText;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class FlickrCollectionViewCell: UICollectionViewCell {
initialSetup()
}

required init(coder aDecoder: NSCoder) {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
initialSetup()
}
Expand Down
8 changes: 4 additions & 4 deletions CharacterText/Classes/Labels/FadingLabel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import CoreText

class FadingLabel: CharacterLabel {

override var attributedText: NSAttributedString! {
override var attributedText: NSAttributedString? {
get {
return super.attributedText
}
Expand All @@ -34,8 +34,8 @@ class FadingLabel: CharacterLabel {

for textLayer in characterTextLayers {

var duration = (NSTimeInterval(arc4random()%100)/200.0)+0.25
var delay = NSTimeInterval(arc4random()%100)/500.0
let duration = (NSTimeInterval(arc4random()%100)/200.0)+0.25
let delay = NSTimeInterval(arc4random()%100)/500.0

CLMLayerAnimation.animation(textLayer, duration:duration, delay:delay, animations: {
textLayer.opacity = 1;
Expand Down Expand Up @@ -70,7 +70,7 @@ class FadingLabel: CharacterLabel {
}
})

++index
index += 1
}
}
}
8 changes: 4 additions & 4 deletions CharacterText/Classes/Labels/FallingLabel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import CoreText

class FallingLabel: CharacterLabel {

override var attributedText: NSAttributedString! {
override var attributedText: NSAttributedString? {
get {
return super.attributedText
}
Expand All @@ -36,8 +36,8 @@ class FallingLabel: CharacterLabel {

for textLayer in characterTextLayers {

var duration = (NSTimeInterval(arc4random()%100)/100.0)+0.3
var delay = NSTimeInterval(arc4random()%100)/500.0
let duration = (NSTimeInterval(arc4random()%100)/100.0)+0.3
let delay = NSTimeInterval(arc4random()%100)/500.0

CLMLayerAnimation.animation(textLayer, duration:duration, delay:delay, animations: {
textLayer.opacity = 1;
Expand Down Expand Up @@ -85,7 +85,7 @@ class FallingLabel: CharacterLabel {
}
})

++index
index += 1
}
}
}
28 changes: 13 additions & 15 deletions CharacterText/Classes/Labels/MotionLabel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,25 @@ class MotionLabel: CharacterLabel {
var oldMatchingCharacters = Dictionary<Int, Int>()
var newMatchingCharacters = Dictionary<Int, Int>()

override var attributedText: NSAttributedString! {
override var attributedText: NSAttributedString? {
get {
return super.attributedText
}

set {

if textStorage.string == newValue.string {
if textStorage.string == newValue!.string {
return
}

super.attributedText = newValue;

let matches = self.matchingCharacterRanges(newValue.string)
let matches = self.matchingCharacterRanges(newValue!.string)
oldMatchingCharacters = matches.oldMatches
newMatchingCharacters = matches.newMatches

self.animateOut(completion: nil);
self.animateIn(completion: nil);
self.animateOut(nil);
self.animateIn(nil);
}

}
Expand All @@ -45,7 +45,7 @@ class MotionLabel: CharacterLabel {
var index = 0
for textLayer in characterTextLayers {

if let oldMatchingIndex = newMatchingCharacters[index] {
if let _ = newMatchingCharacters[index] {
textLayer.opacity = 0;
}else{
let scaleTransform = CATransform3DMakeScale(0.2, 0.2, 1)
Expand All @@ -58,7 +58,7 @@ class MotionLabel: CharacterLabel {

}

++index;
index += 1;
}
}

Expand Down Expand Up @@ -109,7 +109,7 @@ class MotionLabel: CharacterLabel {
})
}

++index;
index += 1;
}
}

Expand All @@ -119,11 +119,9 @@ class MotionLabel: CharacterLabel {

var outerIndex = 0
var innerIndex = 0
var currentIndex = 0
var currentLength = 0
var startingInnerIndex = 0
var buffer = 6
let characterTextLayersEndIndex = count(characterTextLayers)-1
let buffer = 6
let characterTextLayersEndIndex = characterTextLayers.count-1

for characterLayer in oldCharacterTextLayers {
if startingInnerIndex >= characterTextLayersEndIndex {
Expand All @@ -143,11 +141,11 @@ class MotionLabel: CharacterLabel {
startingInnerIndex += innerIndex
break
}
++innerIndex
innerIndex += 1
}
innerIndex = 0
++startingInnerIndex
++outerIndex
startingInnerIndex += 1
outerIndex += 1
}

assert(oldMatches.count == newMatches.count, "Matches dont match", file: "NSStringExtension", line: 46)
Expand Down
Loading