manifestplaceholders的文字会被打印么

IOS_改变UITextField placeHolder颜色、字体
我们有时需要定制化UITextField对象的风格,可以添加许多不同的重写方法,来改变文本字段的显示行为。这些方法都会返回一个CGRect结构,制定了文本字段每个部件的边界范围,甚至修改placeHolder颜色,字体。
– textRectForBounds:  
//重写来重置文字区域
– drawTextInRect:  
//改变绘文字属性.重写时调用super可以按默认图形属性绘制,若自己完全重写绘制函数,就不用调用super了.
– placeholderRectForBounds:  //重写来重置占位符区域
– drawPlaceholderInRect:  //重写改变绘制占位符属性.重写时调用super可以按默认图形属性绘制,若自己完全重写绘制函数,就不用调用super了
– borderRectForBounds:  //重写来重置边缘区域
– editingRectForBounds:  //重写来重置编辑区域
– clearButtonRectForBounds:  //重写来重置clearButton位置,改变size可能导致button的图片失真
– leftViewRectForBounds:
– rightViewRectForBounds:
通过– drawPlaceholderInRect:方法可改变placeHolder颜色、字体,请看代码:
首先定义一个类CustomTextField让它继承UITextField实现以下方法即可:
//控制清除按钮的位置
-(CGRect)clearButtonRectForBounds:(CGRect)bounds
return CGRectMake(bounds.origin.x +
bounds.size.width - 50, bounds.origin.y +
bounds.size.height -20, 16, 16);
//控制placeHolder的位置,左右缩20
-(CGRect)placeholderRectForBounds:(CGRect)bounds
//return CGRectInset(bounds, 20, 0);
CGRect inset = CGRectMake(bounds.origin.x+100,
bounds.origin.y, bounds.size.width -10,
bounds.size.height);//更好理解些
//控制显示文本的位置
-(CGRect)textRectForBounds:(CGRect)bounds
//return CGRectInset(bounds, 50, 0);
CGRect inset = CGRectMake(bounds.origin.x+190,
bounds.origin.y, bounds.size.width -10,
bounds.size.height);//更好理解些
//控制编辑文本的位置
-(CGRect)editingRectForBounds:(CGRect)bounds
//return CGRectInset( bounds, 10 , 0 );
CGRect inset = CGRectMake(bounds.origin.x +10,
bounds.origin.y, bounds.size.width -10,
bounds.size.height);
//控制左视图位置
- (CGRect)leftViewRectForBounds:(CGRect)bounds
CGRect inset = CGRectMake(bounds.origin.x +10,
bounds.origin.y, bounds.size.width-250,
bounds.size.height);
//return CGRectInset(bounds,50,0);
//控制placeHolder的颜色、字体
- (void)drawPlaceholderInRect:(CGRect)rect
//CGContextRef context = UIGraphicsGetCurrentContext();
//CGContextSetFillColorWithColor(context, [UIColor yellowColor].CGColor);
[[UIColororangeColor] setFill];
[[selfplaceholder] drawInRect:rectwithFont:[UIFontsystemFontOfSize:20]];
//下面是使用CustomTextField的代码,可放在viewDidLoad等方法中
_textField = [[CustomTextField alloc] initWithFrame:CGRectMake(20, 150, 280, 30)];
_textField.placeholder = @"请输入帐号信息";
_textField.borderStyle = UITextBorderStyleRoundedR
_textField.textAlignment = UITextAlignmentL
_textField.delegate =
_textField.clearButtonMode = UITextFieldViewModeWhileE
_textField.text = @"aa";
UIImageView *imgv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon-iwant-2.png"]];
_textField.leftView =
_textField.leftViewMode = UITextFieldViewModeAlways;
[self.view addSubview:_textField];_rightTF.placeholder = @&请输入注册时的手机号码&;
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
dict[NSForegroundColorAttributeName] = [UIColor colorWithRed:25/255.0 green:158/255.0 blue:253/255.0 alpha:0.4];
NSAttributedString *attr = [[NSAttributedString alloc] initWithString:_rightTF.placeholder attributes:dict];
[_rightTF setAttributedPlaceholder: attr];
[_rightTF setValue:[UIFont boldSystemFontOfSize:15 * HomePageDefineWidth] forKeyPath:@&_placeholderLabel.font&];
本文已收录于以下专栏:
相关文章推荐
/rentiansheng/archive//1940356.html  ///         /// 打印的按钮   &#...
一、设置字体大小1、打开 File–&Settings(快捷键 Ctrl+Alt+S) 打开设置面板,默认为白色的主题,如下图所示:
2、选择“Save as”,自定义输入一个方案的名字;
原文:/zhcw/archive//2269083.html
窗体中填加控件:按钮、printDocument、printPr...
User select font-face/color/size/backgroundColor设置 字体,颜色,大小,背景色兼容主流浏览器
他的最新文章
讲师:汪剑
讲师:刘道宽
您举报文章:
举报原因:
原文地址:
原因补充:
(最多只允许输入30个字)HTML &textarea& 标签的 placeholder 属性手把手教你如何给TextView添加placeholder属性 - 简书
手把手教你如何给TextView添加placeholder属性
UITextField 有个系统自带的文本提示文字也就是placeholder属性,大家应该都知道,最常见的就用在搜索框上面的提醒文字。但是UITextField因为只能输入一行文字的原因,可能有时候不能满足我们的需求,这时候 UITextView 这个控件就派到用场了。但是UITextView虽然支持输入多行文字,却没有像 UITextField那样有个placeholder属性给我们调用,本文就是教大家如何给
UITextView 增加一个 placeholder属性。废话不多少,我们开始:
虽然这个号早就不维护了,但是好多人有问题,所有又跑上来更新下!
时光飞逝啊,当初写下这篇文章的日子是 ` 11:50`,想想现在自己还在 iOS 开发这条路上辛苦的爬着,废话不多说,有兴趣的去看看下面这个地址的 demo 吧:
UITextField 有个系统自带的文本提示文字也就是placeholder属性,大家应该都知道,最常见的就用在搜索框上面的提醒文字。但是UITextField因为只能输入一行文字的原因,可能有时候不能满足我们的需求,这时候 UITextView 这个控件就派到用场了。但是UITextView虽然支持输入多行文字,却没有像 UITextField那样有个placeholder属性给我们调用,本文就是教大家如何给
UITextView 增加一个 placeholder属性。废话不多少,我们开始:(一)我们先新建一个继承自 UITextView 的 自定义控件 JSTextView。
然后在 - (instancetype)initWithFrame:(CGRect)frame 的里面初始化一个 UILabel。没错,我的思路就是用一个 UILabel 来实现 placeholder 效果。具体代码如下:JSTextView.h 文件这里我们暴露出 一个文字和文字颜色属性,能够方便我们使用的时候设置这些属性!
#import &UIKit/UIKit.h&
@interface JSTextView :UITextView
@property(nonatomic,copy) NSString *myP
@property(nonatomic,strong) UIColor *myPlaceholderC //文字颜色
JSTextView.m 文件
#import"JSTextView.h"
@interface JSTextView()
@property (nonatomic,weak) UILabel *placeholderL //这里先拿出这个label以方便我们后面的使用
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if(self) {
self.backgroundColor= [UIColor clearColor];
UILabel *placeholderLabel = [[UILabel alloc]init];//添加一个占位label
placeholderLabel.backgroundColor= [UIColor clearColor];
placeholderLabel.numberOfLines=0; //设置可以输入多行文字时可以自动换行
[self addSubview:placeholderLabel];
self.placeholderLabel= placeholderL //赋值保存
self.myPlaceholderColor= [UIColor lightGrayColor]; //设置占位文字默认颜色
self.font= [UIFont systemFontOfSize:15]; //设置默认的字体
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:self]; //通知:监听文字的改变
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:self]; //通知:监听文字的改变这里我来解释下: 这个UITextViewTextDidChangeNotification通知会监听UITextView文字的改变,也就是说只要文字一改变就会调用这个通知的方法,利用这个我们可以控制
我们刚才在- (instancetype)initWithFrame:(CGRect)frame
方法里面初始化的
UILabel 的显示和隐藏:
#pragma mark -监听文字改变
- (void)textDidChange {
self.placeholderLabel.hidden = self.hasT
注:这个 hasText
是一个 系统的 BOOL
属性,如果 UITextView 输入了文字
hasText 就是 YES,反之就为 NO。(三)我们在
- (void)layoutSubviews 方法里面设置 UILabel 的 frame.
- (void)layoutSubviews {
[super layoutSubviews];
self.placeholderLabel.y=8; //设置UILabel 的 y值
self.placeholderLabel.x=5;//设置 UILabel 的 x 值
self.placeholderLabel.width=self.width-self.placeholderLabel.x*2.0; //设置 UILabel 的 x
//根据文字计算高度
CGSize maxSize =CGSizeMake(self.placeholderLabel.width,MAXFLOAT);
self.placeholderLabel.height= [self.myPlaceholder boundingRectWithSize:maxSize options:NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : self.placeholderLabel.font} context:nil].size.
注:- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:(NSDictionary*)attributes context:(NSStringDrawingContext*)contextNS_AVAILABLE_IOS(7_0); 这个方法我来解释下:以前我们想要获得 UILabel 文字的大小都用的下图的一系列接口:但是 这些方法已经被苹果遗弃了,并且出现了新的方法,也就是上面的那个方法。
(1)size参数: 宽高限制,用于计算文本绘制时占据的矩形块 。(2)options参数:
大家可以看看这个 官方介绍。在多行的情况下 至少要包含 NSStringDrawingUsesLineFragmentOrigin NSStringDrawingUsesFontLeading
这两个属性。
(3) attributes参数: 这是个字典
@{NSFontAttributeName : self.placeholderLabel.font}
这里注意要成对的传。(4)context参数 : 可以为 nil 。(四)
重写我们 暴露出来的 文字 ,颜色以及系统的font 的 set方法 :
- (void)setMyPlaceholder:(NSString*)myPlaceholder{
_myPlaceholder= [myPlaceholder copy];
//设置文字
self.placeholderLabel.text= myP
//重新计算子控件frame
[self setNeedsLayout];
- (void)setMyPlaceholderColor:(UIColor*)myPlaceholderColor{
_myPlaceholderColor= myPlaceholderC
//设置颜色
self.placeholderLabel.textColor= myPlaceholderC
//重写这个set方法保持font一致
- (void)setFont:(UIFont*)font {
[super setFont:font];
self.placeholderLabel.font=
//重新计算子控件frame
[self setNeedsLayout];
(五) 重写 - (void)setText:(NSString*)text
以及 - (void)setAttributedText:(NSAttributedString*)attributedText 方法来控制 UILabel 的显示 和 隐藏
- (void)setText:(NSString*)text{
[super setText:text];
[self textDidChange]; //这里调用的就是 UITextViewTextDidChangeNotification 通知的回调
- (void)setAttributedText:(NSAttributedString*)attributedText {
[super setAttributedText:attributedText];
[self textDidChange]; //这里调用的就是UITextViewTextDidChangeNotification 通知的回调
(六) 最后别忘了销毁 通知
- (void)dealloc{
[[NSNotificationCenter defaultCenter]removeObserver:UITextViewTextDidChangeNotification];
(7) 如何使用
- (void)setUpTextView {
JSTextView *textView = [[JSTextView alloc]initWithFrame:self.view.bounds];
[self.view addSubview:textView];
//1.设置提醒文字
textView.myPlaceholder=@"分享新鲜事...";
//2.设置提醒文字颜色
textView.myPlaceholderColor= [UIColorlightGrayColor];
随手记下身边精彩的瞬间~}

我要回帖

更多关于 input holdplace 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信