(一三〇)UITextField的光標操作擴展

簡介

在iOS開發中,有時候需要完全自主的定義鍵盤,用於完整的單詞輸入,例如計算機應用中,需要一次性的輸入sin(,在移動光標時要完整的跳過sin(,在刪除時也要完整的刪除,這就需要對光標的位置進行精確控制,而iOS並沒有相關的函數可以直接操作光標,只給出了選擇某個區域的功能,本文將介紹基於區域選擇設計的UITextField擴展,用於獲取光標位置以及移動光標。

實現原理

  • 光標位置的獲取
    在textField中,有一個屬性稱之爲selectedTextRange,這個屬性爲UITextRange類型,包含[start,end)兩個值,通過實驗我們可以發現,在沒有文字被選取時,start代表當前光標的位置,而end=0;當有區域被選擇時,start和end分別是選擇的頭和尾的光標位置,從0開始,並且不包含end,例如選擇了0~3的位置,則start=0,end=4。

  • 光標的移動
    通過setSelectedTextRange:方法可以設置選取範圍,我們只要設置一個選取單個字符的範圍,即可移動光標而不選中。

關鍵屬性

// 內容爲[start,end),無論是否有選取區域,start都描述了光標的位置。
@property (nullable, readwrite, copy) UITextRange *selectedTextRange;
// 文首和文尾的位置
@property (nonatomic, readonly) UITextPosition *beginningOfDocument;
@property (nonatomic, readonly) UITextPosition *endOfDocument;

關鍵方法

// 獲取以from爲基準的to的偏移,例如abcde,光標在c後,則光標相對文尾的偏移爲-2- (NSInteger)offsetFromPosition:(UITextPosition *)from toPosition:(UITextPosition *)toPosition;
// 獲取以from爲基準偏移offset的光標位置。
- (nullable UITextPosition *)positionFromPosition:(UITextPosition *)position offset:(NSInteger)offset;
// 創建一個UITextRange
- (nullable UITextRange *)textRangeFromPosition:(UITextPosition *)fromPosition toPosition:(UITextPosition *)toPosition;

具體實現

  • 下面的代碼爲UITextField創建了一個分類(Category),包含三個方法,分別用於獲取光標位置、從當前位置偏移、從頭偏移。
#import <UIKit/UIKit.h>

@interface UITextField (Extension)

- (NSInteger)curOffset;
- (void)makeOffset:(NSInteger)offset;
- (void)makeOffsetFromBeginning:(NSInteger)offset;

@end
#import "UITextField+Extension.h"

@implementation UITextField (Extension)

- (NSInteger)curOffset{

    // 基於文首計算出到光標的偏移數值。
    return [self offsetFromPosition:self.beginningOfDocument toPosition:self.selectedTextRange.start];

}

- (void)makeOffset:(NSInteger)offset{

    // 實現原理是先獲取一個基於文尾的偏移,然後加上要施加的偏移,再重新根據文尾計算位置,最後利用選取來實現光標定位。
    UITextRange *selectedRange = [self selectedTextRange];
    NSInteger currentOffset = [self offsetFromPosition:self.endOfDocument toPosition:selectedRange.end];
    currentOffset += offset;
    UITextPosition *newPos = [self positionFromPosition:self.endOfDocument offset:currentOffset];
    self.selectedTextRange = [self textRangeFromPosition:newPos toPosition:newPos];

}

- (void)makeOffsetFromBeginning:(NSInteger)offset{

    // 先把光標移動到文首,然後再調用上面實現的偏移函數。
    UITextPosition *begin = self.beginningOfDocument;
    UITextPosition *start = [self positionFromPosition:begin offset:0];
    UITextRange *range = [self textRangeFromPosition:start toPosition:start];
    [self setSelectedTextRange:range];
    [self makeOffset:offset];

}

@end
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章