iOS自定义控件_CustomTextView

以前使用输入框文字限制,基本上是打字过程中阶段输入,效果很不好,特别输中文的过程中,好不容易用智能拼音打了一长串字,结果被自动截取打断了,变成了拼音,简直是气死宝宝了。后来想了个办法,就是识别输入框输入中文,在用户把一长串拼音确认转换为文字之后再截取。下面看效果:

效果(一),使用自动截取

效果(二),不使用自动截取


demo下载地址:http://download.csdn.net/detail/u012460084/9371634


实现代码:

TextViewCustom.h

//

//  TextViewCustom.h

//  PSTextFiled

//

//  Created by William Sterling on 14/12/25.

//  Copyright (c) 2014夏玉鹏. All rights reserved.

//


#import <UIKit/UIKit.h>

typedef void (^textBlocks)();

@interface TextViewCustom : UITextView


///编辑字体个数超过限制,是否使用自动截取的方式,默认:NO

@property (nonatomic,assign) BOOL interception;

///输入字体个数最大限制个数,默认:10000

@property (nonatomic,assign) NSInteger textLength;


///默认提示lab

@property (nonatomic,strong) UILabel *placehLab;

///默认:[UIColor grayColor]

@property (nonatomic,strong) UIColor *placehTextColor;

///默认:[UIFont systemFontOfSize:14.0]

@property (nonatomic,strong) UIFont *placehFont;

///默认:@""

@property (nonatomic,strong) NSString *placehText;


///是否需要右下角文字计数显示lab,默认:YES

@property (nonatomic,assign) BOOL promptLabHiden;

///右下角文字计数显示lab

@property (nonatomic,strong) UILabel *promptLab;

///默认:[UIColor grayColor]

@property (nonatomic,strong) UIColor *promptTextColor;

///默认:[UIFont systemFontOfSize:14.0];

@property (nonatomic,strong) UIFont *promptFont;

///默认:self.backgroundColor

@property (nonatomic,strong) UIColor *promptBackground;

///右下角,文字个数提示框_距父视图右边距_默认:10

@property (nonatomic,assign) CGFloat promptFrameMaxX;

///右下角,文字个数提示框_距父视图底边距_默认:10

@property (nonatomic,assign) CGFloat promptFrameMaxY;


///一个词语输出监听

@property (nonatomic,copy) textBlocks EditChangedBlock;

@end


TextViewCustom.m

//

//  TextViewCustom.m

//  PSTextFiled

//

//  Created by William Sterling on 14/12/25.

//  Copyright (c) 2014夏玉鹏. All rights reserved.

//


#import "TextViewCustom.h"


#define INT_LONG_BASE(x) ((long)x)

#define INT_ULONG_BASE(x) ((unsigned long)x)

@interface TextViewCustom ()

{

    

}

@end

@implementation TextViewCustom

@synthesize promptLab,placehLab;

- (id)initWithFrame:(CGRect)frame{

    self = [super initWithFrame:frame];

    if (self) {

        [self viewdidload];

    }

    return self;

}


-(void)dealloc{

    [[NSNotificationCenter defaultCenter]removeObserver:self name:UITextViewTextDidChangeNotification object:self];

    [[NSNotificationCenter defaultCenter]removeObserver:self name:UITextFieldTextDidBeginEditingNotification object:self];

    [[NSNotificationCenter defaultCenter]removeObserver:self name:UITextFieldTextDidEndEditingNotification object:self];

    

    if (self.EditChangedBlock) {

        self.EditChangedBlock = nil;

    }

}


