博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
IOS博客项目搭建-14-发微博自定义TextView输入框
阅读量:2384 次
发布时间:2019-05-10

本文共 6562 字,大约阅读时间需要 21 分钟。

发微博视图,提示文字背景:

065829_8Sjj_2557944.png

 // IWTextView.m   文件  ////  IWTextView.m//  ItcastWeibo////  Created by apple on 14-5-19.//  Copyright (c) 2014年 itcast. All rights reserved.//#import "IWTextView.h"@interface IWTextView()@property (nonatomic,weak) UILabel *placeholderLabel;@end@implementation IWTextView- (id)initWithFrame:(CGRect)frame{    self = [super initWithFrame:frame];    if (self) {        UILabel *placeholderLabel = [[UILabel alloc] init];        placeholderLabel.textColor = [UIColor lightGrayColor];        placeholderLabel.hidden = YES;                // 文本输入框,文字提示的label背景        placeholderLabel.backgroundColor = [UIColor orangeColor];        [self addSubview:placeholderLabel];        self.placeholderLabel = placeholderLabel;    }    return self;}-(void)setPlaceholder:(NSString *)placeholder{    self.placeholderLabel.text = placeholder;        if(placeholder.length)    // 是否需要显示    {        self.placeholderLabel.hidden = NO;        self.placeholderLabel.frame = self.bounds;                /*        // 计算frame        CGFloat maxH = self.frame.size.height;        CGFloat maxW = self.frame.size.width;        [placeholder sizeWithFont:self.placeholderLabel.font constrainedToSize:CGSizeMake(maxW, maxH)];                self.placeholderLabel.frame = CGRectMake(<#CGFloat x#>, <#CGFloat y#>, <#CGFloat width#>, <#CGFloat height#>);         */            }else{        self.placeholderLabel.hidden = YES;    }        self.placeholderLabel.hidden = (placeholder.length == 0);}@end

065720_Gin6_2557944.png   070152_Q6T5_2557944.png

文字提示“分享新鲜事”在label的中间,怎么让在第一行左边呢?

IWTextView.m文件:

