Ios 在键盘显示时检测UITextView中属性文本的点击
这是对质询的补充质询 我用Xcode 7.1.1和iOS 9.1重新测试了以下代码,它与我链接到的答案中描述的设置配合得很好Ios 在键盘显示时检测UITextView中属性文本的点击,ios,objective-c,swift,uitextview,touch-event,Ios,Objective C,Swift,Uitextview,Touch Event,这是对质询的补充质询 我用Xcode 7.1.1和iOS 9.1重新测试了以下代码,它与我链接到的答案中描述的设置配合得很好 import UIKit class ViewController: UIViewController, UIGestureRecognizerDelegate { @IBOutlet weak var textView: UITextView! override func viewDidLoad() { super.viewDidLo
import UIKit
class ViewController: UIViewController, UIGestureRecognizerDelegate {
@IBOutlet weak var textView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
// Create an attributed string
let myString = NSMutableAttributedString(string: "Swift attributed text")
// Set an attribute on part of the string
let myRange = NSRange(location: 0, length: 5) // range of "Swift"
let myCustomAttribute = [ "MyCustomAttributeName": "some value"]
myString.addAttributes(myCustomAttribute, range: myRange)
textView.attributedText = myString
// Add tap gesture recognizer to Text View
let tap = UITapGestureRecognizer(target: self, action: Selector("myMethodToHandleTap:"))
tap.delegate = self
textView.addGestureRecognizer(tap)
}
func myMethodToHandleTap(sender: UITapGestureRecognizer) {
let myTextView = sender.view as! UITextView
let layoutManager = myTextView.layoutManager
// location of tap in myTextView coordinates and taking the inset into account
var location = sender.locationInView(myTextView)
location.x -= myTextView.textContainerInset.left;
location.y -= myTextView.textContainerInset.top;
// character index at tap location
let characterIndex = layoutManager.characterIndexForPoint(location, inTextContainer: myTextView.textContainer, fractionOfDistanceBetweenInsertionPoints: nil)
// if index is valid then do something.
if characterIndex < myTextView.textStorage.length {
// print the character index
print("character index: \(characterIndex)")
// print the character at the index
let myRange = NSRange(location: characterIndex, length: 1)
let substring = (myTextView.attributedText.string as NSString).substringWithRange(myRange)
print("character at index: \(substring)")
// check if the tap location has a certain attribute
let attributeName = "MyCustomAttributeName"
let attributeValue = myTextView.attributedText.attribute(attributeName, atIndex: characterIndex, effectiveRange: nil) as? String
if let value = attributeValue {
print("You tapped on \(attributeName) and the value is: \(value)")
}
}
}
}
导入UIKit
类ViewController:UIViewController、UIGestureRecognitizerDelegate{
@IBOutlet弱var textView:UITextView!
重写func viewDidLoad(){
super.viewDidLoad()
//创建属性化字符串
让myString=NSMutableAttributedString(字符串:“Swift属性文本”)
//在字符串的一部分设置属性
设myRange=NSRange(位置:0,长度:5)//范围为“Swift”
让myCustomAttribute=[“MyCustomAttributeName”:“某些值”]
addAttributes(myCustomAttribute,范围:myRange)
textView.attributedText=myString
//将点击手势识别器添加到文本视图
让tap=uitappesturerecognizer(目标:self,操作:选择器(“myMethodToHandleTap:”)
tap.delegate=self
textView.AddGestureRecognitor(点击)
}
func myMethodToHandleTap(发件人:UITapGestureRecognitizer){
让myTextView=sender.view为!UITextView
让layoutManager=myTextView.layoutManager
//点击在myTextView坐标中的位置,并考虑插入
var location=sender.locationInView(myTextView)
location.x-=myTextView.TextContainerSet.left;
location.y-=myTextView.TextContainerSet.top;
//抽头位置的字符索引
让characterIndex=layoutManager.CharacterIndexOrpPoint(位置,inTextContainer:myTextView.textContainer,插入点之间距离的分数:nil)
//如果索引是有效的,那么做一些事情。
如果characterIndex
但是,如果更改了UITextView
设置,使其既可编辑又可选择
然后,这将导致键盘显示。显示键盘后,将不再调用tap事件处理程序。当键盘显示时,如何检测属性文本上的点击
更新
虽然这里的代码是用Swift编写的,但最初提出这个问题的人(在对我上面链接的答案的评论中)正在使用Objective-C。因此我很乐意接受用Swift或Objective-C编写的答案。在UITextView的控制器中,您可以实现
UITextViewDelegate
,然后重写方法
-(BOOL)textViewShouldBeginEditing:(UITextView *)textView{
}
在该方法中,您可以访问textView的selectedRange,它也应该是属性文本的“点击范围”。然后根据需要返回true/false。我想获取使用objective c点击的字符串,你知道如何编写代码吗?@Liu-你已经看到问题了吗?在Objective-C中有几个答案。是的,但我只想点击某个文本一次,换句话说,当我点击一个单词时,我想删除该单词的属性,这样我就不能点击两次,我不知道如何点击,你知道吗?我不知道Objective-C,但过程是在点击位置获取单词的范围,然后从该范围中删除属性。如果您找不到堆栈溢出问题和答案来告诉您如何做,那么您可以编写自己的问题和答案。(并在此处的评论中添加链接。)这是触摸事件冲突。这里有两种可能的解决方案:1:将点击手势添加到文本字段的superview,并可以将
delaysTouchesBegan
设置为YES,以优先考虑您的手势。2:子类TextField,并在自定义touchsbegind方法中进行交易点击。