2017-01-18 1 views
2

프로그래밍 방식으로 작성해야하는 링크 된 텍스트보기가 있습니다.UITextView에서 열린 링크 iOS 10 및 iOS 9 (Swift 포함)

현재 링크는 iOS 10에서는 클릭 할 수 있지만 iOS 9.0에서는 클릭 할 수 없습니다.

이 지금까지 코드 :

import UIKit 

class ViewController: UIViewController, UITextViewDelegate { 
    // Empty view container 
    @IBOutlet weak var innerView: UIView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     // Create text view 
     let textView = UITextView(frame: CGRect.zero) 

     // Set the link attribute 
     let linkAttributes = [ 
      NSLinkAttributeName: NSURL(string: "https://www.apple.com")!, 
      NSForegroundColorAttributeName: UIColor.blue 
      ] as [String : Any] 

     let attributedString = NSMutableAttributedString(string: "Just click here to register") 
     attributedString.setAttributes(linkAttributes, range: NSMakeRange(5, 10)) 

     textView.delegate = self 
     textView.attributedText = attributedString 
     textView.isEditable = false 
     textView.isSelectable = true 
     textView.dataDetectorTypes = .link 

     // Add to view 
     textView.sizeToFit() 
     innerView.addSubview(textView) 
    } 

    func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange) -> Bool { 
     if #available(iOS 10.0, *) { 
      UIApplication.shared.open(URL, options: [:]) 
     } else { 
      // Fallback on earlier versions 
      UIApplication.shared.openURL(URL) 
     } 
     return true 
    } 
} 

func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange) -> Bool { 내부에 중단 점을 설정에서 모든 아이폰 OS 9. 다음

에 대한 호출되지 앱이

enter image description here

의 모습입니다

스토리 보드는 내부에 뷰가있는 기본 단일보기 응용 프로그램 템플릿 일뿐입니다.

enter image description here

+0

텍스트 뷰의 선택 속성이 ?? 예입니다 대 링크를 활성화하기 위해 시뮬레이터에 깊은 클릭이 필요합니다. –

+0

@KumarKL은 업데이트 코드입니다. – user2560886

+0

@ user2560886 코드가 iOS 9 및 10에서 모두 작동합니다. –

답변

0

내가 잘못 링크를 클릭했다 밝혀졌습니다. 아이폰 OS 9에서

당신은 아이폰 OS (10)