Skip to content

Commit

Permalink
Fix #74
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel Sharanda committed Apr 7, 2019
1 parent eaf3af6 commit 07ffaae
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 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.7.2"
s.version = "4.7.3"
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
6 changes: 6 additions & 0 deletions Atributika.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
1B2F839F1E5C98F0009AFBCA /* Atributika.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 52D6D97C1BEFF229002C0205 /* Atributika.framework */; };
1B2F83A01E5C98F0009AFBCA /* Atributika.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 52D6D97C1BEFF229002C0205 /* Atributika.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
1B3836A01F97C224001DB333 /* AttributedLabel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1B38369F1F97C224001DB333 /* AttributedLabel.swift */; };
1B4638582259FFE400ED5BA9 /* AttributedLabel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1B38369F1F97C224001DB333 /* AttributedLabel.swift */; };
1B4638592259FFE400ED5BA9 /* AttributedLabel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1B38369F1F97C224001DB333 /* AttributedLabel.swift */; };
1B46385A2259FFE400ED5BA9 /* AttributedLabel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1B38369F1F97C224001DB333 /* AttributedLabel.swift */; };
1BB55AD11E5C73EF00CE6EAD /* AttributedText.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1BB55ACC1E5C73EF00CE6EAD /* AttributedText.swift */; };
1BB55AD21E5C73EF00CE6EAD /* AttributedText.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1BB55ACC1E5C73EF00CE6EAD /* AttributedText.swift */; };
1BB55AD31E5C73F000CE6EAD /* AttributedText.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1BB55ACC1E5C73EF00CE6EAD /* AttributedText.swift */; };
Expand Down Expand Up @@ -627,6 +630,7 @@
1BB55AD31E5C73F000CE6EAD /* AttributedText.swift in Sources */,
1B2E55DB219321FB00D2B934 /* HTMLSpecials.swift in Sources */,
1BB55ADF1E5C73F000CE6EAD /* String+Detection.swift in Sources */,
1B4638592259FFE400ED5BA9 /* AttributedLabel.swift in Sources */,
1BB55AD71E5C73F000CE6EAD /* NSAttributedString+Utils.swift in Sources */,
1BB55AE31E5C73F000CE6EAD /* Style.swift in Sources */,
);
Expand All @@ -640,6 +644,7 @@
1BB55AD41E5C73F000CE6EAD /* AttributedText.swift in Sources */,
1B2E55DC219321FB00D2B934 /* HTMLSpecials.swift in Sources */,
1BB55AE01E5C73F000CE6EAD /* String+Detection.swift in Sources */,
1B46385A2259FFE400ED5BA9 /* AttributedLabel.swift in Sources */,
1BB55AD81E5C73F000CE6EAD /* NSAttributedString+Utils.swift in Sources */,
1BB55AE41E5C73F000CE6EAD /* Style.swift in Sources */,
);
Expand All @@ -653,6 +658,7 @@
1BB55AD21E5C73EF00CE6EAD /* AttributedText.swift in Sources */,
1B2E55DA219321FB00D2B934 /* HTMLSpecials.swift in Sources */,
1BB55ADE1E5C73F000CE6EAD /* String+Detection.swift in Sources */,
1B4638582259FFE400ED5BA9 /* AttributedLabel.swift in Sources */,
1BB55AD61E5C73F000CE6EAD /* NSAttributedString+Utils.swift in Sources */,
1BB55AE21E5C73F000CE6EAD /* Style.swift in Sources */,
);
Expand Down
1 change: 1 addition & 0 deletions Demo/AttributedLabelDemoViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class AttributedLabelDemoViewController: UIViewController {

private var tweets: [String] = [
"@e2F If only Bradley's arm was longer. Best photo ever. 😊 #oscars https://pic.twitter.com/C9U5NOtGap<br>Check this <a href=\"https://github.com/psharanda/Atributika\">link</a>",
"@e2F If only Bradley's arm was longer. Best photo ever. 😊 #oscars https://pic.twitter.com/C9U5NOtGap<br>Check this <a href=\"https://github.com/psharanda/Atributika\">link that won't detect click here</a>",
"For every retweet this gets, Pedigree will donate one bowl of dog food to dogs in need! 😊 #tweetforbowls",
"All the love as always. H",
"We got kicked out of a @Delta airplane because I spoke Arabic to my mom on the phone and with my friend slim... WTFFFFFFFF please spread",
Expand Down
29 changes: 27 additions & 2 deletions Sources/AttributedLabel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ open class AttributedLabel: UIView {

open var attributedText: AttributedText? {
set {
state.attributedTextAndString = newValue.map { ($0, $0.attributedString) }
state = State(attributedTextAndString: newValue.map { ($0, $0.attributedString) }, isEnabled: state.isEnabled, detection: nil)
setNeedsLayout()
}
get {
Expand Down Expand Up @@ -245,7 +245,16 @@ extension NSAttributedString {
paragraphStyle.alignment = textAlignment

let inheritedAttributes = [AttributedStringKey.font: font as Any, AttributedStringKey.paragraphStyle: paragraphStyle as Any]
let result = NSMutableAttributedString(string: string, attributes: inheritedAttributes)

var s = string

//since iOS 11, UILabel line break logic was changed. It tries to avoid orphaned words on last line. To mimic this we can replace last space with non-breaking space
let version = OperatingSystemVersion(majorVersion: 11, minorVersion: 0, patchVersion: 0)
if ProcessInfo.processInfo.isOperatingSystemAtLeast(version) {
s = s.replacingLastOccurrenceOfString(" ", with: "\u{a0}")
}

let result = NSMutableAttributedString(string: s, attributes: inheritedAttributes)

result.beginEditing()
enumerateAttributes(in: NSMakeRange(0, length), options: .longestEffectiveRangeNotRequired, using: { (attributes, range, _) in
Expand All @@ -257,4 +266,20 @@ extension NSAttributedString {
}
}

extension String
{
func replacingLastOccurrenceOfString(_ searchString: String,
with replacementString: String) -> String
{
if let range = range(of: searchString,
options: [.backwards],
range: nil,
locale: nil) {

return replacingCharacters(in: range, with: replacementString)
}
return self
}
}

#endif

0 comments on commit 07ffaae

Please sign in to comment.