Comment ça « le self n’est plus le bon » ? C’est dans le cas ou la cell est réutilisé ? Si tu scroll pas ça marche quand même ? (je comprend que c’est pas le comportement optimal mais c’est pour restreindre le problème)
if let description = questionDetail.description{
var description = description
let attributes = [ NSAttributedString.Key.font: UIFont.init(name: "HiraginoSans-W3", size: 12)! ]
var attrString = NSMutableAttributedString(string: description, attributes : attributes)
attrString = Utils.checkMention(attributedString: attrString)
let style = NSMutableParagraphStyle()
style.lineSpacing = 2 // change line spacing between paragraph like 36 or 48
style.minimumLineHeight = 20 // change line spacing between each line like 30 or 40
let nsText = attrString.string as NSString
let textRange = nsText.range(of: attrString.string)
attrString.addAttribute(NSAttributedString.Key.paragraphStyle, value: style, range: textRange)
textView.attributedText = attrString
textView.isEditable = false
textView.dataDetectorTypes = .all
textView.isSelectable = true
textView.isScrollEnabled = false
}
J'ai modifie ce code comme ceci :
if let description = questionDetail.description{
var description = description
if (isTranslated && questionDetail.description_translated != nil) {
var _self = self
let currentLang = UserDefaults.standard.value(forKey: "lang") as! String
SwiftGoogleTranslate.shared.detect(description) { (detections, error) in
if let detections = detections {
for detection in detections {
SwiftGoogleTranslate.shared.translate(description, currentLang, detection.language) { (text, error) in
description = text as! String
let attributes = [ NSAttributedString.Key.font: UIFont.init(name: "HiraginoSans-W3", size: 12)! ]
var attrString = NSMutableAttributedString(string: description, attributes : attributes)
attrString = Utils.checkMention(attributedString: attrString)
let style = NSMutableParagraphStyle()
style.lineSpacing = 2 // change line spacing between paragraph like 36 or 48
style.minimumLineHeight = 20 // change line spacing between each line like 30 or 40
let nsText = attrString.string as NSString
let textRange = nsText.range(of: attrString.string)
attrString.addAttribute(NSAttributedString.Key.paragraphStyle, value: style, range: textRange)
_self.textView.attributedText = attrString
_self.textView.isEditable = false
_self.textView.dataDetectorTypes = .all
_self.textView.isSelectable = true
_self.textView.isScrollEnabled = false
}
}
}
}
}
let attributes = [ NSAttributedString.Key.font: UIFont.init(name: "HiraginoSans-W3", size: 12)! ]
var attrString = NSMutableAttributedString(string: description, attributes : attributes)
attrString = Utils.checkMention(attributedString: attrString)
let style = NSMutableParagraphStyle()
style.lineSpacing = 2 // change line spacing between paragraph like 36 or 48
style.minimumLineHeight = 20 // change line spacing between each line like 30 or 40
let nsText = attrString.string as NSString
let textRange = nsText.range(of: attrString.string)
attrString.addAttribute(NSAttributedString.Key.paragraphStyle, value: style, range: textRange)
textView.attributedText = attrString
textView.isEditable = false
textView.dataDetectorTypes = .all
textView.isSelectable = true
textView.isScrollEnabled = false
}
Comme SwiftGoogleTranslate n'est pas bloquant, il est execute apres mais l'application semble ne pas trop aimer cette technique
"ne pas trop aimer cette technique" C'est un peu flou. Et si tu fais ça, ça donne quoi ? :
if let description = questionDetail.description, let currentLang = UserDefaults.standard.string(forKey: "lang"){
if isTranslated && questionDetail.description_translated != nil {
SwiftGoogleTranslate.shared.detect(description) {[weak textView] (detections, error) in
for detection in detections ?? [] {
// Je ne sais pas trop ce qui est async et ce qui ne l'est pas, je duplique au pif SwiftGoogleTranslate.shared.translate(description, currentLang, detection.language) { [weak textView](text, error) in
description = text as! String
let attributes = [ NSAttributedString.Key.font: UIFont.init(name: "HiraginoSans-W3", size: 12)! ]
var attrString = NSMutableAttributedString(string: text, attributes : attributes)
attrString = Utils.checkMention(attributedString: attrString)
let style = NSMutableParagraphStyle()
style.lineSpacing = 2 // change line spacing between paragraph like 36 or 48
style.minimumLineHeight = 20 // change line spacing between each line like 30 or 40
let nsText = attrString.string as NSString
let textRange = nsText.range(of: attrString.string)
attrString.addAttribute(NSAttributedString.Key.paragraphStyle, value: style, range: textRange)
textView?.attributedText = attrString
textView?.isEditable = false
textView?.dataDetectorTypes = .all
textView?.isSelectable = true
textView?.isScrollEnabled = false
}
}
}
}
}
let attributes = [ NSAttributedString.Key.font: UIFont.init(name: "HiraginoSans-W3", size: 12)! ]
var attrString = NSMutableAttributedString(string: description, attributes : attributes)
attrString = Utils.checkMention(attributedString: attrString)
let style = NSMutableParagraphStyle()
style.lineSpacing = 2 // change line spacing between paragraph like 36 or 48
style.minimumLineHeight = 20 // change line spacing between each line like 30 or 40
let nsText = attrString.string as NSString
let textRange = nsText.range(of: attrString.string)
attrString.addAttribute(NSAttributedString.Key.paragraphStyle, value: style, range: textRange)
textView.attributedText = attrString
textView.isEditable = false
textView.dataDetectorTypes = .all
textView.isSelectable = true
textView.isScrollEnabled = false
}
Y'a surement des erreurs de compilation, comme une accolade en trop. J'ai édité à la volée, pas simple de s'y retrouver
- Edité par Geda 7 février 2019 à 10:33:41
Swift: Utilisation de Google Translate
× Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
× Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.