-(void)setPlaceholder:(NSString *)placeholder{    self.placeholderLabel.text = placeholder;        if(placeholder.length)    // 是否需要显示    {        self.placeholderLabel.hidden = NO;        self.placeholderLabel.frame = self.bounds;                      // 计算frame,设置文字提示“分享新鲜事...”的显示位置        CGFloat maxH = self.frame.size.height;        CGFloat maxW = self.frame.size.width;        CGSize placeholderSize = [placeholder sizeWithFont:self.placeholderLabel.font constrainedToSize:CGSizeMake(maxW, maxH)];                self.placeholderLabel.frame = CGRectMake(0, 0, placeholderSize.width, placeholderSize.height);                   }else{        self.placeholderLabel.hidden = YES;    }        self.placeholderLabel.hidden = (placeholder.length == 0);}

  设置文字换行:

 placeholderLabel.numberOfLines = 0;   // 文字换行

071532_YyzY_2557944.png

071758_YXo2_2557944.png

从上图可以看出,label跑到光标上边去了,怎么调整呢?

设置placeholderLabel 的添加到父控制器的方式:

IWTextView.m文件:

     // [self addSubview:placeholderLabel];   [self insertSubview:placeholderLabel atIndex:0];

然后在设置Placeholder的方法中,设置光标的位置:

     -(void)setPlaceholder:(NSString *)placeholder{    self.placeholderLabel.text = placeholder;        if(placeholder.length)    // 是否需要显示    {        self.placeholderLabel.hidden = NO;        self.placeholderLabel.frame = self.bounds;                      // 计算frame,设置文字提示“分享新鲜事...”的显示位置        CGFloat placeholderX = 5;   // ******光标初始位置******        CGFloat placeholderY = 8;   // ******光标初始位置******                CGFloat maxH = self.frame.size.height;        CGFloat maxW = self.frame.size.width;        CGSize placeholderSize = [placeholder sizeWithFont:self.placeholderLabel.font constrainedToSize:CGSizeMake(maxW, maxH)];                self.placeholderLabel.frame = CGRectMake(placeholderX, placeholderY, placeholderSize.width, placeholderSize.height);                   }else{        self.placeholderLabel.hidden = YES;    }        self.placeholderLabel.hidden = (placeholder.length == 0);}

073012_m6Fo_2557944.png

我们可以看到,光标已经出现在第一行和提示文字同一行的位置,

下面来实现当输入文字时,提示文字消失,而当删除文字时,又出现提示

实现思路:需要监听textView文字输入,NSNotificationCenter消息通知,当有文字输入时隐藏placeholderLabel

具体实现代码:

    // 2.******监听文字改变,是否隐藏placeholder****** [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:self];

 IWTextView.h头文件

//  IWTextView.h#import 
@interface IWTextView : UITextView@property (nonatomic, copy)NSString *placeholder;@property (nonatomic, strong)UIColor *placeholderColor;@end

IWTextView.m文件

   ////  IWTextView.m//  ItcastWeibo////  Created by apple on 14-5-19.//  Copyright (c) 2014年 itcast. All rights reserved.//#import "IWTextView.h"@interface IWTextView()@property (nonatomic,weak) UILabel *placeholderLabel;@end@implementation IWTextView- (id)initWithFrame:(CGRect)frame{    self = [super initWithFrame:frame];    if (self) {                // 1.添加文字提示        UILabel *placeholderLabel = [[UILabel alloc] init];        placeholderLabel.textColor = [UIColor lightGrayColor];        placeholderLabel.hidden = YES;                // 文本输入框,文字提示的label背景        // placeholderLabel.backgroundColor = [UIColor orangeColor];        placeholderLabel.numberOfLines = 0;   // 文字换行        placeholderLabel.adjustsFontSizeToFitWidth = YES;                [self insertSubview:placeholderLabel atIndex:0];                // [self addSubview:placeholderLabel];        self.placeholderLabel = placeholderLabel;                // 2.******监听文字改变,是否隐藏placeholder******         [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:self];    }    return self;}-(void)setPlaceholder:(NSString *)placeholder{    _placeholder = [placeholder copy];   // 保存数据到模型        self.placeholderLabel.text = placeholder;        if(placeholder.length)    // 是否需要显示    {        self.placeholderLabel.hidden = NO;        self.placeholderLabel.frame = self.bounds;                      // 计算frame,设置文字提示“分享新鲜事...”的显示位置        CGFloat placeholderX = 5;   // 光标初始位置        CGFloat placeholderY = 8;                CGFloat maxH = self.frame.size.height;        CGFloat maxW = self.frame.size.width;        CGSize placeholderSize = [placeholder sizeWithFont:self.placeholderLabel.font constrainedToSize:CGSizeMake(maxW, maxH)];                self.placeholderLabel.frame = CGRectMake(placeholderX, placeholderY, placeholderSize.width, placeholderSize.height);                   }else{        self.placeholderLabel.hidden = YES;    }        self.placeholderLabel.hidden = (placeholder.length == 0);}// 设置文本框颜色-(void)setPlaceholderColor:(UIColor *)placeholderColor{    _placeholder = placeholderColor;    self.placeholderLabel.textColor = placeholderColor;}// 设置字体-(void)setFont:(UIFont *)font{    [super setFont:font]; // 调用系统的字体        self.placeholderLabel.font = font;    self.placeholder = self.placeholder;}-(void)textDidChange{    self.placeholderLabel.hidden = (self.text.length != 0);    }-(void)dealloc{    // [NSNotificationCenter removeObserve:self];}@end

效果:

005649_ploz_2557944.gif

转载于:https://my.oschina.net/corwien/blog/671936

你可能感兴趣的文章
C#_winform_DataGridView_的18种常见属性
查看>>
C# 扩展系统类string的方法
查看>>
webBrowser强制在本窗口打开,禁止在新窗口打开
查看>>
C#获取CPU序列号代码、硬盘ID、网卡硬件地址等类文件
查看>>
Html常用符号
查看>>
WinForm控制Webbrowser自动登录
查看>>
access表(.mdb文件) 导入 power designer
查看>>
PowerDesigner如何设计表之间的关联
查看>>
SQLite通用数据库类
查看>>
CMD下修改IP地址!
查看>>
安卓手机可以连上wifi但无法上网的解决办法
查看>>
C++程序员常用工具集
查看>>
在CSDN博客中添加量子恒道统计功能的做法
查看>>
C++调用IDL程序的做法(一)
查看>>
外部修改应用程序图标的做法
查看>>
database disk image is malformed解决方法
查看>>
有关error PRJ0003错误的思考
查看>>
实现自定义对话框程序快捷键的两种方法
查看>>
如何对抗微软霸权,google给我们上了一课
查看>>
获取windows版本信息的做法
查看>>