-
Notifications
You must be signed in to change notification settings - Fork 2
/
mytextview.mm
56 lines (46 loc) · 1.39 KB
/
mytextview.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "mytextview.h"
#include <QQuickWindow>
#include <QDebug>
#import <CoreFoundation/CoreFoundation.h>
#import <UIKit/UIKit.h>
static inline CGRect toCGRect(const QRectF &rect)
{
return CGRectMake(rect.x(), rect.y(), rect.width(), rect.height());
}
MyTextView::MyTextView(QQuickItem *parent /*= 0*/)
: QQuickItem(parent)
, m_view(0)
{
connect(this, SIGNAL(windowChanged(QQuickWindow*)), this, SLOT(onWindowChanged(QQuickWindow*)));
connect(this, SIGNAL(visibleChanged()), this, SLOT(onVisibleChanged()));
}
MyTextView::~MyTextView()
{
[m_view release];
}
void MyTextView::onWindowChanged(QQuickWindow* window)
{
if(!m_view) {
m_view = [[UITextView alloc] init];
// m_view.backgroundColor = [UIColor grayColor];
}
if (window != 0) {
UIView *parentView = reinterpret_cast<UIView *>(window->winId());
[parentView addSubview:m_view];
} else {
[m_view removeFromSuperView];
}
}
void MyTextView::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry)
{
QQuickItem::geometryChanged(newGeometry, oldGeometry);
CGRect rc = toCGRect(mapRectToScene(newGeometry).toRect());
rc.size.width -= 2;
rc.size.height -= 2;
[m_view setFrame:rc];
qDebug() << newGeometry << [m_view frame].size.width << [m_view frame].size.height;
}
void MyTextView::onVisibleChanged()
{
[m_view setHidden:isVisible()];
}