Skip to content

Commit

Permalink
Merge pull request #23 from JNDisrupter/development
Browse files Browse the repository at this point in the history
Fixing Bug in SeSmartText
  • Loading branch information
jayelzaghmoutt authored Jul 29, 2024
2 parents 309c3cb + 1c0453b commit cd12609
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "607FACCF1AFB9204008FA782"
BuildableName = "JNMentionTextView_Example.app"
BlueprintName = "JNMentionTextView_Example"
ReferencedContainer = "container:JNMentionTextView.xcodeproj">
</BuildableReference>
</MacroExpansion>
<Testables>
<TestableReference
skipped = "NO">
Expand All @@ -53,17 +62,6 @@
</BuildableReference>
</TestableReference>
</Testables>
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "607FACCF1AFB9204008FA782"
BuildableName = "JNMentionTextView_Example.app"
BlueprintName = "JNMentionTextView_Example"
ReferencedContainer = "container:JNMentionTextView.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
Expand All @@ -85,8 +83,6 @@
ReferencedContainer = "container:JNMentionTextView.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
Expand Down
2 changes: 1 addition & 1 deletion JNMentionTextView.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|

s.name = "JNMentionTextView"
s.version = "1.0.3"
s.version = "1.0.4"
s.summary = "JNMentionTextView is a UITextView replacement supporting the mention feature for iOS applications."
s.description = "A UITextView drop-in replacement supporting special characters such as [ #, @ ] and regex patterns, written in Swift."
s.homepage = "https://github.com/JNDisrupter"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ extension JNMentionTextView {

switch self.options.viewPositionMode {
case .up:
popoverPresentationController?.permittedArrowDirections = .up
case .down:
popoverPresentationController?.permittedArrowDirections = .down
case .down:
popoverPresentationController?.permittedArrowDirections = .up
default:
popoverPresentationController?.permittedArrowDirections = [.up, .down]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,7 @@ extension JNMentionTextView {
- Parameter text: smart text which m.
- Parameter selectedLocation: selected location.
*/
open func setSmartText(_ text: String) {

/// Difference
var difference = 0
public func setSmartText(_ text: String) {

/// Attributed String
let attributedString = NSMutableAttributedString(string: text, attributes: self.normalAttributes)
Expand All @@ -82,44 +79,37 @@ extension JNMentionTextView {
// build pattern
let updatedPattern = "(\\" + pattern + "([A-Za-z0-9]{0,}))\\s"

// reset difference
difference = 0

do {


// build regex
let regex = try NSRegularExpression(pattern: updatedPattern)
regex.enumerateMatches(in: attributedString.string, range: NSRange(location: 0, length: attributedString.length)) {
match, flags, stop in

// get matches
let matches = regex.matches(in: attributedString.string, options: [], range: NSRange(location: 0, length: attributedString.length))

// loop in matches in revers
for match in matches.reversed() {

let matchRange = match.range(at: 1)

if let matchRange = match?.range(at: 1) {
// get mention ID
let searchID = String((attributedString.string as NSString).substring(with: matchRange).dropFirst())

// get mention item for ID
if let item = self.mentionDelegate?.jnMentionTextView(getMentionItemFor: pattern, id: searchID) {

// update range
var updatedRange = matchRange
updatedRange.location += difference
// create mention entity
let mentionItem = JNMentionEntity(item: item, symbol: pattern)

// get mention ID
let searchID = String((attributedString.string as NSString).substring(with: updatedRange).dropFirst())
// update attribute string by adding mention item
var updatedAttributes = attributes
updatedAttributes[JNMentionTextView.JNMentionAttributeName] = mentionItem

// get mention item for ID
if let item = self.mentionDelegate?.jnMentionTextView(getMentionItemFor: pattern, id: searchID) {

// create mention entity
let mentionItem = JNMentionEntity(item: item, symbol: pattern)

// update attribute string by adding mention item
var updatedAttributes = attributes
updatedAttributes[JNMentionTextView.JNMentionAttributeName] = mentionItem

// create mention attributed string
let mentionAttributedString = NSMutableAttributedString(attributedString: NSAttributedString(string: item.getPickableTitle(),
attributes: updatedAttributes))
// replace the matched pattern with the mention attributed string
attributedString.replaceCharacters(in: updatedRange, with: mentionAttributedString)

// check if difference is less than.
difference += item.getPickableTitle().count - matchRange.length

}
// create mention attributed string
let mentionAttributedString = NSMutableAttributedString(attributedString: NSAttributedString(string: item.getPickableTitle(),
attributes: updatedAttributes))
// replace the matched pattern with the mention attributed string
attributedString.replaceCharacters(in: matchRange, with: mentionAttributedString)
}
}
}
Expand All @@ -142,14 +132,11 @@ extension JNMentionTextView {
- Parameter mentionReplacements: Mention Replacement.
- Returns smartReplacement: smart replacement attributed string.
*/
open class func getSmartReplacement(text: String, data: [String: [JNMentionPickable]], normalAttributes: [NSAttributedString.Key: Any], mentionReplacements: [String: [NSAttributedString.Key : Any]]) -> NSAttributedString {
public class func getSmartReplacement(text: String, data: [String: [JNMentionPickable]], normalAttributes: [NSAttributedString.Key: Any], mentionReplacements: [String: [NSAttributedString.Key : Any]]) -> NSAttributedString {

// add space to text
let text = text + " "

/// Difference
var difference = 0

/// Attributed String
let attributedString = NSMutableAttributedString(string: text, attributes: normalAttributes)

Expand All @@ -159,44 +146,37 @@ extension JNMentionTextView {
// build pattern
let updatedPattern = "(\\" + pattern + "([A-Za-z0-9]{0,}))\\s"

// reset difference
difference = 0

do {

// build regex
let regex = try NSRegularExpression(pattern: updatedPattern)
regex.enumerateMatches(in: attributedString.string, range: NSRange(location: 0, length: attributedString.length)) {
match, flags, stop in

// get matches
let matches = regex.matches(in: attributedString.string, options: [], range: NSRange(location: 0, length: attributedString.length))

// loop in matches in revers
for match in matches.reversed() {

let matchRange = match.range(at: 1)

// get mention ID
let searchID = String((attributedString.string as NSString).substring(with: matchRange).dropFirst())

if let matchRange = match?.range(at: 1) {
// get mention item for ID
if let item = data[pattern]?.first(where: { $0.getPickableIdentifier() == searchID}) {

// update range
var updatedRange = matchRange
updatedRange.location += difference
// create mention entity
let mentionItem = JNMentionEntity(item: item , symbol: pattern)

// get mention ID
let searchID = String((attributedString.string as NSString).substring(with: updatedRange).dropFirst())
// update attribute string by adding mention item
var updatedAttributes = attributes
updatedAttributes[JNMentionTextView.JNMentionAttributeName] = mentionItem

// get mention item for ID
if let item = data[pattern]?.first(where: { $0.getPickableIdentifier() == searchID}) {

// create mention entity
let mentionItem = JNMentionEntity(item: item , symbol: pattern)

// update attribute string by adding mention item
var updatedAttributes = attributes
updatedAttributes[JNMentionTextView.JNMentionAttributeName] = mentionItem

// create mention attributed string
let mentionAttributedString = NSMutableAttributedString(attributedString: NSAttributedString(string: item.getPickableTitle(),
attributes: updatedAttributes))
// replace the matched pattern with the mention attributed string
attributedString.replaceCharacters(in: updatedRange, with: mentionAttributedString)

// check if difference is less than.
difference += item.getPickableTitle().count - matchRange.length

}
// create mention attributed string
let mentionAttributedString = NSMutableAttributedString(attributedString: NSAttributedString(string: item.getPickableTitle(),
attributes: updatedAttributes))
// replace the matched pattern with the mention attributed string
attributedString.replaceCharacters(in: matchRange, with: mentionAttributedString)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ public struct JNMentionPickerViewPositionwMode: OptionSet {
}

/// Picker View Positionw Mode Up
public static let up = JNMentionPickerViewPositionwMode(rawValue: 0)
public static let up = JNMentionPickerViewPositionwMode(rawValue: 1 << 0)

/// Picker View Positionw Mode Down
public static let down = JNMentionPickerViewPositionwMode(rawValue: 1)
public static let down = JNMentionPickerViewPositionwMode(rawValue: 1 << 1)

/// Picker View Positionw Mode automatic
public static let automatic = JNMentionPickerViewPositionwMode(rawValue: 2)
public static let automatic = JNMentionPickerViewPositionwMode(rawValue: 1 << 2)
}

/// JNMention Picker View Options
Expand Down

0 comments on commit cd12609

Please sign in to comment.