Skip to content

Commit

Permalink
Add possibility to precisely configure style depending on tag and its…
Browse files Browse the repository at this point in the history
… attributes
  • Loading branch information
psharanda committed Aug 7, 2018
1 parent c0d2c36 commit f9bc6ed
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Atributika.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "Atributika"
s.version = "4.5.0"
s.version = "4.6.0"
s.summary = "Convert text with HTML tags, hashtags, mentions, links into NSAttributedString. Make them clickable with UILabel drop-in replacement."
s.description = <<-DESC
`Atributika` is an easy and painless way to build NSAttributedString. It is able to detect HTML-like tags, links, phone numbers, hashtags, any regex or even standard ios data detectors and style them with various attributes like font, color, etc. `Atributika` comes with drop-in label replacement `AttributedLabel` which is able to make any detection clickable.
Expand Down
2 changes: 2 additions & 0 deletions Atributika.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,7 @@
CLANG_ANALYZER_NONNULL = YES;
CLANG_ENABLE_CODE_COVERAGE = NO;
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
DEVELOPMENT_TEAM = "";
INFOPLIST_FILE = Demo/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
Expand All @@ -715,6 +716,7 @@
CLANG_ANALYZER_NONNULL = YES;
CLANG_ENABLE_CODE_COVERAGE = NO;
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
DEVELOPMENT_TEAM = "";
INFOPLIST_FILE = Demo/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
Expand Down
8 changes: 4 additions & 4 deletions Sources/AttributedText.swift
Original file line number Diff line number Diff line change
Expand Up @@ -162,15 +162,15 @@ extension String: AttributedTextProtocol {
return Style()
}

public func style(tags: [Style], transformers: [TagTransformer] = [TagTransformer.brTransformer]) -> AttributedText {
public func style(tags: [Style], transformers: [TagTransformer] = [TagTransformer.brTransformer], tuner: (Style, Tag) -> Style = { s, _ in return s}) -> AttributedText {
let (string, tagsInfo) = detectTags(transformers: transformers)

var ds: [Detection] = []

tagsInfo.forEach { t in

if let style = (tags.first { style in style.name.lowercased() == t.tag.name.lowercased() }) {
ds.append(Detection(type: .tag(t.tag), style: style, range: t.range))
ds.append(Detection(type: .tag(t.tag), style: tuner(style, t.tag), range: t.range))
} else {
ds.append(Detection(type: .tag(t.tag), style: Style(), range: t.range))
}
Expand All @@ -179,8 +179,8 @@ extension String: AttributedTextProtocol {
return AttributedText(string: string, detections: ds, baseStyle: baseStyle)
}

public func style(tags: Style..., transformers: [TagTransformer] = [TagTransformer.brTransformer]) -> AttributedText {
return style(tags: tags, transformers: transformers)
public func style(tags: Style..., transformers: [TagTransformer] = [TagTransformer.brTransformer], tuner: (Style, Tag) -> Style = { s, _ in return s}) -> AttributedText {
return style(tags: tags, transformers: transformers, tuner: tuner)
}
}

Expand Down
42 changes: 42 additions & 0 deletions Tests/AtributikaTests/AtributikaTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -482,9 +482,51 @@ class AtributikaTests: XCTestCase {

XCTAssertEqual(test, reference)
}

func testTuner() {

func hexStringToUIColor (hex:String) -> UIColor {
var cString:String = hex.trimmingCharacters(in: .whitespacesAndNewlines).uppercased()

if (cString.hasPrefix("#")) {
cString.remove(at: cString.startIndex)
}

if ((cString.count) != 6) {
return UIColor.gray
}

var rgbValue:UInt32 = 0
Scanner(string: cString).scanHexInt32(&rgbValue)

return UIColor(
red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
alpha: CGFloat(1.0)
)
}

let font = Style("font")

let test = "Monday - Friday:<font color=\"#6cc299\"> 8:00 - 19:00</font>".style(tags: font, tuner: { style, tag in
if tag.name == font.name {
if let colorString = tag.attributes["color"] {
return style.foregroundColor(hexStringToUIColor(hex: colorString))
}
}
return style
}).attributedString

let reference = NSMutableAttributedString(string: "Monday - Friday: 8:00 - 19:00")
reference.addAttributes([NSAttributedStringKey.foregroundColor: hexStringToUIColor(hex: "#6cc299")], range: NSMakeRange(16, 13))
XCTAssertEqual(test, reference)
}
}




#if os(Linux)
extension AtributikaTests {
static var allTests : [(String, (AtributikaTests) -> () throws -> Void)] {
Expand Down

0 comments on commit f9bc6ed

Please sign in to comment.