テキストビューにNSMutableAttributedStringを追加しています.tappableStringをクリックすると新しいVCを表示します。 VCは提示されていないので、どこで間違っているのか分からない。Swift iOS -NSMutableAttributedString ViewControllerが表示されませんか?
私はTextViewのデリゲートを設定するとのTextViewの...shouldInteractWithURL
を使用して、そこの内側に私は、URLの絶対的な文字列が「doSomethingの」ですが、VCが表示されないNSLinkAttributeName's
値と一致することを確認してください。
どこが間違っていますか?
class FirstController: UIViewController, UITextViewDelegate {
fileprivate let textView: UITextView = {
let textView = UITextView()
textView.translatesAutoresizingMaskIntoConstraints = false
textView.isEditable = false
textView.isSelectable = true
textView.textAlignment = .center
textView.isScrollEnabled = true
return textView
}()
override func viewDidLoad() {
super.viewDidLoad()
textView.delegate = self
configureTextView()
}
override func viewDidLayoutSubviews() {
textView.setContentOffset(.zero, animated: false)
}
func configureTextView(){
//textView anchors are set
let firstPlainSent = "Read this first.\n"
let secondPlainSent = "Read this second.\n"
plainSentAttr = [NSFontAttributeName: UIFont(name: "Helvetica", size: 17)!, NSForegroundColorAttributeName: UIColor.black]
let firstSent = NSAttributedString(string: firstPlainSent, attributes: plainSentAttr)
let secondSent = NSAttributedString(string: secondPlainSent, attributes: plainSentAttr)
let mutableAttributedString = NSMutableAttributedString()
mutableAttributedString.append(firstSent)
mutableAttributedString.append(secondSent)
let tappableString = NSMutableAttributedString(string: "Click here to present the SecondVC\n\n")
tappableString.addAttribute(NSFontAttributeName, value: UIFont(name: "Helvetica", size: 17)!, range: NSMakeRange(0, (tappableString.length)))
tappableString.addAttribute(NSForegroundColorAttributeName, value: UIColor.blue, range: NSMakeRange(0, (tappableString.length)))
tappableString.addAttribute(NSUnderlineStyleAttributeName, value: 1, range: NSMakeRange(0,tappableString.length))
tappableString.addAttribute(NSUnderlineColorAttributeName, value: UIColor.blue, range: NSMakeRange(0, tappableString.length))
tappableString.addAttribute(NSLinkAttributeName, value: "doSomething", range: NSMakeRange(0, (tappableString.length)))
mutableAttributedString.append(tappableString)
textView.attributedText = mutableAttributedString
}
func textView(textView: UITextView, shouldInteractWithURL URL: NSURL, inRange characterRange: NSRange) -> Bool {
// I also tried URL.scheme
if URL.absoluteString == "doSomething"{
let secondVC = SecondController()
let navVC = UINavigationController(rootViewController: secondVC)
present(navVC, animated: true, completion: nil)
return false
}
return true
}
}
新しいプロジェクトにコードを追加しましたが、私の場合は新しいVCが表示されます!コードの他の部分に何か問題がありますか? –
@AndréSlottaは助けてくれてありがとう。私が間違っていることは分かっていません。私はテキストビューとテキストを見ることができるので、アンカーは問題ありません。それはあなたのために、私のためにはうまくいかないのですか?もう一度もう一度チェックします。 –
@AndréSlotta問題は、私はtextViewのメソッドの古いバージョンを使用していました。助けてくれてありがとう! –