Created
October 15, 2015 19:55
-
-
Save jkopelioff/dd4e072c3f8ce938137d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // AttributtedStrings.swift | |
| // eHow | |
| // | |
| // Created by Joel Kopelioff on 3/5/15. | |
| // Copyright (c) 2015 Demand Media. All rights reserved. | |
| // | |
| import Foundation | |
| extension NSAttributedString | |
| { | |
| convenience init(text:String, style:FontStyle) | |
| { | |
| let boldStringRanges = NSAttributedString.stringRangesWithinTags(text, startTag: "<b>", endTag: "</b>") | |
| let paragraphStyle = NSMutableParagraphStyle() | |
| paragraphStyle.lineHeightMultiple = style.lineHeight | |
| paragraphStyle.paragraphSpacingBefore = 9 | |
| let attributedString = NSMutableAttributedString(string: boldStringRanges.string) | |
| let range = NSMakeRange(0, attributedString.length) | |
| attributedString.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle , range: range) | |
| attributedString.addAttribute(NSFontAttributeName, value: UIFont(name: "\(style.fontName)\(style.fontStyle)", size: style.fontSize)!, range: range) | |
| attributedString.addAttribute(NSForegroundColorAttributeName, value: style.fontColor , range: range) | |
| for subRange in boldStringRanges.ranges { | |
| attributedString.addAttribute(NSFontAttributeName, value: UIFont(name: "\(style.fontName)\(style.fontStyleBold)", size: style.fontSize)!, range: subRange) | |
| } | |
| self.init(attributedString:attributedString) | |
| } | |
| class func stringRangesWithinTags(text:String, startTag:String, endTag:String) -> (string:String, ranges:[NSRange]) { | |
| let scanner = NSScanner(string: text) | |
| scanner.charactersToBeSkipped = nil | |
| var lastLocation = 0 | |
| var snippets:(String, Bool) | |
| var arrayOfSnippets = [(NSString, Bool)]() | |
| while !scanner.atEnd { | |
| var tagText : NSString? | |
| var beforeText : NSString? | |
| scanner.scanUpToString(startTag, intoString:&beforeText) | |
| scanner.scanString(startTag, intoString:nil) | |
| if beforeText != nil { | |
| arrayOfSnippets.append((beforeText!, false)) | |
| } | |
| if !scanner.scanUpToString(endTag, intoString: &tagText) { | |
| break; | |
| } | |
| scanner.scanString(endTag, intoString:nil) | |
| if tagText != nil { | |
| arrayOfSnippets.append((tagText!, true)) | |
| } | |
| lastLocation = scanner.scanLocation | |
| } | |
| // let lastText = text.substringFromIndex(advance(text.startIndex, lastLocation)) | |
| // if !lastText.isEmpty { | |
| // arrayOfSnippets.append((lastText, false)) | |
| // } | |
| // | |
| var currentIndex = 0 | |
| var newString:String = "" | |
| var ranges:[NSRange] = [] | |
| for snippet in arrayOfSnippets { | |
| if snippet.1 { | |
| ranges.append(NSMakeRange(currentIndex, snippet.0.length)) | |
| } | |
| newString += snippet.0 as! String | |
| currentIndex = count(newString) | |
| } | |
| return (string:newString, ranges:ranges) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment