ios - 如何在 NSAttributedString 中创建可点击链接

标签 ios swift

我正在尝试使 UITextView 内容的某些部分可点击。这是我的代码:

class ViewController: UIViewController,UITextViewDelegate {


@IBOutlet weak var definition:UITextView!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    //self.loadHTML()
    self.makeList()
}

func makeList(){

    definition.delegate = self

    let attributedString = NSMutableAttributedString(string: "Want to learn iOS? You should visit the best source of free iOS tutorials!")
    attributedString.addAttribute(NSLinkAttributeName, value: "https://www.hackingwithswift.com", range: NSRange(location: 0, length: 5))
    definition.attributedText = attributedString
}



func textView(textView: UITextView, shouldInteractWithURL URL: NSURL, inRange characterRange: NSRange) -> Bool {
    UIApplication.sharedApplication().openURL(URL)
    return true
}
}

有些这是行不通的。你能帮帮我吗?

最佳答案

    definition.editable = false;
    definition.delegate = self;

希望能帮到你。

关于ios - 如何在 NSAttributedString 中创建可点击链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37290831/

相关文章:

ios - Swift UITableView,插入,然后删除,然后插入

ios - UICollectionView 选择和取消选择问题

arrays - Swift Array 的 Map 方法是否并发实现?

ios - 为什么 NSPredicate with block 在评估 nil 时会失败?

ios - View 宽度无法通过自动布局正确更改

objective-c - 如何在 Swift 中编写 array.valueForKey?

ios - UITableViewCell 子类的行为不像 UITableViewCells

ios - 使用 Xcode 和 Swift 在 pageControler 中播放声音文件

iphone - 具有自定义 View 的 UIBarButtonItem 在第一次查看后不显示

ios - 如何在 iOS Safari(在实际设备上)中将 "crop"关闭屏幕 <div>?