1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- //
- // UITextField+ExtentRange.m
- // DDTG
- //
- // Created by 翟玉磊 on 16/11/8.
- // Copyright © 2016年 翟玉磊. All rights reserved.
- //
- #import "UITextField+ExtentRange.h"
- @implementation UITextField (ExtentRange)
- - (NSRange) selectedRange
- {
- UITextPosition* beginning = self.beginningOfDocument;
-
- UITextRange* selectedRange = self.selectedTextRange;
- UITextPosition* selectionStart = selectedRange.start;
- UITextPosition* selectionEnd = selectedRange.end;
-
- const NSInteger location = [self offsetFromPosition:beginning toPosition:selectionStart];
- const NSInteger length = [self offsetFromPosition:selectionStart toPosition:selectionEnd];
-
- return NSMakeRange(location, length);
- }
- - (void) setSelectedRange:(NSRange) range // 备注:UITextField必须为第一响应者才有效
- {
- UITextPosition* beginning = self.beginningOfDocument;
-
- UITextPosition* startPosition = [self positionFromPosition:beginning offset:range.location];
- UITextPosition* endPosition = [self positionFromPosition:beginning offset:range.location + range.length];
- UITextRange* selectionRange = [self textRangeFromPosition:startPosition toPosition:endPosition];
-
- [self setSelectedTextRange:selectionRange];
- }
- /**
- 修改默认字体颜色
- @param color 要修改成的颜色
- */
- - (void)changePlaceholderColor:(UIColor *)color {
-
- self.attributedPlaceholder = [[NSAttributedString alloc] initWithString:self.placeholder attributes:@{NSForegroundColorAttributeName: color}];
- }
- /**
- setup Clean Button Image
- @param imageName 图片名字
- */
- - (void)setupCleanButtonImageName:(NSString *)imageName {
-
- UIButton *cleanButton = [self valueForKey:@"_clearButton"];
- [cleanButton setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
- [cleanButton setImage:[UIImage imageNamed:imageName] forState:UIControlStateHighlighted];
- }
- @end
|