怎么响应uitextview里某个链接的点击事件?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了怎么响应uitextview里某个链接的点击事件?相关的知识,希望对你有一定的参考价值。
Uitextview里有个链接,我想点击它的时候,safari browser打开,加载相应的内容,怎么实现?
参考技术A内部用webView打开:就设置代理(uitextview.delegate = self)和禁止编辑(uitextview.editable = NO),实现代理方法
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:;在这个代理方法中拦截URL,并用webView打开
App外部打开(safari打开):自己会跳转,不需要实现代理方法,什么都不用干。
参考技术B 如果不用safari打开。用内部的浏览器打开。也就是说自己写的viewcontroller中的webview打开呢。如何获得uitextview中的链接点击事件呢。 参考技术C txtView.dataDetectorTypes = UIDataDetectorTypeLink;这样会自动把uitextview中的连结转成hyper link直接点击就会自动开启safari了 参考技术D @interface MyApplication : UIApplication @end@implementation MyApplication-(BOOL)openURL:(NSURL *)url if ([self.delegate openURL:url]) return YES; else return [super openURL:url];@endQT鼠标点击响应事件
假如我们想在窗口指定区域响应鼠标点击事件,怎么办呢?
比如我们有一个widget窗口,该窗口里有一个PixmapLabel图片,
我们假设想在图片的左上角响应鼠标的点击事件,那么我们可以这样做!
1. 创建一个新类
//mainForm.h
#ifndef MAINFORM_H
#define MAINFORM_H
#include <qevent.h>
#include <qvariant.h>
#include <qwidget.h>
#include <qmessagebox.h>
class QVBoxLayout;
class QHBoxLayout;
class QGridLayout;
class QLabel;
class mainForm : public QWidget
{
Q_OBJECT
public:
mainForm( QWidget* parent = 0, const char* name = 0, WFlags fl = 0 );
~mainForm();
QLabel* myPixmapLabel;
signals:
void clicked();
public slots:
virtual void mousePressEventSlot();
protected:
void mousePressEvent(QMouseEvent *);
protected slots:
virtual void languageChange();
};
#endif // MAINFORM_H
2. 实现文件
//mainForm.cpp
#include "mainForm.h"
#include <qvariant.h>
#include <qlabel.h>
#include <qlayout.h>
#include <qtooltip.h>
#include <qwhatsthis.h>
#include <qimage.h>
#include <qpixmap.h>
/*
* Constructs a mainForm as a child of 'parent', with the
* name 'name' and widget flags set to 'f'.
*/
mainForm::mainForm( QWidget* parent, const char* name, WFlags fl )
: QWidget( parent, name, fl )
{
if ( !name )
setName( "mainForm" );
//setCaption("Qt Mouse Click Event Example");
myPixmapLabel = new QLabel( this, "myPixmapLabel" );
myPixmapLabel->setGeometry( QRect( 120, 60, 300, 270 ) );
//放置一个图片,该图片应该在同一文件夹里,否则要指定路径
myPixmapLabel->setPixmap( QPixmap::fromMimeSource( "about-to-install.png" ) );
myPixmapLabel->setScaledContents( TRUE );
languageChange();
resize( QSize(600, 480).expandedTo(minimumSizeHint()) );
connect( this, SIGNAL( clicked() ), this, SLOT( mousePressEventSlot() ) ); //信号连接
}
/*
* Destroys the object and frees any allocated resources
*/
mainForm::~mainForm()
{
// no need to delete child widgets, Qt does it all for us
}
void mainForm::mousePressEvent(QMouseEvent *e)
{
int x = e->x();
int y = e->y();
//假如在QRect( 120, 60, 200, 200 ) );这个区域里,就发出信号
if (x>120 && x<200 && y>60 && y<200)
emit clicked();
}
void mainForm::mousePressEventSlot()
{
//该信号响应的曹
//给出一个提示信息
QMessageBox::about( this, "Qt Mouse Click Event Example",
"You haved clicked the prearranged position /nand the widget will be closed."
);
close(); //关闭程序
}
/*
* Sets the strings of the subwidgets using the current
* language.
*/
void mainForm::languageChange()
{
setCaption( tr( "Qt Mouse Click Event Example" ) );
}
3. main 函数
//main.cpp
#include <qapplication.h>
#include "mainForm.h"
int main( int argc, char ** argv )
{
QApplication a( argc, argv );
mainForm w;
w.show();
a.connect( &a, SIGNAL( lastWindowClosed() ), &a, SLOT( quit() ) );
return a.exec();
}
将三个上述文件放在同一文件夹里,然后新建 pro 文件:
TEMPLATE = app
INCLUDEPATH += .
# Input
HEADERS += mainForm.h
SOURCES += main.cpp mainForm.cpp
然后:
qmake
make
直接运行,看看效果如何?
比如我们有一个widget窗口,该窗口里有一个PixmapLabel图片,
我们假设想在图片的左上角响应鼠标的点击事件,那么我们可以这样做!
1. 创建一个新类
//mainForm.h
#ifndef MAINFORM_H
#define MAINFORM_H
#include <qevent.h>
#include <qvariant.h>
#include <qwidget.h>
#include <qmessagebox.h>
class QVBoxLayout;
class QHBoxLayout;
class QGridLayout;
class QLabel;
class mainForm : public QWidget
{
Q_OBJECT
public:
mainForm( QWidget* parent = 0, const char* name = 0, WFlags fl = 0 );
~mainForm();
QLabel* myPixmapLabel;
signals:
void clicked();
public slots:
virtual void mousePressEventSlot();
protected:
void mousePressEvent(QMouseEvent *);
protected slots:
virtual void languageChange();
};
#endif // MAINFORM_H
2. 实现文件
//mainForm.cpp
#include "mainForm.h"
#include <qvariant.h>
#include <qlabel.h>
#include <qlayout.h>
#include <qtooltip.h>
#include <qwhatsthis.h>
#include <qimage.h>
#include <qpixmap.h>
/*
* Constructs a mainForm as a child of 'parent', with the
* name 'name' and widget flags set to 'f'.
*/
mainForm::mainForm( QWidget* parent, const char* name, WFlags fl )
: QWidget( parent, name, fl )
{
if ( !name )
setName( "mainForm" );
//setCaption("Qt Mouse Click Event Example");
myPixmapLabel = new QLabel( this, "myPixmapLabel" );
myPixmapLabel->setGeometry( QRect( 120, 60, 300, 270 ) );
//放置一个图片,该图片应该在同一文件夹里,否则要指定路径
myPixmapLabel->setPixmap( QPixmap::fromMimeSource( "about-to-install.png" ) );
myPixmapLabel->setScaledContents( TRUE );
languageChange();
resize( QSize(600, 480).expandedTo(minimumSizeHint()) );
connect( this, SIGNAL( clicked() ), this, SLOT( mousePressEventSlot() ) ); //信号连接
}
/*
* Destroys the object and frees any allocated resources
*/
mainForm::~mainForm()
{
// no need to delete child widgets, Qt does it all for us
}
void mainForm::mousePressEvent(QMouseEvent *e)
{
int x = e->x();
int y = e->y();
//假如在QRect( 120, 60, 200, 200 ) );这个区域里,就发出信号
if (x>120 && x<200 && y>60 && y<200)
emit clicked();
}
void mainForm::mousePressEventSlot()
{
//该信号响应的曹
//给出一个提示信息
QMessageBox::about( this, "Qt Mouse Click Event Example",
"You haved clicked the prearranged position /nand the widget will be closed."
);
close(); //关闭程序
}
/*
* Sets the strings of the subwidgets using the current
* language.
*/
void mainForm::languageChange()
{
setCaption( tr( "Qt Mouse Click Event Example" ) );
}
3. main 函数
//main.cpp
#include <qapplication.h>
#include "mainForm.h"
int main( int argc, char ** argv )
{
QApplication a( argc, argv );
mainForm w;
w.show();
a.connect( &a, SIGNAL( lastWindowClosed() ), &a, SLOT( quit() ) );
return a.exec();
}
将三个上述文件放在同一文件夹里,然后新建 pro 文件:
TEMPLATE = app
INCLUDEPATH += .
# Input
HEADERS += mainForm.h
SOURCES += main.cpp mainForm.cpp
然后:
qmake
make
直接运行,看看效果如何?
以上是关于怎么响应uitextview里某个链接的点击事件?的主要内容,如果未能解决你的问题,请参考以下文章