- (void)viewdidload{

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textViewEditChanged:) name:UITextViewTextDidChangeNotification object:self];

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textViewEditBegin:) name:UITextViewTextDidBeginEditingNotification object:self];

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textViewEditEnd:) name:UITextViewTextDidEndEditingNotification object:self];

    

    self.showsVerticalScrollIndicator = NO;

    

    _textLength = 10000;

    _interception = NO;

    

    _placehTextColor = [UIColor grayColor];

    _placehFont = [UIFont systemFontOfSize:14.0];

    _placehText = @"";

    

    _promptLabHiden = YES;

    _promptFrameMaxX = 10.0;

    _promptFrameMaxY = 10.0;

    _promptTextColor = [UIColor grayColor];

    _promptFont = [UIFont systemFontOfSize:14.0];

    _promptBackground = self.backgroundColor;

    

    placehLab = [[UILabel alloc] initWithFrame:CGRectMake(10, 7, self.frame.size.width-10, 0)];

    placehLab.backgroundColor = [UIColor clearColor];

    placehLab.textColor = _placehTextColor;

    placehLab.font = _placehFont;

    [self addSubview:placehLab];

    

    if (_promptLabHiden) {//初始界面加入右下角字数提示语句,开始编辑时需要删除,因为textview_scrollview上加载的视图会跟随画布移动

        promptLab = [[UILabel alloc] init];

        promptLab.backgroundColor = [UIColor clearColor];

        promptLab.font = _promptFont;

        promptLab.textColor = _promptTextColor;

        [self addSubview:promptLab];

    }

}


#pragma mark <--------------self_text_set-------------->

- (void)setTextLength:(NSInteger)textLength{

    _textLength = textLength;

    

    promptLab.text = [NSString stringWithFormat:@"0/%ld",_textLength];

    [promptLab sizeToFit];

    promptLab.frame = CGRectMake(CGRectGetWidth(self.frame)-CGRectGetWidth(promptLab.frame)-_promptFrameMaxX, CGRectGetHeight(self.frame)-CGRectGetHeight(promptLab.frame)-_promptFrameMaxY, CGRectGetWidth(promptLab.frame), CGRectGetHeight(promptLab.frame));

    

    self.scrollEnabled = NO;

    self.contentInset = UIEdgeInsetsMake(self.contentInset.top, self.contentInset.left, CGRectGetHeight(promptLab.frame)+_promptFrameMaxY, self.contentInset.right);

}


#pragma mark <--------------placehLab_set-------------->

- (void)setPlacehTextColor:(UIColor *)placehTextColor{

    _placehTextColor = placehTextColor;

    placehLab.textColor = _placehTextColor;

}

- (void)setPlacehFont:(UIFont *)placehFont{

    _placehFont = placehFont;

    placehLab.font = _placehFont;

}

- (void)setPlacehText:(NSString *)placehText{

    _placehText = placehText;

    placehLab.text = _placehText;

    [placehLab sizeToFit];

}


#pragma mark <--------------promptLab_set-------------->

- (void)setPromptFrameMaxX:(CGFloat)promptFrameMaxX{

    _promptFrameMaxX = promptFrameMaxX;

    promptLab.frame = CGRectMake(CGRectGetWidth(self.frame)-CGRectGetWidth(promptLab.frame)-_promptFrameMaxX, CGRectGetHeight(self.frame)-CGRectGetHeight(promptLab.frame)-_promptFrameMaxY, CGRectGetWidth(promptLab.frame), CGRectGetHeight(promptLab.frame));

}

- (void)setPromptFrameMaxY:(CGFloat)promptFrameMaxY{

    _promptFrameMaxY = promptFrameMaxY;

    promptLab.frame = CGRectMake(CGRectGetWidth(self.frame)-CGRectGetWidth(promptLab.frame)-_promptFrameMaxX, CGRectGetHeight(self.frame)-CGRectGetHeight(promptLab.frame)-_promptFrameMaxY, CGRectGetWidth(promptLab.frame), CGRectGetHeight(promptLab.frame));

}

- (void)setPromptTextColor:(UIColor *)promptTextColor{

    _promptTextColor = promptTextColor;

    promptLab.textColor = _promptTextColor;

}

- (void)setPromptFont:(UIFont *)promptFont{

    _promptFont = promptFont;

    promptLab.font = _promptFont;

    

    [promptLab sizeToFit];

    promptLab.frame = CGRectMake(CGRectGetWidth(self.frame)-CGRectGetWidth(promptLab.frame)-_promptFrameMaxX, CGRectGetHeight(self.frame)-CGRectGetHeight(promptLab.frame)-_promptFrameMaxY, CGRectGetWidth(promptLab.frame), CGRectGetHeight(promptLab.frame));

}

- (void)setPromptBackground:(UIColor *)promptBackground{

    _promptBackground = promptBackground;

    promptLab.backgroundColor = _promptBackground;

}


#pragma mark <--------------textView_通知监听-------------->

