ios UIKit框架分析 第6天
2014-05-16 10:25
288 查看
1.UITableView
-》自定义UITableViewCell的accessory样式 消息传递的实现
默认的accessoryType属性有四种取值:UITableViewCellAccessoryNone、 UITableViewCellAccessoryDisclosureIndicator、 UITableViewCellAccessoryDetailDisclosureButton、 UITableViewCellAccessoryCheckmark。
如果想使用自定义附件按钮的其他样式,则需使用UITableView的accessoryView属性来指定
UIButton*button;
if(isEditableOrNot){
UIImage*image =[UIImage imageNamed:@"delete.png"];
button =[UIButton buttonWithType:UIButtonTypeCustom];
CGRect frame =CGRectMake(0.0,0.0,image.size.width,image.size.height);
button.frame = frame;
[button setBackgroundImage:image forState:UIControlStateNormal];
button.backgroundColor =[UIColor clearColor];
cell.accessoryView = button;
}else{
button =[UIButton buttonWithType:UIButtonTypeCustom];
button.backgroundColor =[UIColor clearColor];
cell.accessoryView = button;
}
以上代码仅仅是定义了附件按钮两种状态下的样式,问题是现在这个自定义附件按钮的事件仍不可用。
即事件还无法传递到 UITableViewDelegate的accessoryButtonTappedForRowWithIndexPath方法上。
当我们在上述代码 中在加入以下语句:
[button addTarget:self
action:@selector(btnClicked:event:) forControlEvents:UIControlEventTouchUpInside];
后, 虽然可以捕捉到每个附件按钮的点击事件,但我们还无法进行区别到底是哪一行的附件按钮发生了点击动作!因为addTarget:方法最多允许传递两个参 数:target和event,这两个参数都有各自的用途了(target指向事件委托对象,event指向所发生的事件)。看来只依靠Cocoa框架已 经无法做到了。
但我们还是可以利用event参数,在自定义的btnClicked方法中判断出事件发生在UITableView的哪一个cell上。因为UITableView有一个很关键的方法indexPathForRowAtPoint,可以根据触摸发生的位置,返回触摸发生在哪一个cell的indexPath。而且通过event对象,正好也可以获得每个触摸在视图中的位置。
// 检查用户点击按钮时的位置,并转发事件到对应的accessory tapped事件
-(void)btnClicked:(id)sender event:(id)event
{
NSSet*touches =[event allTouches];
UITouch*touch =[touches anyObject];
CGPoint currentTouchPosition =[touch locationInView:self.tableView];
NSIndexPath*indexPath =[self.tableView indexPathForRowAtPoint:currentTouchPosition];
if(indexPath !=nil)
{
[self
tableView:self.tableView accessoryButtonTappedForRowWithIndexPath:indexPath];
}
}
这样,UITableView的accessoryButtonTappedForRowWithIndexPath方法会被触发,并且获得一个indexPath参数。通过这个indexPath参数,我们即可区分到底哪一行的附件按钮发生了触摸事件。
-(void)tableView:(UITableView*)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath*)indexPath
{
//这里加入自己的逻辑
}
->uitableViewCell 构成
@property (nonatomic)BOOL showsReorderControl; // default is NO
cell.showsReorderControl =YES;
@property (nonatomic)UITableViewCellAccessoryType accessoryType;
// default is UITableViewCellAccessoryNone. use to set standard type
@property (nonatomic,retain)UIView
*accessoryView; 自定义accessoryView;
@property (nonatomic)UITableViewCellAccessoryType editingAccessoryType;
//编辑模式下的accessoryView
@property (nonatomic,retain)UIView *editingAccessoryView; 自定义
cell.editingAccessoryType =UITableViewCellAccessoryDisclosureIndicator;
2.UITextField
->限制输入长度
->限制输入类型 数字、字符
#define NUMBERSVALUE @"0123456789\n" 仅限数字输入
#define NUMBERSVALUE @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
仅限字符输入
-》-(BOOL)textField:(UITextField *)textField
shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
其中string是你输入的值,range是要替换的范围,如果返回值为NO,不替换,表现在界面中是无法输入;如果返回值是YES,替换。
3.UITextInputTraits 定制 键盘的外观以及部分特性,输入文本的属性。
4.NSAttributedString 字符串同时显示多个属性的效果 颜色 大小 字体等
-》一次增加一个属性代码如下:
-》同时增加多个属性代码如下:
5.uitextView
-》ios7中呈现的部分新特性
A ) IOS7新增加的 UITextViewDelegate 方法:
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRangeNS_AVAILABLE_IOS(7_0);
这个代理方法是当用户点击UITextView中的超链接的时候回调次方法,return YES则打开链接
B ) UITextView IOS7 中新增加属性 BOOL selectable
@property(nonatomic,getter=isSelectable)BOOL selectable NS_AVAILABLE_IOS(7_0);//
toggle selectability, which controls the ability of the user to select content and interact with URLs & attachments
用法:决定UITextView 中文本是否可以响应用户的触摸,主要指:1、文本中URL是否可以被点击;2、UIMenuItem是否可以响应
C) UITextView IOS7 中新增加属性 BOOL editable
By default, users can add, remove, or change text within a text view. (默认是YES)可以添加、移出、更改UITextView的text。
D) UITextView IOS7 中新增加属性 NSTextContainer *textContainer
意思是UITextView的文本输入容器,这个属性可以设置文本的对齐方式,布局的面积大小,折线模式,行数限制等属性;
// Set and get the text container for the text view
@property(nonatomic,readonly)NSTextContainer *textContainerNS_AVAILABLE_IOS(7_0);
E)iOS7新增加的类:NSTextContainer、NSLayoutManager、NSTextStorage及其相互关系
NSTextStorage保存并管理UITextView要展示的文字内容,该类是NSMutableAttributedString的子类,由于可以灵活地往文字添加或修改属性,所以非常适用于保存并修改文字属性。
NSLayoutManager用于管理NSTextStorage其中的文字内容的排版布局。
NSTextContainer则定义了一个矩形区域用于存放已经进行了排版并设置好属性的文字。
详情参考 /article/1999236.html
-》自定义UITableViewCell的accessory样式 消息传递的实现
默认的accessoryType属性有四种取值:UITableViewCellAccessoryNone、 UITableViewCellAccessoryDisclosureIndicator、 UITableViewCellAccessoryDetailDisclosureButton、 UITableViewCellAccessoryCheckmark。
如果想使用自定义附件按钮的其他样式,则需使用UITableView的accessoryView属性来指定
UIButton*button;
if(isEditableOrNot){
UIImage*image =[UIImage imageNamed:@"delete.png"];
button =[UIButton buttonWithType:UIButtonTypeCustom];
CGRect frame =CGRectMake(0.0,0.0,image.size.width,image.size.height);
button.frame = frame;
[button setBackgroundImage:image forState:UIControlStateNormal];
button.backgroundColor =[UIColor clearColor];
cell.accessoryView = button;
}else{
button =[UIButton buttonWithType:UIButtonTypeCustom];
button.backgroundColor =[UIColor clearColor];
cell.accessoryView = button;
}
以上代码仅仅是定义了附件按钮两种状态下的样式,问题是现在这个自定义附件按钮的事件仍不可用。
即事件还无法传递到 UITableViewDelegate的accessoryButtonTappedForRowWithIndexPath方法上。
当我们在上述代码 中在加入以下语句:
[button addTarget:self
action:@selector(btnClicked:event:) forControlEvents:UIControlEventTouchUpInside];
后, 虽然可以捕捉到每个附件按钮的点击事件,但我们还无法进行区别到底是哪一行的附件按钮发生了点击动作!因为addTarget:方法最多允许传递两个参 数:target和event,这两个参数都有各自的用途了(target指向事件委托对象,event指向所发生的事件)。看来只依靠Cocoa框架已 经无法做到了。
但我们还是可以利用event参数,在自定义的btnClicked方法中判断出事件发生在UITableView的哪一个cell上。因为UITableView有一个很关键的方法indexPathForRowAtPoint,可以根据触摸发生的位置,返回触摸发生在哪一个cell的indexPath。而且通过event对象,正好也可以获得每个触摸在视图中的位置。
// 检查用户点击按钮时的位置,并转发事件到对应的accessory tapped事件
-(void)btnClicked:(id)sender event:(id)event
{
NSSet*touches =[event allTouches];
UITouch*touch =[touches anyObject];
CGPoint currentTouchPosition =[touch locationInView:self.tableView];
NSIndexPath*indexPath =[self.tableView indexPathForRowAtPoint:currentTouchPosition];
if(indexPath !=nil)
{
[self
tableView:self.tableView accessoryButtonTappedForRowWithIndexPath:indexPath];
}
}
这样,UITableView的accessoryButtonTappedForRowWithIndexPath方法会被触发,并且获得一个indexPath参数。通过这个indexPath参数,我们即可区分到底哪一行的附件按钮发生了触摸事件。
-(void)tableView:(UITableView*)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath*)indexPath
{
//这里加入自己的逻辑
}
->uitableViewCell 构成
@property (nonatomic)BOOL showsReorderControl; // default is NO
cell.showsReorderControl =YES;
@property (nonatomic)UITableViewCellAccessoryType accessoryType;
// default is UITableViewCellAccessoryNone. use to set standard type
@property (nonatomic,retain)UIView
*accessoryView; 自定义accessoryView;
@property (nonatomic)UITableViewCellAccessoryType editingAccessoryType;
//编辑模式下的accessoryView
@property (nonatomic,retain)UIView *editingAccessoryView; 自定义
cell.editingAccessoryType =UITableViewCellAccessoryDisclosureIndicator;
2.UITextField
->限制输入长度
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{ //限制输入长度为8 BOOL flag = YES; int maxLength = 8; if (range.location >= maxLength) { flag = NO; } //检测是否输入空格 NSString *resultingString = [textField.text stringByReplacingCharactersInRange: range withString: string]; NSCharacterSet *whitespaceSet = [NSCharacterSet whitespaceCharacterSet]; if ([resultingString rangeOfCharacterFromSet:whitespaceSet].location != NSNotFound) flag = NO; return flag; }
->限制输入类型 数字、字符
#define NUMBERSVALUE @"0123456789\n" 仅限数字输入
#define NUMBERSVALUE @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
仅限字符输入
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { NSCharacterSet *cs= [[NSCharacterSet characterSetWithCharactersInString:NUMBERSVALUE] invertedSet]; NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""]; BOOL result = [string isEqualToString:filtered]; return result; }
-》-(BOOL)textField:(UITextField *)textField
shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
其中string是你输入的值,range是要替换的范围,如果返回值为NO,不替换,表现在界面中是无法输入;如果返回值是YES,替换。
3.UITextInputTraits 定制 键盘的外观以及部分特性,输入文本的属性。
4.NSAttributedString 字符串同时显示多个属性的效果 颜色 大小 字体等
-》一次增加一个属性代码如下:
NSMutableAttributedString * attStr = [[NSMutableAttributedString alloc]initWithString: @"My phone number is +8602980000000.\r\n" "My personal web site www.xxxxxx.com.\r\n" "My E-mail address is XXXXX@gmail.com.\r\n" "I was born in 1900-01-01."]; [attStr addAttribute:NSLinkAttributeName value:@"www.xxxxxx.com." range:[[attStr string] rangeOfString:@"www.xxxxxx.com"]];
-》同时增加多个属性代码如下:
NSMutableAttributedString * attStr = [[NSMutableAttributedString alloc]initWithString: @"My phone number is +8602980000000.\r\n" "My personal web site www.xxxxxx.com.\r\n" "My E-mail address is XXXXX@gmail.com.\r\n" "I was born in 1900-01-01."]; NSDictionary * atteibutes = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor redColor],NSBackgroundColorAttributeName, [UIFont systemFontOfSize:30.0f],NSFontAttributeName,nil]; NSRange range = [[attStr string] rangeOfString:@"My phone number is"]; [attStr addAttributes:atteibutes range:range];
5.uitextView
-》ios7中呈现的部分新特性
A ) IOS7新增加的 UITextViewDelegate 方法:
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRangeNS_AVAILABLE_IOS(7_0);
这个代理方法是当用户点击UITextView中的超链接的时候回调次方法,return YES则打开链接
B ) UITextView IOS7 中新增加属性 BOOL selectable
@property(nonatomic,getter=isSelectable)BOOL selectable NS_AVAILABLE_IOS(7_0);//
toggle selectability, which controls the ability of the user to select content and interact with URLs & attachments
用法:决定UITextView 中文本是否可以响应用户的触摸,主要指:1、文本中URL是否可以被点击;2、UIMenuItem是否可以响应
C) UITextView IOS7 中新增加属性 BOOL editable
By default, users can add, remove, or change text within a text view. (默认是YES)可以添加、移出、更改UITextView的text。
D) UITextView IOS7 中新增加属性 NSTextContainer *textContainer
意思是UITextView的文本输入容器,这个属性可以设置文本的对齐方式,布局的面积大小,折线模式,行数限制等属性;
// Set and get the text container for the text view
@property(nonatomic,readonly)NSTextContainer *textContainerNS_AVAILABLE_IOS(7_0);
self.text.textContainer.lineBreakMode = NSLineBreakByTruncatingTail; self.text.textContainer.size = CGSizeMake(320, 300);
E)iOS7新增加的类:NSTextContainer、NSLayoutManager、NSTextStorage及其相互关系
NSTextStorage保存并管理UITextView要展示的文字内容,该类是NSMutableAttributedString的子类,由于可以灵活地往文字添加或修改属性,所以非常适用于保存并修改文字属性。
NSLayoutManager用于管理NSTextStorage其中的文字内容的排版布局。
NSTextContainer则定义了一个矩形区域用于存放已经进行了排版并设置好属性的文字。
详情参考 /article/1999236.html
相关文章推荐
- ios UIKit框架分析 第1天
- ios UIKit框架分析 第3天
- ios UIKit框架分析 第9天
- ios UIKit框架分析 第4天
- ios UIKit框架分析 第2天
- ios UIKit框架分析 第7天
- ios UIKit框架分析 第8天
- ios UIKit框架分析 第5天
- iOS基础-UIKit框架-多控制器管理-控制器创建和控制器view的创建
- 转:iOS开发框架Cocoa Touch:UIKit
- IOS开发学习笔记(五)——多视图应用程序之框架分析
- 写iOS程序时可以不用导入Foundation和UIKit框架的原因
- iOS:UIKit框架图
- iOS基础-UIKit框架-高级视图-UIDatePicker
- iOS开发资源分享:UIKit关键帧动画框架Jazz Hands、MTLog、Pocket Weather源码以及其他
- iOS基础-UIKit框架-高级视图-UIPickerView-实例1:点菜(列与列之间无关系)
- iOS基础-UIKit框架-高级视图-UIPickerView-实例2:城市选择(列与列之间有关系)
- iOS基础-UIKit框架-手势处理
- IOS开发笔记--UIKit框架之 UIView
- IOS开发基础—关于uikit框架里面uiview类的一些知识点