IOS代码笔记UIView的placeholder的效果
程序员文章站
2023-12-15 10:43:28
本文实例为大家分享了ios占位符效果,供大家参考,具体内容如下
一、效果图
二、工程图
三、代码
rootviewcontr...
本文实例为大家分享了ios占位符效果,供大家参考,具体内容如下
一、效果图
二、工程图
三、代码
rootviewcontroller.h
#import <uikit/uikit.h> @interface rootviewcontroller : uiviewcontroller <uitextviewdelegate> { uitextview *pstextview; uilabel *pslabel; } @end
rootviewcontroller.m
#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.title=@"uiview的placeholder的效果"; [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; }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。