本文实例为大家分享了ios占位符效果,供大家参考,具体内容如下
一、效果图
二、工程图
三、代码
rootviewcontroller.h
1
2
3
4
5
6
7
8
9
|
#import <uikit/uikit.h>
@interface rootviewcontroller : uiviewcontroller
<uitextviewdelegate>
{
uitextview *pstextview;
uilabel *pslabel;
}
@end
|
rootviewcontroller.m
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
56
57
58
59
60
61
62
63
64
65
66
67
68
|
#import "rootviewcontroller.h"
@interface rootviewcontroller ()
@end
@implementation rootviewcontroller
- (id)initwithnibname:(nsstring *)nibnameornil bundle:(nsbundle *)nibbundleornil
{
self = [super initwithnibname:nibnameornil bundle:nibbundleornil];
if (self) {
// custom initialization
}
return self;
}
- ( void )viewdidload
{
[super viewdidload];
// do any additional setup after loading the view.
[self initbackgroundview];
}
#pragma -mark -初始化背景图
-( void )initbackgroundview
{
uiview *backview=[[uiview alloc]initwithframe:cgrectmake(0, 0, 320, 480)];
[self.view addsubview:backview];
pstextview=[[uitextview alloc]initwithframe:cgrectmake(10, 70, 280, 200)];
pstextview.text=@ "" ;
pstextview.backgroundcolor=[uicolor yellowcolor];
pstextview.delegate=self;
pstextview.scrollenabled=no;
[backview addsubview:pstextview];
pslabel=[[uilabel alloc]initwithframe:cgrectmake(15, 140, 280, 20)];
pslabel.text=@ "请输入备注信息" ;
pslabel.textcolor=[uicolor graycolor];
pslabel.enabled=no;
pslabel.backgroundcolor=[uicolor clearcolor];
[backview addsubview:pslabel];
}
#pragma -mark -uitextview delegate
- ( bool )textviewshouldbeginediting:(uitextview *)textview
{
return yes;
}
-( bool )textview:(uitextview *)textview shouldchangetextinrange:(nsrange)range replacementtext:(nsstring *)text
{
if (text.length>0) {
pslabel.hidden=yes;
}
if ([text isequaltostring:@ "\n" ]) {
[textview resignfirstresponder];
return no;
}
return yes;
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。