iOS-UI UITextField篇

官方TextField

@interface UITextField : UIControl <UITextInput, NSCoding> 

@property(nonatomic,copy)   NSString               *text;                 // 文本         default is nil
@property(nonatomic,copy)   NSAttributedString     *attributedText NS_AVAILABLE_IOS(6_0); // default is nil
@property(nonatomic,retain) UIColor                *textColor;            // 文本颜色      default is nil. use opaque black
@property(nonatomic,retain) UIFont                 *font;                 // 文本字体      default is nil. use system font 12 pt
@property(nonatomic)        NSTextAlignment         textAlignment;        // 文本的排版    default is NSLeftTextAlignment
@property(nonatomic)        UITextBorderStyle       borderStyle;          // 边框的样式    default is UITextBorderStyleNone. If set to UITextBorderStyleRoundedRect, custom background images are ignored.
@property(nonatomic,copy)   NSDictionary           *defaultTextAttributes NS_AVAILABLE_IOS(7_0); //文本属性 用字典填充 applies attributes to the full range of text. Unset attributes act like default values.

@property(nonatomic,copy)   NSString               *placeholder;          // 文本填充      default is nil. string is drawn 70% gray
@property(nonatomic,copy)   NSAttributedString     *attributedPlaceholder NS_AVAILABLE_IOS(6_0); // default is nil
@property(nonatomic)        BOOL                    clearsOnBeginEditing; // 开始编辑的时候清空  default is NO which moves cursor to location clicked. if YES, all text cleared
@property(nonatomic)        BOOL                    adjustsFontSizeToFitWidth; //自适应文本到宽度 default is NO. if YES, text will shrink to minFontSize along baseline
@property(nonatomic)        CGFloat                 minimumFontSize;      //最小的文本大小 default is 0.0. actual min may be pinned to something readable. used if adjustsFontSizeToFitWidth is YES
@property(nonatomic,assign) id<UITextFieldDelegate> delegate;             // 协议         default is nil. weak reference
@property(nonatomic,retain) UIImage                *background;           // 背景         default is nil. draw in border rect. image should be stretchable
@property(nonatomic,retain) UIImage                *disabledBackground;   // 禁用的背景    default is nil. ignored if background not set. image should be stretchable

@property(nonatomic,readonly,getter=isEditing) BOOL editing;              //是否被编辑
@property(nonatomic) BOOL allowsEditingTextAttributes NS_AVAILABLE_IOS(6_0); // default is NO. allows editing text attributes with style operations and pasting rich text
@property(nonatomic,copy) NSDictionary *typingAttributes NS_AVAILABLE_IOS(6_0); // automatically resets when the selection changes

// You can supply custom views which are displayed at the left or right
// sides of the text field. Uses for such views could be to show an icon or
// a button to operate on the text in the field in an application-defined
// manner.
//
// A very common use is to display a clear button on the right side of the
// text field, and a standard clear button is provided. Note: if the clear
// button overlaps one of the other views, the clear button will be given
// precedence.

@property(nonatomic)        UITextFieldViewMode  clearButtonMode; // sets when the clear button shows up. default is UITextFieldViewModeNever

@property(nonatomic,retain) UIView              *leftView;        // e.g. magnifying glass
@property(nonatomic)        UITextFieldViewMode  leftViewMode;    // sets when the left view shows up. default is UITextFieldViewModeNever

@property(nonatomic,retain) UIView              *rightView;       // e.g. bookmarks button
@property(nonatomic)        UITextFieldViewMode  rightViewMode;   // sets when the right view shows up. default is UITextFieldViewModeNever

// drawing and positioning overrides

- (CGRect)borderRectForBounds:(CGRect)bounds;
- (CGRect)textRectForBounds:(CGRect)bounds;
- (CGRect)placeholderRectForBounds:(CGRect)bounds;
- (CGRect)editingRectForBounds:(CGRect)bounds;
- (CGRect)clearButtonRectForBounds:(CGRect)bounds;
- (CGRect)leftViewRectForBounds:(CGRect)bounds;
- (CGRect)rightViewRectForBounds:(CGRect)bounds;

- (void)drawTextInRect:(CGRect)rect;
- (void)drawPlaceholderInRect:(CGRect)rect;

// Presented when object becomes first responder.  If set to nil, reverts to following responder chain.  If
// set while first responder, will not take effect until reloadInputViews is called.
@property (readwrite, retain) UIView *inputView;             //键盘的view
@property (readwrite, retain) UIView *inputAccessoryView;    //想在键盘上展示一个自定义的view时,你就可以设置该属性

@property(nonatomic) BOOL clearsOnInsertion NS_AVAILABLE_IOS(6_0); // defaults to NO. if YES, the selection UI is hidden, and inserting text will replace the contents of the field. changing the selection will automatically set this to NO.

@end
UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(100, 100, 200, 50)];
    textField.tag = 10;

    textField.borderStyle = UITextBorderStyleRoundedRect;//
