Skip to content

Commit

Permalink
- Color.link is now Color.defaultLink
Browse files Browse the repository at this point in the history
- UIColor.link is now UIColor.defaultLink
- CLTextEditor & CLRichText cleaning and improving
  • Loading branch information
juliangerhards committed Jun 8, 2022
1 parent 45d3ac3 commit 9f69082
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 30 deletions.
2 changes: 1 addition & 1 deletion Sources/CleanUI/Extensions/SwiftUI/Color.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public extension Color {
ColorProvider.color("GrayText")
}

static var link: Color {
static var defaultLink: Color {
ColorProvider.color("Link")
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/CleanUI/Extensions/UIKit/UIColor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public extension UIColor {
ColorProvider.uiColor("GrayText")
}

static var link: UIColor {
static var defaultLink: UIColor {
ColorProvider.uiColor("Link")
}

Expand Down
24 changes: 12 additions & 12 deletions Sources/CleanUI/Views/TextEditors/CLTextEditor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public struct CLTextEditor: View {
.allowsHitTesting(false)
.frame(minHeight: minHeight)

if(text.isEmpty){
if text.isEmpty {
Text(placeholder)
.foregroundColor(.gray)
.opacity(0.6)
Expand All @@ -70,15 +70,15 @@ public struct CLTextEditor: View {
.overlay(
UTextViewOverlay(text: $text, font: .callout, maxLayoutWidth: UIScreen.main.bounds.size.width, textViewStore: textViewStore, keyboardType: keyboardType, attributes: attributes)
)
.onChange(of: text, perform: { value in
if(characterLimit != 0){
if(value.count > characterLimit){
.onChange(of: text) { value in
if characterLimit != 0 {
if value.count > characterLimit {
DispatchQueue.main.async {
text = String(text.dropLast(value.count - characterLimit))
}
}
}
})
}
}
}

Expand Down Expand Up @@ -127,19 +127,19 @@ struct UTextViewOverlay: UIViewRepresentable {
let links = text.getLinks()

for (_, range) in links {
attributedText.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.link, range: range)
attributedText.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.defaultLink, range: range)
}
case .hashtags(_):
let mentions = text.getMentions()
let hashtags = text.getHashtags()

for (_, range) in mentions {
attributedText.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.link, range: range)
for (_, range) in hashtags {
attributedText.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.defaultLink, range: range)
}
case .mentions(_):
let hashtags = text.getHashtags()
let mentions = text.getMentions()

for (_, range) in hashtags {
attributedText.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.link, range: range)
for (_, range) in mentions {
attributedText.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.defaultLink, range: range)
}
}
}
Expand Down
14 changes: 12 additions & 2 deletions Sources/CleanUI/Views/Texts/CLExpandableText.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,17 @@ public struct CLExpandableText: View {

struct CLExpandableText_Previews: PreviewProvider {
static var previews: some View {
CLExpandableText("Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.")
.padding()
CLExpandableText("Hallo #knogglHashtag www.knoggl.com @knogglMention", attributes: [
.links { linkString in
CUAlertMessage.show("Link: " + linkString)
},
.hashtags { hashtagString in
CUAlertMessage.show("Hashtag: " + hashtagString)
},
.mentions { mentionString in
CUAlertMessage.show("Mention: " + mentionString)
},
])
.padding()
}
}
46 changes: 32 additions & 14 deletions Sources/CleanUI/Views/Texts/CLRichText.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public struct CLRichText: View {

internal extension CLRichText {
struct TextViewOverlay: UIViewRepresentable {

var string: String
var font: Font
var maxLayoutWidth: CGFloat
Expand Down Expand Up @@ -107,25 +108,25 @@ internal extension CLRichText {
for (foundedLink, range) in links {
var multipleAttributes = [NSAttributedString.Key : Any]()
multipleAttributes[NSAttributedString.Key.customLink] = foundedLink
multipleAttributes[NSAttributedString.Key.foregroundColor] = UIColor.link
multipleAttributes[NSAttributedString.Key.foregroundColor] = UIColor.defaultLink
attributedText.addAttributes(multipleAttributes, range: range)
}
case .hashtags(_):
let mentions = string.getMentions()
let hashtags = string.getHashtags()

for (foundedMention, range) in mentions {
for (foundedHashtag, range) in hashtags {
var multipleAttributes = [NSAttributedString.Key : Any]()
multipleAttributes[NSAttributedString.Key.mention] = foundedMention.dropFirst()
multipleAttributes[NSAttributedString.Key.foregroundColor] = UIColor.link
multipleAttributes[NSAttributedString.Key.hashtag] = foundedHashtag.dropFirst()
multipleAttributes[NSAttributedString.Key.foregroundColor] = UIColor.defaultLink
attributedText.addAttributes(multipleAttributes, range: range)
}
case .mentions(_):
let hashtags = string.getHashtags()
let mentions = string.getMentions()

for (foundedHashtag, range) in hashtags {
for (foundedMention, range) in mentions {
var multipleAttributes = [NSAttributedString.Key : Any]()
multipleAttributes[NSAttributedString.Key.hashtag] = foundedHashtag.dropFirst()
multipleAttributes[NSAttributedString.Key.foregroundColor] = UIColor.link
multipleAttributes[NSAttributedString.Key.mention] = foundedMention.dropFirst()
multipleAttributes[NSAttributedString.Key.foregroundColor] = UIColor.defaultLink
attributedText.addAttributes(multipleAttributes, range: range)
}
}
Expand Down Expand Up @@ -156,8 +157,8 @@ internal extension CLRichText {

// location of tap in textView coordinates and taking the inset into account
var location = sender.location(in: textView)
location.x -= textView.textContainerInset.left;
location.y -= textView.textContainerInset.top;
location.x -= textView.textContainerInset.left
location.y -= textView.textContainerInset.top

// character index at tap location
let characterIndex = layoutManager.characterIndex(for: location, in: textView.textContainer, fractionOfDistanceBetweenInsertionPoints: nil)
Expand All @@ -173,7 +174,7 @@ internal extension CLRichText {
switch attribute {
case .links(let action):
if let action = action {
action(linkValue as! String)
action(linkValue as? String ?? "")
}
default:
break
Expand All @@ -189,7 +190,7 @@ internal extension CLRichText {
switch attribute {
case .mentions(let action):
if let action = action {
action(mentionValue as! String)
action(mentionValue as? String ?? "")
}
default:
break
Expand All @@ -205,7 +206,7 @@ internal extension CLRichText {
switch attribute {
case .hashtags(let action):
if let action = action {
action(hashtagValue as! String)
action(hashtagValue as? String ?? "")
}
default:
break
Expand All @@ -218,3 +219,20 @@ internal extension CLRichText {
}
}
}

struct CLRichText_Previews: PreviewProvider {
static var previews: some View {
CLRichText("Hallo #knogglHashtag www.knoggl.com @knogglMention", attributes: [
.links { linkString in
CUAlertMessage.show("Link: " + linkString)
},
.hashtags { hashtagString in
CUAlertMessage.show("Hashtag: " + hashtagString)
},
.mentions { mentionString in
CUAlertMessage.show("Mention: " + mentionString)
},
])
.padding()
}
}

0 comments on commit 9f69082

Please sign in to comment.