- (void)textViewEditBegin:(NSNotification *)obj{//编辑开始

    if (_promptLabHiden && [[promptLab superview] isKindOfClass:[self class]] && promptLab) {

        [promptLab removeFromSuperview];//删除旧的初始界面右下角字数提示语句父视图是(self),开始编辑时需要删除,因为textview_scrollview上加载的视图会跟随画布移动

        

        self.scrollEnabled = YES;

        

        //新的右下角字数提示语句

        promptLab = [[UILabel alloc] init];

        promptLab.backgroundColor = self.backgroundColor;

        promptLab.font = _promptFont;

        promptLab.textColor = [UIColor grayColor];

        promptLab.text = [NSString stringWithFormat:@"%ld/%ld",self.text.length,_textLength];

        [promptLab sizeToFit];

        promptLab.frame = CGRectMake(CGRectGetMaxX(self.frame)-CGRectGetWidth(promptLab.frame)-_promptFrameMaxX, CGRectGetMaxY(self.frame)-CGRectGetHeight(promptLab.frame)-_promptFrameMaxY, CGRectGetWidth(promptLab.frame), CGRectGetHeight(promptLab.frame));

        

        //底部的遮罩图

        UIView *whiteView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.frame) - CGRectGetHeight(promptLab.frame) - _promptFrameMaxY, CGRectGetWidth(self.frame), CGRectGetHeight(promptLab.frame) + _promptFrameMaxY)];

        whiteView.backgroundColor = self.backgroundColor;

        

        [[self superview] addSubview:whiteView];//父视图切换为[self superview],防止跟随付self.scrollview的画布滚动

        [[self superview] addSubview:promptLab];//父视图切换为[self superview],防止跟随付self.scrollview的画布滚动

    }

}

- (void)textViewEditEnd:(NSNotification *)obj{//编辑结束

    

}

-(void)textViewEditChanged:(NSNotification *)obj{//编辑中

    if (self.text.length == 0) {

        placehLab.hidden = NO;

    }else{

        placehLab.hidden = YES;

    }

    

    NSString *toBeString = self.text;

    NSString *primaryLanguageStr = self.textInputMode.primaryLanguage; // 键盘输入模式

    if ([primaryLanguageStr isEqualToString:@"zh-Hans"]) {

        UITextRange *selectedRange = [self markedTextRange];

        //获取高亮部分

        UITextPosition *position = [self positionFromPosition:selectedRange.start offset:0];

        //没有高亮选择的字,则对已输入的文字进行字数统计和限制

        if (!position) {

            if (toBeString.length > _textLength) {

                if (_interception) {

                    self.text = [toBeString substringToIndex:_textLength];

                }else{

                    

                }

                [self changePromptLab];

            }else{

                [self changePromptLab];

            }

        }else{

            //有高亮选择的字符串,则暂不对文字进行统计和限制

        }

    }else{

        //中文输入法以外的直接对其统计限制即可,不考虑其他语种情况

        if (toBeString.length > _textLength) {

            if (_interception) {

                self.text = [toBeString substringToIndex:_textLength];

            }else{

                

            }

            [self changePromptLab];

        }else{

            [self changePromptLab];

        }

    }

    

    if (self.EditChangedBlock) {//一个词语输出监听

        self.EditChangedBlock();

    }

}

- (void)changePromptLab{

    

    NSString *changeStr = [NSString stringWithFormat:@"%ld/%ld",self.text.length,_textLength];

    if (!_interception && (self.text.length > _textLength)) {

        NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:changeStr];

        [attributedStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, changeStr.length - [NSString stringWithFormat:@"%ld",_textLength].length)];

        promptLab.attributedText = attributedStr;

    }else{

        promptLab.text = changeStr;

    }

    

    [promptLab sizeToFit];

    promptLab.frame = CGRectMake(CGRectGetMaxX(self.frame)-CGRectGetWidth(promptLab.frame)-_promptFrameMaxX, CGRectGetMaxY(self.frame)-CGRectGetHeight(promptLab.frame)-_promptFrameMaxY, CGRectGetWidth(promptLab.frame), CGRectGetHeight(promptLab.frame));

}

/*

 // Only override drawRect: if you perform custom drawing.

 // An empty implementation adversely affects performance during animation.

 - (void)drawRect:(CGRect)rect {

 // Drawing code

 }

 */


@end


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值