iOS控件之UITextView

父類

繼承于UIScrollView,所以它具有UIScrollView的屬性和方法。
繼承于UIScrollView的相關屬性和方法以下不再贅述請參見:iOS控件之UIScrollView

創(chuàng)建

UITextView * textView = [[UITextView alloc] init];
UITextView * textView = [[UITextView alloc] initWithFrame:CGRectMake(20, 80, 300, 200)];
UITextView * textView = [[UITextView alloc] initWithFrame:CGRectMake(20, 80, 300, 200) textContainer:container]; 

屬性

  • 內容
textView.text = @"這是一段文字,這是一段文字,這是一段文字,這是一段文字,這是一段文字,這是一段文字,這是一段文字,這是一段文字,這是一段文字,這是一段文字,這是一段文字。\n這是一段文字,這是一段文字,這是一段文字,這是一段文字,這是一段文字,這是一段文字,這是一段文字,這是一段文字,這是一段文字,這是一段文字,這是一段文字。";
  • 文字顏色
textView.textColor = [UIColor blackColor];
  • 字體
textView.font = [UIFont systemFontOfSize:18.f];
  • 對齊方式
textView.textAlignment = NSTextAlignmentCenter;
typedef NS_ENUM(NSInteger, NSTextAlignment) {
    NSTextAlignmentLeft      = 0,    // 左對齊
#if TARGET_OS_IPHONE
    NSTextAlignmentCenter    = 1,    // 居中對齊
    NSTextAlignmentRight     = 2,    // 右對齊
#else /* !TARGET_OS_IPHONE */
    NSTextAlignmentRight     = 1,
    NSTextAlignmentCenter    = 2,
#endif
    NSTextAlignmentJustified = 3,    // 兩端對齊
    NSTextAlignmentNatural   = 4,    // 根據(jù)現(xiàn)實的文字特性對齊
} NS_ENUM_AVAILABLE_IOS(6_0);
  • 是否可以編輯
textView.editable = NO; // 默認YES 
  • 是否可以選中
textView.selectable = NO; // 默認YES 當設置為NO時,不能選擇
  • 選中范圍
textView.selectedRange = NSMakeRange(8, 6);
  • 富文本
NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithString:@"這是一個富文本"];
[attrStr addAttribute:NSFontAttributeName
                    value:[UIFont systemFontOfSize:30.0f]
                    range:NSMakeRange(4, 3)];

textView.attributedText = attrStr;
// 是否允許改變文本屬性字典
textView.allowsEditingTextAttributes = NO;
NSMutableDictionary * attributesDic = [textView.typingAttributes mutableCopy];
[attributesDic setObject:[UIColor redColor] forKey:NSForegroundColorAttributeName];
// automatically resets when the selection changes
// 重新設置 接下來改變的文字 的屬性字典
textView.typingAttributes = attributesDic;
/*一般在一些代理函數(shù)中使用,比如當編輯狀態(tài)的變化*/


關于富文本的知識請看iOS富文本字符串AttributedString詳解

  • 輸入視圖
// 試著改變view的frame,發(fā)現(xiàn)只有height值會對視圖有影響,只會改變附加視圖的高度
UIView * view = [[UIView alloc] initWithFrame:CGRectMake(100, 50, 100, 100)];
view.backgroundColor = [UIColor redColor];
// 不彈出鍵盤,彈出添加的這個視圖,一般用作像銀行app的自定義鍵盤
textView.inputView = view;
  • 輸入鍵盤附加視圖
UIView * view = [[UIView alloc] initWithFrame:CGRectMake(100, 50, 100, 50)];
view.backgroundColor = [UIColor redColor];
// 在鍵盤上附加一個視圖,一般用于添加一個收回鍵盤的按鈕
textView.inputAccessoryView = view;
  • 獲得焦點后選中現(xiàn)有文本,輸入內容時清除當前選中文本
textView.clearsOnInsertion = YES; // 默認為NO
  • 文本內容與邊界的間距
