Skip to content

Commit

Permalink
Fixed Text Alignment
Browse files Browse the repository at this point in the history
  • Loading branch information
Mathias Koehnke committed Oct 5, 2015
1 parent 469b55f commit fdb82eb
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
24 changes: 23 additions & 1 deletion AnimatedBlurLabel/AnimatedBlurLabel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,20 @@ class AnimatedBlurLabel : UILabel {
}
}

private func hasAlignment(alignment: NSTextAlignment) -> Bool {
var hasAlignment = false
if let text = attributedTextToRender {
text.enumerateAttribute(NSParagraphStyleAttributeName, inRange: NSMakeRange(0, text.length), options: [], usingBlock: { value, _ , stop in
let paragraphStyle = value as? NSParagraphStyle
hasAlignment = paragraphStyle?.alignment == alignment
stop.memory = true
})
} else if let _ = textToRender {
hasAlignment = textAlignment == alignment
}
return hasAlignment
}

private func deferBlur(blurred: Bool, animated: Bool, completion: ((finished : Bool) -> Void)?) {
print("Defer blurring ...")
blurredParameter = blurred
Expand Down Expand Up @@ -308,10 +322,18 @@ class AnimatedBlurLabel : UILabel {
CATransaction.setDisableActions(true)
blurLayer1.frame = bounds
blurLayer2.frame = bounds

if let renderedTextImage = renderedTextImage where hasAlignment(.Center) == false {
var newX = (bounds.size.width - renderedTextImage.size.width) / 2
newX = hasAlignment(.Right) ? newX : (newX * -1)
blurLayer1.frame = CGRectOffset(blurLayer1.frame, newX, 0)
blurLayer2.frame = CGRectOffset(blurLayer2.frame, newX, 0)
}

CATransaction.setDisableActions(false)
CATransaction.commit()
}

private func applyBlurEffect(image: UIImage, blurLevel: Double) -> UIImage {
let resultImage : CIImage!
if blurLevel > 0 {
Expand Down
1 change: 0 additions & 1 deletion Example/AnimatedBlurLabelDemo/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
<constraint firstItem="Nrj-rR-itJ" firstAttribute="leading" secondItem="8bC-Xf-vdC" secondAttribute="leading" id="7SO-xA-ZWY"/>
<constraint firstItem="Nrj-rR-itJ" firstAttribute="centerY" secondItem="8bC-Xf-vdC" secondAttribute="centerY" id="Beg-my-Ynp"/>
<constraint firstAttribute="trailing" secondItem="Nrj-rR-itJ" secondAttribute="trailing" id="Dtq-O3-seq"/>
<constraint firstItem="Nrj-rR-itJ" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="qFu-Em-cME"/>
</constraints>
</view>
<connections>
Expand Down
4 changes: 2 additions & 2 deletions Example/AnimatedBlurLabelDemo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

blurLabel.animationDuration = 1.0
}

override func viewDidLayoutSubviews() {
Expand All @@ -27,7 +27,7 @@ class ViewController: UIViewController {
let attributes = [NSParagraphStyleAttributeName : paragraph, NSForegroundColorAttributeName : UIColor.whiteColor()]

let string = NSMutableAttributedString(string: "Title\n\nSubtitle", attributes: attributes)
string.addAttribute(NSFontAttributeName, value: UIFont.boldSystemFontOfSize(32.0), range: NSMakeRange(0, 5))
string.addAttribute(NSFontAttributeName, value: UIFont.boldSystemFontOfSize(48.0), range: NSMakeRange(0, 5))
string.addAttribute(NSFontAttributeName, value: UIFont.systemFontOfSize(14.0), range: NSMakeRange(7, 7))
blurLabel.attributedText = string
}
Expand Down

0 comments on commit fdb82eb

Please sign in to comment.