iOS-UIKit框架学习—UITextView

UITextView的类实现一个滚动的,多行文本区域的行为。类支持使用自定义字体,颜色,和对齐的文本的显示,同时还支持文本编辑。通常可以使用一个文本视图,显示多行文本。

@protocol UITextViewDelegate 

@optional

// 是否开始编辑
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView;
// 是否完成编辑
- (BOOL)textViewShouldEndEditing:(UITextView *)textView;
// 开始编辑
- (void)textViewDidBeginEditing:(UITextView *)textView;
// 完成编辑
- (void)textViewDidEndEditing:(UITextView *)textView;
// 文本发生改变,是否替换成指定的文本
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;
// 文本内容发生改变
- (void)textViewDidChange:(UITextView *)textView;
// 更改选中的文本
- (void)textViewDidChangeSelection:(UITextView *)textView;
// 是否允许指定的用户使用指定范围的URL进行交互
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange interaction:(UITextItemInteraction)interaction NS_AVAILABLE_IOS(10_0);
// 是否允许指定文本附件与文本进行交互
- (BOOL)textView:(UITextView *)textView shouldInteractWithTextAttachment:(NSTextAttachment *)textAttachment inRange:(NSRange)characterRange interaction:(UITextItemInteraction)interaction NS_AVAILABLE_IOS(10_0);
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange NS_DEPRECATED_IOS(7_0, 10_0, "Use textView:shouldInteractWithURL:inRange:forInteractionType: instead");
- (BOOL)textView:(UITextView *)textView shouldInteractWithTextAttachment:(NSTextAttachment *)textAttachment inRange:(NSRange)characterRange NS_DEPRECATED_IOS(7_0, 10_0, "Use textView:shouldInteractWithTextAttachment:inRange:forInteractionType: instead");

@end

NS_CLASS_AVAILABLE_IOS(2_0) @interface UITextView : UIScrollView 
// 代理
@property(nullable,nonatomic,weak) id delegate;
// 文本内容
@property(null_resettable,nonatomic,copy) NSString *text;
// 字体
@property(nullable,nonatomic,strong) UIFont *font;
// 字体颜色
@property(nullable,nonatomic,strong) UIColor *textColor;
// 对齐方式
@property(nonatomic) NSTextAlignment textAlignment;
// 选择的范围
@property(nonatomic) NSRange selectedRange;
// 是否可编辑
@property(nonatomic,getter=isEditable) BOOL editable __TVOS_PROHIBITED;
// 是否可选
@property(nonatomic,getter=isSelectable) BOOL selectable NS_AVAILABLE_IOS(7_0);
// 自动检测文本类型,如电话号码、邮箱、网址等
@property(nonatomic) UIDataDetectorTypes dataDetectorTypes NS_AVAILABLE_IOS(3_0) __TVOS_PROHIBITED;
// 是否允许设置文本属性 默认为NO
@property(nonatomic) BOOL allowsEditingTextAttributes NS_AVAILABLE_IOS(6_0);
// 文本属性
@property(null_resettable,copy) NSAttributedString *attributedText NS_AVAILABLE_IOS(6_0);
// 属性字典
@property(nonatomic,copy) NSDictionary *typingAttributes NS_AVAILABLE_IOS(6_0);
// 设置指定可见的的滚动范围
- (void)scrollRangeToVisible:(NSRange)range;
// 自定义输入框
@property (nullable, readwrite, strong) UIView *inputView;
// 输入框的辅助视图
@property (nullable, readwrite, strong) UIView *inputAccessoryView;
// 再次编辑是否清楚文本 默认为NO
@property(nonatomic) BOOL clearsOnInsertion NS_AVAILABLE_IOS(6_0);

// 创建文本视图
- (instancetype)initWithFrame:(CGRect)frame textContainer:(nullable NSTextContainer *)textContainer NS_AVAILABLE_IOS(7_0) NS_DESIGNATED_INITIALIZER;
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder NS_DESIGNATED_INITIALIZER;

// 获取文本视图的文本容器
@property(nonatomic,readonly) NSTextContainer *textContainer NS_AVAILABLE_IOS(7_0);
// 设置文本容器内间距
@property(nonatomic, assign) UIEdgeInsets textContainerInset NS_AVAILABLE_IOS(7_0);
// 获取布局管理器
@property(nonatomic,readonly) NSLayoutManager *layoutManager NS_AVAILABLE_IOS(7_0);
// 获取文本存储对象中的文本
@property(nonatomic,readonly,strong) NSTextStorage *textStorage NS_AVAILABLE_IOS(7_0);

// link的样式属性
@property(null_resettable, nonatomic, copy) NSDictionary *linkTextAttributes NS_AVAILABLE_IOS(7_0);

@end
// 开始编辑时发送通知
UIKIT_EXTERN NSNotificationName const UITextViewTextDidBeginEditingNotification;
// 文本发生变化发送通知
UIKIT_EXTERN NSNotificationName const UITextViewTextDidChangeNotification;
// 编辑完成发送通知
UIKIT_EXTERN NSNotificationName const UITextViewTextDidEndEditingNotification;

e.g.

UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 100)];
textView.font = [UIFont systemFontOfSize:12.f];
textView.text = @"iOS-UIKit框架学习—UITextView";
textView.textColor = UIColorFromRGB(0xababab);
// UITextView在上下左右分别有一个8px的padding,设置为0后可以正常计算文本高度,否则要宽度-16
[textView setContentOffset:CGPointZero];
[self.view addSubview:textView];

// 用此方法计算高度不需要设置内容边距
CGSize sizeToFit = [textView sizeThatFits:CGSizeMake(SCREEN_WIDTH, MAXFLOAT)];

你可能感兴趣的:(iOS-UIKit框架学习—UITextView)