textView.textContainerInset = UIEdgeInsetsMake(20, 20, 20, 20);
  • 鏈接文本的樣式設置
/*在接下來的應用中會介紹*/
@property(null_resettable, nonatomic, copy) NSDictionary<NSString *, id> *linkTextAttributes NS_AVAILABLE_IOS(7_0);
  • 只讀屬性
    有時間會專門來說這三個屬性......,會將鏈接帖到這兒
// Get the text container for the text view
@property(nonatomic,readonly) NSTextContainer *textContainer NS_AVAILABLE_IOS(7_0);
// Convenience accessors (access through the text container)
@property(nonatomic,readonly) NSLayoutManager *layoutManager NS_AVAILABLE_IOS(7_0);
@property(nonatomic,readonly,strong) NSTextStorage *textStorage NS_AVAILABLE_IOS(7_0);

方法

  • 滾動到文本的某個段落
[textView scrollRangeToVisible:NSMakeRange(50, 5)];

代理函數(shù)

// 將要開始編輯
- (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;
// 文本發(fā)生改變
- (void)textViewDidChange:(UITextView *)textView;
// 焦點發(fā)生改變
- (void)textViewDidChangeSelection:(UITextView *)textView;

// 是否允許對文本中的URL進行操作
- (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);

通知

// 在程序中添加以下通知就可以獲得相應的狀態(tài)事件

// 開始編輯的通知
UIKIT_EXTERN NSString * const UITextViewTextDidBeginEditingNotification;
// 文本發(fā)生變化的通知
UIKIT_EXTERN NSString * const UITextViewTextDidChangeNotification;
// 編輯結束的通知
UIKIT_EXTERN NSString * const UITextViewTextDidEndEditingNotification;

應用

  • 設置鏈接樣式
UITextView * textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 100, 300, 50)];

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"這是一個鏈接:www.123456.com"];
[attributedString addAttribute:NSLinkAttributeName
                         value:@"url1://www.baidu.com"
                         range:NSMakeRange(7, 14)];
    
    
NSDictionary *linkAttributes = @{NSForegroundColorAttributeName: [UIColor greenColor],
                                  NSUnderlineColorAttributeName: [UIColor lightGrayColor],
                                  NSUnderlineStyleAttributeName: @(NSUnderlinePatternSolid)};
    

textView.linkTextAttributes = linkAttributes;
textView.attributedText     = attributedString;
textView.delegate           = self;
textView.editable           = NO; // 可編輯狀態(tài)不能點擊鏈接
[self.view addSubview:textView];
// 要實現(xiàn)代理
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange {
    if ([[URL scheme] isEqualToString:@"url1"]) {
        NSString * url = [URL host];
        
        NSLog(@"%@",url);
        
        // 在這里利用url做點什么事情......

        return NO;
    }
    return YES;
}

版權聲明:出自MajorLMJ技術博客的原創(chuàng)作品 ,轉載時必須注明出處及相應鏈接!

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • 父類 繼承于UIScrollView,所以它具有UIScrollView的屬性和方法。 繼承于UIScrollVi...
    炸街程序猿閱讀 1,886評論 0 2
  • 翻譯自“Collection View Programming Guide for iOS” 0 關于iOS集合視...
    lakerszhy閱讀 4,060評論 1 22
  • 我見過流星我許了愿. 我拔過睫毛我許了愿. 我望過月亮我許了愿. 我折過紙船我許了愿. 我聽過飛機我許了愿. 我吃...
    鄧斯文閱讀 183評論 0 0
  • 感恩佛法讓我精進。 感恩清晨的陽光讓我感受美好的一天。 感恩母親無私奉獻,支持鼓勵。 感恩同事們對我支持鼓勵理解寬...
    supersunsh_39e8閱讀 182評論 0 0
  • 文:不會說謊的偽裝者 1 心靈的成熟過程 是持續(xù)不斷的自我發(fā)現(xiàn)、自我探尋的過程 除非我們先了解自己 否則我們很難去...
    啞白先生閱讀 472評論 0 0

友情鏈接更多精彩內容