iOS 怎么给UITextView添加占位符文字

iOS 怎么给UITextView添加占位符文字,第1张

这个只需要在UItextview的代理函数里面写限制即可,说白了就是判断当前字符串长度大于了你的限制字数,就把后面的删除掉就可以了范例代码 范例代码仅仅是Controller的m文件#import "ViewController.h"@interface ViewController ()<UITextViewDelegate>@property (strong ,nonatomic) UITextView *text@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad]self.text = [[UITextView alloc]initWithFrame:CGRectMake(0, 20, 300, 300)][self.view addSubview:self.text][self.text setBackgroundColor:[UIColor grayColor]]self.text.delegate = self// Do any additional setup after loading the view, typically from a nib.}-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{#define MY_MAX 20if ((textView.text.length - range.length + text.length) >MY_MAX){NSString *substring = [text substringToIndex:MY_MAX - (textView.text.length - range.length)]NSMutableString *lastString = [textView.text mutableCopy][lastString replaceCharactersInRange:range withString:substring]textView.text = [lastString copy]return NO}else{return YES}}- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning]// Dispose of any resources that can be recreated.}@end

在UITextField中自带placeholder属性,可以用于提示输入框信息。但是UITextView并不具备此功能

介绍两种方法来实现:

第一种:

初始化UITextView

//首先定义UITextView

UITextView *textView = [[UITextView alloc] init]

textView.font = [UIFont systemFontOfSize:14]

textView.frame =CGRectMake(10, 0, cell.contentView.bounds.size.width-20, side)

textView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth

textView.backgroundColor = [UIColor whiteColor]

[cell.contentView addSubview:textView]

textView.hidden = NO

textView.delegate = self

//其次在UITextView上面覆盖个UILable,UILable设置为全局变量。

uilabel.frame =CGRectMake(17, 8, cell.contentView.bounds.size.width - side+10, 20)

uilabel.text = @"请填写审批意见..."

uilabel.enabled = NO//lable必须设置为不可用

uilabel.backgroundColor = [UIColor clearColor]

[cell.contentView addSubview:uilabel]

实现UITextView的代理

-(void)textViewDidChange:(UITextView *)textView

{

self.examineText = textView.text

if (textView.text.length == 0) {

uilabel.text = @"请填写审批意见..."

}else{

uilabel.text = @""

}

}

第二种:

UITextView 实现 placeholder 及隐藏键盘

#import <Foundation/Foundation.h>

@interface UIPlaceHolderTextView : UITextView {

NSString *placeholder

UIColor *placeholderColor

@private

UILabel *placeHolderLabel

}

@property(nonatomic, retain) UILabel *placeHolderLabel

@property(nonatomic, retain) NSString *placeholder

@property(nonatomic, retain) UIColor *placeholderColor

-(void)textChanged:(NSNotification*)notification

@end

#import "UIPlaceHolderTextView.h"

@implementation UIPlaceHolderTextView

@synthesize placeHolderLabel

@synthesize placeholder

@synthesize placeholderColor

- (void)dealloc

{

[[NSNotificationCenter defaultCenter] removeObserver:self]

[placeHolderLabel release]placeHolderLabel = nil

[placeholderColor release]placeholderColor = nil

[placeholder release]placeholder = nil

[super dealloc]

}

- (void)awakeFromNib

{

[super awakeFromNib]

[self setPlaceholder:@""]

[self setPlaceholderColor:[UIColor lightGrayColor]]

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged:) name:UITextViewTextDidChangeNotification object:nil]

}

- (id)initWithFrame:(CGRect)frame

{

if( (self = [super initWithFrame:frame]) )

{

[self setPlaceholder:@""]

[self setPlaceholderColor:[UIColor lightGrayColor]]

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged:) name:UITextViewTextDidChangeNotification object:nil]

}

return self

}

- (void)textChanged:(NSNotification *)notification

{

if([[self placeholder] length] == 0)

{

return

}

if([[self text] length] == 0)

{

[[self viewWithTag:999] setAlpha:1]

}

else

{

[[self viewWithTag:999] setAlpha:0]

}

}

- (void)setText:(NSString *)text {

[super setText:text]

[self textChanged:nil]

}

- (void)drawRect:(CGRect)rect

{

if( [[self placeholder] length] >0 )

{

if ( placeHolderLabel == nil )

{

placeHolderLabel = [[UILabel alloc] initWithFrame:CGRectMake(8,8,self.bounds.size.width - 16,0)]

placeHolderLabel.lineBreakMode = UILineBreakModeWordWrap

placeHolderLabel.numberOfLines = 0

placeHolderLabel.font = self.font

placeHolderLabel.backgroundColor = [UIColor clearColor]

placeHolderLabel.textColor = self.placeholderColor

placeHolderLabel.alpha = 0

placeHolderLabel.tag = 999

[self addSubview:placeHolderLabel]

}

placeHolderLabel.text = self.placeholder

[placeHolderLabel sizeToFit]

[self sendSubviewToBack:placeHolderLabel]

}

if( [[self text] length] == 0 &&[[self placeholder] length] >0 )

{

[[self viewWithTag:999] setAlpha:1]

}

[super drawRect:rect]

}

@end

//隐藏键盘,实现UITextViewDelegate

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text

{

if ([text isEqualToString:@"\n"]) {

[m_textView resignFirstResponder]

return NO

}

return YES

}

<img src="default.jpg" originsrc="ture.jpg" id="img" />

<script>

    //懒加载,先加载默认图片,等真实的图片加载出来之后再渲染真实图片

    var img=document.getElementByID("img")

    img.src=img.getAttribute("originsrc")

    img.onload=function(){

        img.setAttribute("src",this.src)

    }

</script>


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/8006738.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-12
下一篇 2023-04-12

发表评论

登录后才能评论

评论列表(0条)

保存