#pragma mark - 关于文本的设置
-(void)textSetting
{
    //找到文本输入框
    UITextField *textField = (id)[self.view viewWithTag:10];
    //设置文字
//    textField.text = @"我是文本框";
    //设置文字颜色
    textField.textColor = [UIColor redColor];
    //设置文字字体
    textField.font = [UIFont boldSystemFontOfSize:25];
    //设置文字对齐方式,默认居左
    textField.textAlignment = NSTextAlignmentRight;
    /**
     NSTextAlignmentLeft
     NSTextAlignmentRight
     NSTextAlignmentCenter
     */
    //设置文字自适应宽度
    textField.adjustsFontSizeToFitWidth = YES;
    //设置允许的最小字体,在adjustsFontSizeToFitWidth = YES,才有效
    textField.minimumFontSize = 17;

    //设置提示文字
    textField.placeholder = @"请输入用户名";
    //设置是否使用密文输入
//    textField.secureTextEntry = YES;

    //设置开始编辑时,清除已有的文字
    textField.clearsOnBeginEditing = YES;
}
#pragma mark - 关于样式的设置

-(void)styleSetting
{
    //找到已经创建好的UITextField
    UITextField *textField = (UITextField *)[self.view viewWithTag:10];
    //设置背景颜色
//    textField.backgroundColor = [UIColor cyanColor];
    //设置边框样式
    textField.borderStyle = UITextBorderStyleNone;
    /**
     UITextBorderStyleNone
     无边框
     UITextBorderStyleLine
     线性矩形
     UITextBorderStyleBezel
     尖角矩形
     UITextBorderStyleRoundedRect
     圆角矩形
     */
    //通过layer设置圆角
    textField.layer.cornerRadius = 10;
    textField.layer.borderColor = [UIColor lightGrayColor].CGColor;
    textField.layer.borderWidth = 1;

    //设置是否显示清除按钮
    textField.clearButtonMode = UITextFieldViewModeUnlessEditing;
    //显示清除按钮的前提都是要有文字
    /**
     UITextFieldViewModeNever,
     //从不显示
     UITextFieldViewModeWhileEditing,
     //编辑时显示
     UITextFieldViewModeUnlessEditing,
     //非编辑时显示
     UITextFieldViewModeAlways
     //一直显示
     */

    //成为第一响应者
    //第一响应者,一个界面可能有多个可输入的控件,哪一个正在编辑哪一个就是第一响应者
    [textField becomeFirstResponder];

    //设置背景图片
    textField.background = [UIImage imageNamed:@"1.png"];
    //设置不可编辑时的背景图
    textField.disabledBackground = [UIImage imageNamed:@"5.png"];
    //设置是否可以编辑,YES 可以编辑,NO不可以
//    textField.enabled = NO;
    UIView *leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
    leftView.backgroundColor = [UIColor magentaColor];
    //设置左视图,所有直接或间接继承于UIView的类的对象,都可以作为左视图
    textField.leftView = leftView;
    //设置左视图的显示模式
    textField.leftViewMode = UITextFieldViewModeAlways;

    UIView *rightView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
    rightView.backgroundColor = [UIColor yellowColor];
    //设置右视图
    textField.rightView = rightView;
    //设置右视图的显示模式
    textField.rightViewMode = UITextFieldViewModeAlways;
}

自定制输入键盘

@protocol KeyBoardViewDelegate <NSObject>

-(void)inputText:(NSString *)text;

@end

@interface KeyBoardView : UIView

@property(nonatomic, assign) id <KeyBoardViewDelegate> delegate;

@end
@implementation KeyBoardView

-(instancetype)init
{
    self = [super init];
    if (self) {
        CGSize size = [UIScreen mainScreen].bounds.size;
        self.frame = CGRectMake(0, 0, size.width, 258);
        [self createButtons];
    }
    return self;

}

-(void)createButtons
{
    NSArray *array = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"0",@"+",@"-"];
    int i = 0;
    for (NSString *title in  array)
    {
        //遍历标题,创建按钮
        UIButton *button = [[UIButton alloc]init];
        button.backgroundColor = [UIColor lightGrayColor];
        button.frame = CGRectMake(10 + (i % 3) * 90, 10 + (i / 3) * 50, 85, 45);
        [button setTitle:title forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont boldSystemFontOfSize:30];
        [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
        i++;
    }
}

-(void)buttonClicked:(UIButton *)sender
{
    //代理存在,且能响应inputText:这个方法
    if(_delegate && [_delegate respondsToSelector:@selector(inputText:)])
    {
        [_delegate inputText:sender.titleLabel.text];
    }
}
@end
时间: 2024-08-01 22:43:01

iOS-UI UITextField篇的相关文章

iOS开发网络篇—NSURLConnection基本使用

iOS开发网络篇—NSURLConnection基本使用 一.NSURLConnection的常用类 (1)NSURL:请求地址 (2)NSURLRequest:封装一个请求,保存发给服务器的全部数据,包括一个NSURL对象,请求方法.请求头.请求体.... (3)NSMutableURLRequest:NSURLRequest的子类 (4)NSURLConnection:负责发送请求,建立客户端和服务器的连接.发送NSURLRequest的数据给服务器,并收集来自服务器的响应数据 二.NSUR

