Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
touyu committed May 14, 2017
1 parent 296d848 commit 3b93cda
Show file tree
Hide file tree
Showing 18 changed files with 469 additions and 47 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
68 changes: 68 additions & 0 deletions Sources/Attribute.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//
// Attribute.swift
// SwiftyAttributedString
//
// Created by Yuto Akiba on 2017/05/14.
// Copyright © 2017年 Yuto Akiba. All rights reserved.
//

import UIKit

public enum AttributeValue {
case font(UIFont)
case foregroundColor(UIColor)
case backgroundColor(UIColor)
case kern(NSNumber)
case strikethroughStyle(NSNumber)
case underlineStyle(NSNumber)
case strokeColor(UIColor)
case strokeWidth(NSNumber)
case shadow(NSShadow)
case textEffect(TextEffect)
case link(URL)
case baselineOffset(NSNumber)
case obliqueness(NSNumber)
case expansion(NSNumber)
}

public enum AttributeRange {
case all
case portion(of: Portion)

enum Portion {
case string(String)
case range(NSRange)
}

func toNSRange(string: String) -> NSRange {

switch self {
case .all:
let titleRange = (string as NSString).range(of: string)
return titleRange
case .portion(let portion):

switch portion {
case .string(let portionString):
let titleRange = (string as NSString).range(of: portionString)
return titleRange
case .range(let range):
return range
}
}
}
}

public struct Attribute {
var value: AttributeValue
var range: AttributeRange

init(value: AttributeValue, range: AttributeRange = .all) {
self.value = value
self.range = range
}
}

public enum TextEffect: String {
case letterPressStyle = "_UIKitNewLetterpressStyle"
}
53 changes: 53 additions & 0 deletions Sources/NSMutableAttributedString+AttributeExtension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//
// NSMutableAttributedString+AttributeExtension.swift
// SwiftyAttributedString
//
// Created by Yuto Akiba on 2017/05/14.
// Copyright © 2017年 Yuto Akiba. All rights reserved.
//

import UIKit

public extension NSMutableAttributedString {
func add(attribute: Attribute) -> NSMutableAttributedString {
let text = self.string
let range = attribute.range.toNSRange(string: text)

switch attribute.value {
case .font(let font):
self.addAttribute(NSFontAttributeName, value: font, range: range)
case .foregroundColor(let color):
self.addAttribute(NSForegroundColorAttributeName, value: color, range: range)
case .backgroundColor(let color):
self.addAttribute(NSBackgroundColorAttributeName, value: color, range: range)
case .kern(let nsNumber):
self.addAttribute(NSKernAttributeName, value: nsNumber, range: range)
case .strikethroughStyle(let nsNumber):
self.addAttribute(NSStrikethroughStyleAttributeName, value: nsNumber, range: range)
case .underlineStyle(let nsNumber):
self.addAttribute(NSUnderlineStyleAttributeName, value: nsNumber, range: range)
case .strokeColor(let color):
self.addAttribute(NSStrokeColorAttributeName, value: color, range: range)
case .strokeWidth(let nsNumber):
self.addAttribute(NSStrokeWidthAttributeName, value: nsNumber, range: range)
case .shadow(let shadow):
self.addAttribute(NSShadowAttributeName, value: shadow, range: range)
case .textEffect(let textEffect):
self.addAttribute(NSTextEffectAttributeName, value: textEffect.rawValue, range: range)
case .link(let url):
self.addAttribute(NSLinkAttributeName, value: url, range: range)
case .baselineOffset(let nsNumber):
self.addAttribute(NSBaselineOffsetAttributeName, value: nsNumber, range: range)
case .obliqueness(let nsNumber):
self.addAttribute(NSObliquenessAttributeName, value: nsNumber, range: range)
case .expansion(let nsNumber):
self.addAttribute(NSExpansionAttributeName, value: nsNumber, range: range)
}

return self
}

func add(attributeValue: AttributeValue) -> NSMutableAttributedString {
return self.add(attribute: Attribute(value: attributeValue, range: .all))
}
}
35 changes: 35 additions & 0 deletions Sources/String+AttributeExtension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// NSAttributedStringHelper.swift
// SwiftyAttributedString
//
// Created by Yuto Akiba on 2017/05/14.
// Copyright © 2017年 Yuto Akiba. All rights reserved.
//

import UIKit

public extension String {

func findRange(_ string: String?) -> NSRange {
guard let string = string else { return NSRange() }
let titleRange = (self as NSString).range(of: string)
return titleRange
}

func add(attribute: Attribute) -> NSMutableAttributedString {
let attributedString = NSMutableAttributedString(string: self)
return attributedString.add(attribute: attribute)
}

func add(attributeValue: AttributeValue) -> NSMutableAttributedString {
return self.add(attribute: Attribute(value: attributeValue, range: .all))
}

func add(attributes: [Attribute]) -> NSMutableAttributedString {
var attributedString = NSMutableAttributedString(string: self)
for attribute in attributes {
attributedString = attributedString.add(attribute: attribute)
}
return attributedString
}
}
Loading

0 comments on commit 3b93cda

Please sign in to comment.