iOS控件之UITextView

父類(lèi)

繼承于UIScrollView,所以它具有UIScrollView的屬性和方法。

繼承于UIScrollView的相關(guān)屬性和方法以下不再贅述請(qǐng)參見(jiàn):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];

屬性

內(nèi)容

textView.text = @"這是一段文字,這是一段文字,這是一段文字,這是一段文字,這是一段文字,這是一段文字,這是一段文字,這是一段文字,這是一段文字,這是一段文字,這是一段文字。\n這是一段文字,這是一段文字,這是一段文字,這是一段文字,這是一段文字,這是一段文字,這是一段文字,這是一段文字,這是一段文字,這是一段文字,這是一段文字。";

文字顏色

textView.textColor = [UIColor blackColor];

字體

textView.font = [UIFont systemFontOfSize:18.f];

對(duì)齊方式

textView.textAlignment = NSTextAlignmentCenter;

typedef NS_ENUM(NSInteger, NSTextAlignment) {

NSTextAlignmentLeft? ? ? = 0,? ? // 左對(duì)齊

#if TARGET_OS_IPHONE

NSTextAlignmentCenter? ? = 1,? ? // 居中對(duì)齊

NSTextAlignmentRight? ? = 2,? ? // 右對(duì)齊

#else /* !TARGET_OS_IPHONE */

NSTextAlignmentRight? ? = 1,

NSTextAlignmentCenter? ? = 2,

#endif

NSTextAlignmentJustified = 3,? ? // 兩端對(duì)齊

NSTextAlignmentNatural? = 4,? ? // 根據(jù)現(xiàn)實(shí)的文字特性對(duì)齊

} NS_ENUM_AVAILABLE_IOS(6_0);

是否可以編輯

textView.editable = NO; // 默認(rèn)YES

是否可以選中

textView.selectable = NO; // 默認(rèn)YES 當(dāng)設(shè)置為NO時(shí),不能選擇

選中范圍

textView.selectedRange = NSMakeRange(8, 6);

富文本

NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithString:@"這是一個(gè)富文本"];

[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

// 重新設(shè)置 接下來(lái)改變的文字 的屬性字典

textView.typingAttributes = attributesDic;

/*一般在一些代理函數(shù)中使用,比如當(dāng)編輯狀態(tài)的變化*/

關(guān)于富文本的知識(shí)請(qǐng)看iOS富文本字符串AttributedString詳解

輸入視圖

// 試著改變view的frame,發(fā)現(xiàn)只有height值會(huì)對(duì)視圖有影響,只會(huì)改變附加視圖的高度

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

view.backgroundColor = [UIColor redColor];

// 不彈出鍵盤(pán),彈出添加的這個(gè)視圖,一般用作像銀行app的自定義鍵盤(pán)

textView.inputView = view;

輸入鍵盤(pán)附加視圖

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

view.backgroundColor = [UIColor redColor];

// 在鍵盤(pán)上附加一個(gè)視圖,一般用于添加一個(gè)收回鍵盤(pán)的按鈕

textView.inputAccessoryView = view;

獲得焦點(diǎn)后選中現(xiàn)有文本,輸入內(nèi)容時(shí)清除當(dāng)前選中文本

textView.clearsOnInsertion = YES; // 默認(rèn)為NO

文本內(nèi)容與邊界的間距

textView.textContainerInset = UIEdgeInsetsMake(20, 20, 20, 20);

鏈接文本的樣式設(shè)置

/*在接下來(lái)的應(yīng)用中會(huì)介紹*/

@property(null_resettable, nonatomic, copy) NSDictionary *linkTextAttributes NS_AVAILABLE_IOS(7_0);

只讀屬性

有時(shí)間會(huì)專(zhuān)門(mén)來(lái)說(shuō)這三個(gè)屬性......,會(huì)將鏈接帖到這兒

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

方法

滾動(dòng)到文本的某個(gè)段落

[textView scrollRangeToVisible:NSMakeRange(50, 5)];

代理函數(shù)

// 將要開(kāi)始編輯

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView;

// 將要結(jié)束編輯

- (BOOL)textViewShouldEndEditing:(UITextView *)textView;

// 開(kāi)始編輯

- (void)textViewDidBeginEditing:(UITextView *)textView;

// 結(jié)束編輯

- (void)textViewDidEndEditing:(UITextView *)textView;

// 文本將要改變

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

// 文本發(fā)生改變

- (void)textViewDidChange:(UITextView *)textView;

// 焦點(diǎn)發(fā)生改變

- (void)textViewDidChangeSelection:(UITextView *)textView;

// 是否允許對(duì)文本中的URL進(jìn)行操作

- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange NS_AVAILABLE_IOS(7_0);

// 是否允許對(duì)文本中的富文本進(jìn)行操作

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

通知

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

// 開(kāi)始編輯的通知

UIKIT_EXTERN NSString * const UITextViewTextDidBeginEditingNotification;

// 文本發(fā)生變化的通知

UIKIT_EXTERN NSString * const UITextViewTextDidChangeNotification;

// 編輯結(jié)束的通知

UIKIT_EXTERN NSString * const UITextViewTextDidEndEditingNotification;

應(yīng)用

設(shè)置鏈接樣式

UITextView * textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 100, 300, 50)];

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"這是一個(gè)鏈接: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)不能點(diǎn)擊鏈接

[self.view addSubview:textView];

// 要實(shí)現(xiàn)代理

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

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

NSString * url = [URL host];

NSLog(@"%@",url);

// 在這里利用url做點(diǎn)什么事情......

return NO;

}

return YES;

}

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

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容