Gibt es eine Möglichkeit, einen Textabsatz auf Wischgeste zu ändern? Ich möchte den Absatz mit einem anderen vordefinierten Absatz ändern, wenn der Benutzer einen Absatz wischt.IOS 10 xcode 8 swift 3, UITextView Text beim Swipe ändern
Ich bin fest, um den Absatz zu erkennen, können wir versteckte Tags darauf oder etwas anderes einfügen, um Absatz auf Gesten zu erkennen?
Ich kann den Index des Zeichens erhalten, aber wie kann ich das Absatzobjekt oder einen Weg zu bekommen?
override func viewDidLoad()
{
super.viewDidLoad()
// set delegate for text view
self.textView.delegate = self
// set the swipeGesture to the UITextView
let swipe = UISwipeGestureRecognizer(target: self, action: #selector(self.swipeOnce(_:)))
swipe.delegate = self
swipe.direction = UISwipeGestureRecognizerDirection.left
self.textView.addGestureRecognizer(swipe)
// data
var arrString = [String]()
arrString.append("\n"+"5 Comme quelques-uns parlaient du temple, qui était orné de belles pierres et d'objets apportés en offrandes, Jésus dit:")
arrString.append("\n"+"6 «Les jours viendront où il ne restera pas pierre sur pierre de ce que vous voyez, tout sera détruit.»")
arrString.append("\n"+"7 Ils lui demandèrent: «Maître, quand donc cela arrivera-t-il et à quel signe reconnaîtra-t-on que ces événements vont se produire?»")
arrString.append("\n"+"8 Jésus répondit: «Faites bien attention à ne pas vous laisser égarer. En effet, beaucoup viendront sous mon nom en disant:'C'est moi', et:'Le moment est arrivé.'Ne les suivez pas.")
arrString.append("\n"+"9 Quand vous entendrez parler de guerres et de soulèvements, ne vous laissez pas effrayer, car il faut que ces choses arrivent d'abord. Cependant, ce ne sera pas encore la fin.»")
arrString.append("\n"+"10 Puis il leur dit: «Une nation se dressera contre une nation et un royaume contre un royaume;")
arrString.append("\n"+"11 il y aura de grands tremblements de terre en divers endroits, ainsi que des pestes et des famines; il y aura des phénomènes terrifiants et de grands signes dans le ciel.")
arrString.append("\n"+"12 Mais, avant tout cela, on mettra la main sur vous et l'on vous persécutera, on vous livrera aux synagogues, on vous jettera en prison, on vous traînera devant des rois et devant des gouverneurs à cause de mon nom.")
// set font
let font = UIFont(name: "SourceSansPro-Regular", size: 20)
let textFont = [NSFontAttributeName:font]
let concat = NSMutableAttributedString()
// set the paragraphe
let count = arrString.count-1
for x in 0...count {
// Create a string that will be our paragraph
let para = NSMutableAttributedString()
// Create locally formatted strings
let attrString = NSAttributedString(string: arrString[x], attributes:(textFont as Any as! [String : Any]))
para.append(attrString)
// Define paragraph styling
let paraStyle = NSMutableParagraphStyle()
//paraStyle.firstLineHeadIndent = 15.0
//paraStyle.paragraphSpacingBefore = 10.0
paraStyle.lineSpacing = 5
// Apply paragraph styles to paragraph
para.addAttribute(NSParagraphStyleAttributeName, value: paraStyle, range: NSRange(location: 0,length: para.length))
// concat paragraphe
concat.append(para)
}
// assign para
textView.attributedText = concat
}
func swipeOnce(_ gestureRecognizer: UITapGestureRecognizer)
{
// Get the location of the gesture recogniser in the text view
let point:CGPoint = gestureRecognizer.location(in: self.textView)
// Get the character index of the swipe
let index:Int = self.textView.layoutManager.characterIndex(for: point, in: self.textView.textContainer, fractionOfDistanceBetweenInsertionPoints: nil)
print(index);
}
danke.
Machen Sie Ihr Array von Strings zu einer Klasseneigenschaft, damit Sie über die Aktionsmethode des Swipe-Gestenerkenners darauf zugreifen können. – nathan