我一直在努力用Swift检测UITextView上的点击.
我的UITextViews在一个表中,我必须能够检测链接并按下它们,这些链接长度未知.
此外,如果我点击单元格,我没有点击链接,我想在我的导航控制器堆栈上推送一个新的UIViewController.
我试图创建自己的textview来覆盖touchesCancelled,但这并不成功.它检测到在实际设备上不被视为点击的取消.
该模拟器中不会出现该错误,但似乎我无法点击真实设备,只需长按即可.
class LinkTextView: UITextView {
override func touchesCancelled(touches: Set<NSObject>!, withEvent event: UIEvent!) {
self.textViewWasTapped(self)
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试直接添加手势识别器.我也没有任何成功.它根本不会调用手势识别器.
我将UIGestureReconizer委托添加到我的UIViewController和我的那些行中
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var singleTap : UIGestureRecognizer = UIGestureRecognizer(target: self, action: "tapTextView:")
singleTap.delegate = self
cell.mTextOutlet.attributedText = myMutableString
cell.mTextOutlet.addGestureRecognizer(singleTap)
Run Code Online (Sandbox Code Playgroud)
在我的LinkTextView类中:
class LinkTextView: UITextView {
func tapTextView(stapGesture: UIGestureRecognizer){
println("TAPPING1")
}
}
Run Code Online (Sandbox Code Playgroud)
我看了论坛,发现这篇文章:发帖.它建议使用CHHLinkTextView.我尝试使用它,但我想要的是自动检测链接,正常的uitextview实际上是这样.
我尝试使用界面构建器中的checkBox来解析CHHLinkTextView的链接,但它不起作用.我没有在文档中看到任何暗示它可以完成的内容.
我该怎么办?
我在视图中有一些代码,它使用CoreText绘制一些属性文本.在这里,我正在搜索网址并将其设为蓝色.我们的想法是不要UIWebView为了获得可点击链接而带来所有开销.一旦用户点击该链接(而不是整个表视图单元格),我想触发一个委托方法,然后该方法将用于呈现一个模态视图,其中包含一个转到该URL的Web视图.
我将路径和字符串本身保存为视图的实例变量,并且绘图代码发生在-drawRect:(我为了简洁而将其遗漏).
然而,我的触摸处理程序虽然不完整,却没有打印出我期望它的内容.它在下面:
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
CGContextRef context = UIGraphicsGetCurrentContext();
NSLog(@"attribString = %@", self.attribString);
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)self.attribString);
CTFrameRef ctframe = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, self.attribString.length), attribPath, NULL);
CFArrayRef lines = CTFrameGetLines(ctframe);
for(CFIndex i = 0; i < CFArrayGetCount(lines); i++)
{
CTLineRef line = CFArrayGetValueAtIndex(lines, i);
CGRect lineBounds = CTLineGetImageBounds(line, context);
// Check if tap was on our line
if(CGRectContainsPoint(lineBounds, point))
{
NSLog(@"Tapped line");
CFArrayRef …Run Code Online (Sandbox Code Playgroud) 我知道之前已经问过这个问题,虽然我似乎无法找到我想要的东西.我的应用程序中有一个部分,其中有一个带有textview的tableview.我不想为tableview单元格分别创建.xib,.h和.m文件.tableview不需要缩小或增长,具体取决于textview中的文本量.我也不希望textview可以编辑.我希望这不是太多要求,虽然我现在真的被困住了.