Swift’te bir UITextView içindeki HTML metni işlemek ve linklere tıklanınca özel bir işlem gerçekleştirmek için UITextViewDelegate protokolünü kullanabilirsiniz. İşte temel bir örnek:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
| import UIKit
class ViewController: UIViewController, UITextViewDelegate {
@IBOutlet weak var textView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
// TextView'in delegate'ini ayarla
textView.delegate = self
// HTML string'i oluştur
let htmlString = "<p>Bu bir <a href='custom://link'>özel link</a> içeren HTML metnidir.</p>"
// HTML string'ini attributed string'e çevir
if let attributedString = attributedStringFromHTML(htmlString) {
textView.attributedText = attributedString
}
}
// HTML string'ini attributed string'e çeviren fonksiyon
func attributedStringFromHTML(_ htmlString: String) -> NSAttributedString? {
do {
let data = htmlString.data(using: .utf8)
let options: [NSAttributedString.DocumentReadingOptionKey: Any] = [
.documentType: NSAttributedString.DocumentType.html,
.characterEncoding: String.Encoding.utf8.rawValue
]
let attributedString = try NSAttributedString(data: data!, options: options, documentAttributes: nil)
return attributedString
} catch {
print("HTML string'i çevirme hatası: \(error.localizedDescription)")
return nil
}
}
// Link tıklandığında çağrılan delegate metodu
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
// Tıklanan linkin URL'sini kontrol et
if URL.scheme == "custom" && URL.host == "link" {
// Özel link tıklandığında yapılacak işlemi burada gerçekleştir
print("Özel link tıklandı!")
return false // Linkin varsayılan işlemi gerçekleşmesin
}
// Diğer durumlar için varsayılan işlemi gerçekleştir
return true
}
}
|
Bu örnekte, UITextView‘in delegate’ini ayarladık ve shouldInteractWith metodunu kullanarak linklere tıklanınca özel bir işlemi ele aldık. attributedStringFromHTML fonksiyonu ise HTML string’ini attributed string’e çevirmek için kullanıldı. Özel bir link tıklandığında, kendi belirlediğiniz işlemleri gerçekleştirebilirsiniz.