標籤:連結 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常用屬性方法