iOS開發UItextview常用屬性方法

來源:互聯網
上載者:User

標籤:連結   ict   rac   editing   全選   import   添加   ati   war   

//

//  ViewController.m

//  TextViewAll

#import "ViewController.h"

 

@interface ViewController ()<UITextViewDelegate>

 

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor yellowColor];

    

    UITextView *myTextView = [[UITextView alloc]initWithFrame:CGRectMake(10, 50, [UIScreen mainScreen].bounds.size.width - 20, 200)];

    //設定背景色

    myTextView.backgroundColor = [UIColor brownColor];

    //設定初始文本

    myTextView.text = @"生活在於折騰,生活在於折騰,生活在於折騰,生活在於折騰,生活在於折騰  www.baidu.com";

    //設定文字大小

    myTextView.font = [UIFont systemFontOfSize:15.0];

//    myTextView.textAlignment = 1;

    //    NSTextAlignmentLeft      = 0,    // 靠左對齊

    //    NSTextAlignmentCenter    = 1,    // 置中對齊

    //    NSTextAlignmentRight     = 2,    // 靠右對齊

    

    //是否可以編輯

    myTextView.editable = YES; // 預設YES

 

    myTextView.selectable = YES; // 預設YES 當設定為NO時,不能選擇

    

    //替換鍵盤,常用語自訂鍵盤

    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(100, 50, 100, 100)];

    view.backgroundColor = [UIColor redColor];

//    myTextView.inputView = view;

    

    //在鍵盤上面添加一個緊貼著鍵盤的view,常用於 確定 OR  取消  按鈕

    UIView * viewSecond = [[UIView alloc] initWithFrame:CGRectMake(100, 50, 100, 50)];

    viewSecond.backgroundColor = [UIColor cyanColor];

//    myTextView.inputAccessoryView = viewSecond;

    //擷取焦點後,自動全選常值內容

//    myTextView.clearsOnInsertion = YES; // 預設為NO

    //文本與邊框的距離(上,左,下右)

    myTextView.textContainerInset = UIEdgeInsetsMake(20, 0, 50, 100);

    

    myTextView.dataDetectorTypes = UIDataDetectorTypeAll;

    /*UIDataDetectorTypeAll可以檢測檢測電話、網址和郵箱。合格文本中的內容就會高亮

    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

     */

    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)};

    myTextView.linkTextAttributes = linkAttributes;

    myTextView.attributedText     = attributedString;

    myTextView.delegate           = self;

    myTextView.editable           = NO; // 可編輯狀態不能點選連結

    [self.view addSubview:myTextView];

    

}

// 要實現代理

- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange {

    if ([[URL scheme] isEqualToString:@"url1"]) {

        NSString * url = [URL host];

        NSLog(@"%@",url);

        return NO;

    }

    return YES;

}

// 將要開始編輯

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView

{

    return YES;

}

// 將要結束編輯

- (BOOL)textViewShouldEndEditing:(UITextView *)textView

{

    return YES;

}

 

// 開始編輯

- (void)textViewDidBeginEditing:(UITextView *)textView

{

    

}

// 結束編輯

- (void)textViewDidEndEditing:(UITextView *)textView

{

    

}

 

// 文本將要改變

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text

{

    return YES;

}

// 文本發生改變

- (void)textViewDidChange:(UITextView *)textView

{

    

}

// 焦點發生改變

- (void)textViewDidChangeSelection:(UITextView *)textView

{

    

}

// 是否允許對文本中的富文本進行操作

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

    return  YES;

}

 

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

 

@end

 

iOS開發UItextview常用屬性方法

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.