您的位置:首页 > 产品设计 > UI/UE

UITextView

2015-12-04 09:02 666 查看
1、基本用法:显示文本信息(不可编辑)

UITextView * textView = [[UITextView alloc] init];
textView.frame = self.view.bounds;
textView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
textView.editable = NO;//默认可编辑,设置为不可编辑
textView.textColor = [UIColor orangeColor];
textView.backgroundColor = [UIColor lightGrayColor];
textView.font = [UIFont systemFontOfSize:20];
NSMutableString * str = [[NSMutableString alloc] init];
for (int i = 0; i < 100; i++) {
[str appendString:[NSString stringWithFormat:@"这是第%i行\n", i]];
}
textView.text = str;
[self.view addSubview:textView];如果文本太多,超出屏幕,则会有滚动条:



2、可编辑状态

_textView.editable = YES;

当出现键盘时,会遮挡最下面部分:



对于这种,应该在键盘跳出和隐藏的时候,适当调整UITextView的尺寸,具体做法如下:

#import "ViewController.h"

@interface ViewController () <UITextViewDelegate>
{
UITextView * _textView;
}
@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = [UIColor whiteColor];

_textView = [[UITextView alloc] init];
_textView.frame = self.view.bounds;
_textView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
_textView.editable = YES;//设置是否可编辑
_textView.textColor = [UIColor orangeColor];
_textView.backgroundColor = [UIColor lightGrayColor];
_textView.font = [UIFont systemFontOfSize:20];
_textView.delegate = self;
NSMutableString * str = [[NSMutableString alloc] init];
for (int i = 0; i <= 100; i++) {
[str appendString:[NSString stringWithFormat:@"这是第%i行\n", i]];
}
NSRange range = NSMakeRange(str.length - 2, 2);
//去掉最后一行后面的\n,否则会空一行
[str deleteCharactersInRange:range];

_textView.text = str;
[self.view addSubview:_textView];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardShow:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardHide:) name:UIKeyboardDidHideNotification object:nil];

}

- (void) keyBoardShow:(NSNotification *) notification {
NSDictionary* info = [notification userInfo];
//kbSize即為鍵盤尺寸 (有width, height)
CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;//得到鍵盤的高度
NSLog(@"keyboard_height:%f",kbSize.height);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
CGRect textViewFrame = _textView.frame;
CGRect newTextViewFrame = CGRectMake(textViewFrame.origin.x, textViewFrame.origin.y, textViewFrame.size.width, textViewFrame.size.height - kbSize.height);
_textView.frame = newTextViewFrame;
[UIView commitAnimations];
}
- (void) keyBoardHide:(NSNotification *) notification {

}

- (void)textViewDidBeginEditing:(UITextView *)textView {
//或者在此处改变UITextView的frame
}

3、

- (BOOL)hasText;
判断有无文本

设置文本对齐方式

@property(nonatomic) NSTextAlignment textAlignment; // default is NSLeftTextAlignment
常见的有这三种:(居左,居中,居右)
NSTextAlignmentLeft
NSTextAlignmentCenter
NSTextAlignmentRight

取得文本的选择范围:

- (void)textViewDidChangeSelection:(UITextView *)textView {
//此函数实时监测文本的选择情况
 NSLog(@"%@", NSStringFromRange(textView.selectedRange));
}


2015-12-04 10:03:55.778 07-UITextView[1218:27730] {1, 8}


滚动位置控制:

[_textView scrollRangeToVisible:NSMakeRange(1, 1)];
执行此代码后,将立即滚动到NSRange所包含的字符的位置

4、URL与电话号码的链接显示

typedef NS_OPTIONS(NSUInteger, UIDataDetectorTypes) {
//自动检测电话号码
 UIDataDetectorTypePhoneNumber = 1 << 0, // Phone number detection

//自动检测超链接
UIDataDetectorTypeLink = 1 << 1, // URL detection

//自动检测地址
UIDataDetectorTypeAddress NS_ENUM_AVAILABLE_IOS(4_0) = 1 << 2, // Street address detection

//自动检测日期
UIDataDetectorTypeCalendarEvent NS_ENUM_AVAILABLE_IOS(4_0) = 1 << 3, // Event detection

UIDataDetectorTypeNone = 0, // No detection at all

//自动检测全部
UIDataDetectorTypeAll = NSUIntegerMax // All types
};
首先:

_textView.editable = NO;然后:
_textView.dataDetectorTypes = UIDataDetectorTypeAll;


点击网址:



5、 UITextView状态监视:

@protocol UITextViewDelegate <NSObject, UIScrollViewDelegate>

@optional

//编辑开始前被调用,如果返回NO,编辑将不会开始
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView;

//编辑结束前被调用,如果返回NO,编辑将不会结束
- (BOOL)textViewShouldEndEditing:(UITextView *)textView;

//编辑开始后被调用(UITextView变为第一响应者时调用)
- (void)textViewDidBeginEditing:(UITextView *)textView;

//编辑结束后被调用(UITextView失去第一响应者时被调用)
- (void)textViewDidEndEditing:(UITextView *)textView;

//改变文本前被调用,range中是变化范围,text中是变化后的字符串,如果返回NO,变化将不会被响应
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;

//文本变更时被调用
- (void)textViewDidChange:(UITextView *)textView;

//游标移动,选择范围发生变化是被调用
- (void)textViewDidChangeSelection:(UITextView *)textView;

//点击网址时被调用
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange NS_AVAILABLE_IOS(7_0);

- (BOOL)textView:(UITextView *)textView shouldInteractWithTextAttachment:(NSTextAttachment *)textAttachment inRange:(NSRange)characterRange NS_AVAILABLE_IOS(7_0);

@end

6、图文混排
CGRect rect = CGRectMake(100, 100, 100, 100);

UIView * view1 = [[UIView alloc] initWithFrame:rect];
view1.backgroundColor = [UIColor redColor];
[self.view addSubview:view1];
//设置环绕的路径
UIBezierPath * path = [UIBezierPath bezierPathWithRect:rect];

_textView.textContainer.exclusionPaths = @[path];


红色区域可以放置图片,或者其他UIView
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  UITextView