iOS开发项目篇—28自定义UITextView

iOS开发项目篇—28自定义UITextView 一.简单说明 1.要实现的效果 2.分析 (1)UITextField 1.最多只能输入一行文字 2.能设置提醒文字(placehoder) 3.不具备滚动功能 (2)UITextView 1.能输入N行文字(N>0) 2.不能设置提醒文字(没有placehoder属性) 3.具备滚动功能 需求:技能输入多行文字,又具备文字提醒功能. 这里选择自定义一个类,让其继承自UITextView类,为其添加一个设置文字提醒的功能. 二.实现 自定义UI控

iOS开发项目篇—12搜索框的封装

iOS开发项目篇—12搜索框的封装 一.在“发现”导航栏中添加搜索框 1.实现代码 1 #import "YYDiscoverViewController.h" 2 3 @interface YYDiscoverViewController () 4 5 @end 6 7 @implementation YYDiscoverViewController 8 9 - (void)viewDidLoad 10 { 11 [super viewDidLoad]; 12 13 //添加搜索框

iOS开发项目篇—10对齐方式

iOS开发项目篇—10对齐方式 一.关于四个容易混淆属性的对比说明 1. textAligment : 文字的水平方向的对齐方式 (1) 取值 NSTextAlignmentLeft      = 0,    // 左对齐 NSTextAlignmentCenter    = 1,    // 居中对齐 NSTextAlignmentRight    = 2,    // 右对齐 (2) 哪些控件有这个属性 : 一般能够显示文字的控件都有这个属性 * UITextField * UILabel

iOS GCD中级篇 - dispatch_group的理解及使用

前文我们讲了GCD基础篇,以及同步.异步,并发.并行几个概率的理解. 参考链接: iOS GCD基础篇 - 同步.异步,并发.并行的理解 现在讲一下dispatch_group的概念以及几种场景下的使用 1.关于dispatch_group 把一组任务提交到队列中,这些队列可以不相关,然后监听这组任务完成的事件. 最常见的几个方法: 1.dispatch_group_create创建一个调度任务组 2.dispatch_group_async 把一个任务异步提交到任务组里 3.dispatch_

iOS开发网络篇—网络编程基础

iOS开发网络篇—网络编程基础 一.为什么要学习网络编程 1.简单说明 在移动互联网时代,移动应用的特征有: (1)几乎所有应用都需要用到网络,比如QQ.微博.网易新闻.优酷.百度地图 (2)只有通过网络跟外界进行数据交互.数据更新,应用才能保持新鲜.活力 (3)如果没有了网络,也就缺少了数据变化,无论外观多么华丽,终将变成一潭死水 移动网络应用 = 良好的UI + 良好的用户体验 + 实时更新的数据 新闻:网易新闻.新浪新闻.搜狐新闻.腾讯新闻 视频:优酷.百度视频.搜狐视频.爱奇艺视频 音乐

iOS开发拓展篇—CoreLocation地理编码

iOS开发拓展篇—CoreLocation地理编码 一.简单说明 CLGeocoder:地理编码器,其中Geo是地理的英文单词Geography的简写. 1.使用CLGeocoder可以完成“地理编码”和“反地理编码” 地理编码:根据给定的地名,获得具体的位置信息(比如经纬度.地址的全称等) 反地理编码:根据给定的经纬度,获得具体的位置信息 (1)地理编码方法 - (void)geocodeAddressString:(NSString *)addressString completionHan

IOS的UITextField,UIButton,UIWebView的一些属性介绍和IOS图片资源的使用技巧

有时候UI给开发的资源跟实际的frame不一致,这个时候我们就要去拉伸图片 UIImage* image = [[UIImage imageNamed:@"text_field_bg.png"] stretchableImageWithLeftCapWidth:20 topCapHeight:0]; //stretchableImageWithLeftCapWidth使图片有拉伸效果 UITextField的属性介绍: UITextField* field = [[UITextFiel

iOS开发多线程篇—自定义NSOperation

iOS开发多线程篇—自定义NSOperation 一.实现一个简单的tableView显示效果 实现效果展示: 代码示例(使用以前在主控制器中进行业务处理的方式) 1.新建一个项目,让控制器继承自UITableViewController. 1 // 2 // YYViewController.h 3 // 01-自定义Operation 4 // 5 // Created by apple on 14-6-26. 6 // Copyright (c) 2014年 itcase. All rig

iOS开发多线程篇—多线程简单介绍

iOS开发多线程篇—多线程简单介绍 一.进程和线程 1.什么是进程 进程是指在系统中正在运行的一个应用程序 每个进程之间是独立的,每个进程均运行在其专用且受保护的内存空间内 比如同时打开QQ.Xcode,系统就会分别启动2个进程 通过“活动监视器”可以查看Mac系统中所开启的进程 2.什么是线程 1个进程要想执行任务,必须得有线程(每1个进程至少要有1条线程) 线程是进程的基本执行单元,一个进程(程序)的所有任务都在线程中执行 比如使用酷狗播放音乐.使用迅雷下载电影,都需要在线程中执行 3.线程