zoukankan      html  css  js  c++  java
  • IOS关于不同字体和不同颜色的选择

    在项目中,用到一个功能,就是不同文字和不同颜色字体选择,

    界面如下:,效果如下:

    这个功能主要用到了textview的几个代理办法,其中一个重要的问题就是,在英文下和英文下的不同判断方式,以及是否有追加字:

    字体,颜色,字符用语封装起来,便于后续操作,

    在项目中,每个字符用一个对象保存在数组中。

    我们知道,中文在保存的时候占用两个字符,英文咱占用一个,所以在回调方法的时候会掉用两次

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

    这个方法的用法是这样子的,删除和修改回条用这个方法的 粘贴什么的夜会掉用这个方法 点击键盘的自述的时候会条用 点击默认追加的字不会条用这个方法,插入是用文本替换字符串 删除时用空字符串替换文本,当输入中文的时候 中国的时候 会出现条用8次 但是在文本与中之显示zhong guo9个字符的现象出现 其中多出现一个字符 空格  这个时候 使用textViewDidChange来进行判断数据的信息,其实做法是用text替换掉range中间的字符串

    所有代理方法的用法:

    ///

    ///@brief 准备编辑 最先条用

    ///

    - (BOOL)textViewShouldBeginEditing:(UITextView *)textView

    {

        FLOG_METHOD;

        

        NSLog(@"%@---",textView.text);

        return YES;

    }

    ///

    ///@brief 准备结束 顺序3

    ///

    - (BOOL)textViewShouldEndEditing:(UITextView *)textView

    {

        FLOG_METHOD;

        

        NSLog(@"%@---",textView.text);

        return YES;

    }

    ///

    ///@brief 开始编辑 顺序2

    ///

    - (void)textViewDidBeginEditing:(UITextView *)textView

    {

        FLOG_METHOD;

        NSLog(@"%@---",textView.text);

    }

    ///

    ///@brief 结束 顺序4

    ///

    - (void)textViewDidEndEditing:(UITextView *)textView

    {

        FLOG_METHOD;

        NSLog(@"%@---",textView.text);

    }

    ///

    ///@brief 删除和修改回条用这个方法的 粘贴什么的夜会掉用这个方法 点击键盘的自述的时候会条用 点击默认追加的字不会条用这个方法

    ///插入是用文本替换字符串 删除时用空字符串替换文本

    ///当输入中文的时候 中国的时候 会出现条用8次 但是在文本与中之显示zhong guo9个字符的现象出现 其中多出现一个字符 空格  这个时候 使用textViewDidChange来进行判断数据的信息

    ///其实做法是用text替换掉range中间的字符串

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

    {

        FLOG_METHOD;

        

        NSInteger location = range.location;

        

        NSLog(@"%@---%ld",textView.text,(long)textView.text.length);

        

        //删除字符串

        if([text isEqualToString:@""] && range.length > 0){

            for(NSInteger i = range.length - 1; i >= 0; i--)

                [_currentcolorarray removeObjectAtIndex:(location + i)];

            

            NSLog(@"删除字符个数 %ld  位置:%ld", (long)range.length, (long)location);

            return YES;

        }

        

        //替换字符串 替换的是那几个字符串呢? 如果有多的部分自动补缺

        if(![text isEqualToString:@""] && range.length > 0){

            NSInteger islast = 0;

            

            if(_currentcolorarray.count == 0 || textView.text.length == range.length + range.location)

                islast = 1;

            else

                islast = 0;

            

            //先删掉 在添加

            [_currentcolorarray removeObjectsInRange:NSMakeRange(range.location, range.length)];

            

            //现在添加  是插入还是增加

            if(islast == 0){

                NSInteger firstposition = range.location;

                

                for(NSInteger i = 0; i < range.length; i++){

                    NSString* temptext = [text substringWithRange:NSMakeRange( i, 1)];

                    

                    ColorTextObject* temp = [[ColorTextObject alloc] init];

                    

                    temp.text = temptext;

                    temp.fontsize = _selectfontsize;

                    temp.selectcolor = _selectcolorindex;

                    

                    const CGFloat *components = CGColorGetComponents(_selectcolor.CGColor);

                    

                    temp.redComponent = components[0];

                    temp.greenComponent = components[1];

                    temp.blueComponent = components[2];

                    temp.alphaComponent = components[3];

                    

                    [_currentcolorarray insertObject:temp atIndex:firstposition++];

                }

            }else{//增加

                

                for(NSInteger i = 0; i < range.length; i++){

                    ColorTextObject* temp = [[ColorTextObject alloc] init];

                    NSString* temptext = [text substringWithRange:NSMakeRange( i, 1)];

                    

                    temp.text = temptext;

                    temp.fontsize = _selectfontsize;

                    temp.selectcolor = _selectcolorindex;

                    

                    const CGFloat *components = CGColorGetComponents(_selectcolor.CGColor);

                    

                    temp.redComponent = components[0];

                    temp.greenComponent = components[1];

                    temp.blueComponent = components[2];

                    temp.alphaComponent = components[3];

                 

                    [_currentcolorarray addObject:temp];

                }

            }

            

            return YES;

        }

        

        return YES;

    }

    ///

    ///@brief 添加英文数据信息 是直接添加的

    ///

    -(void)addEnglishTextView:(UITextView*)textView location:(NSInteger)location

    {

        FLOG_METHOD;

        

        NSLog(@"英文---------%@----------%ld",textView.text,(long)location);

        

        if(_waitindex == 0){//英文的输入 直接添加或者插入数据信息

            NSString* tempstring = textView.text;

            NSString* temp = [tempstring substringWithRange:NSMakeRange(location - 1, 1)];

            

            ColorTextObject* object = [[ColorTextObject alloc] init];

            object.text = temp;

            object.fontsize = _selectfontsize;

            object.selectcolor = _selectcolorindex;

            

            const CGFloat *components = CGColorGetComponents(_selectcolor.CGColor);

            

            object.redComponent = components[0];

            object.greenComponent = components[1];

            object.blueComponent = components[2];

            object.alphaComponent = components[3];

            

            if(location == textView.text.length)

                [_currentcolorarray addObject:object];

            else

                [_currentcolorarray insertObject:object atIndex:(location - 1)];

            

            _lastposition = location;

            return;

        }

        

        NSLog(@"----------------%ld",(long)_waitnumber);

        

        if(_waitnumber > 1)

            _waitnumber--;

        

        for(NSInteger i = 0; i < location - _lastposition; i++){

            NSString* tempstirng = textView.text;

            NSString* temp = [tempstirng substringWithRange:NSMakeRange( _lastposition + i, 1)];

            

            ColorTextObject* object = [[ColorTextObject alloc] init];

            object.text = temp;

            object.fontsize = _selectfontsize;

            object.selectcolor = _selectcolorindex;

            

            const CGFloat *components = CGColorGetComponents(_selectcolor.CGColor);

            

            object.redComponent = components[0];

            object.greenComponent = components[1];

            object.blueComponent = components[2];

            object.alphaComponent = components[3];

            

            if(location == textView.text.length)

                [_currentcolorarray addObject:object];

            else

                [_currentcolorarray insertObject:object atIndex:(_lastposition + i)];

        }

        

        _lastposition = location;

        _waitnumber = 0;

        return;

    }

    ///

    ///@brief 添加英文数据信息 是直接添加的

    ///

    -(void)addChineseTextView:(UITextView*)textView location:(NSInteger)location

    {

        FLOG_METHOD;

        

        _waitindex = 1;//有中文的

        _waitnumber++;//表示中文的个数

    }

    ///

    ///@brief 输入框改变了 改变的话 条用顺序2

    ///

    - (void)textViewDidChange:(UITextView *)textView

    {

        FLOG_METHOD;

        

        NSInteger location = textView.selectedRange.location;

        UITextRange* textrange = textView.markedTextRange;

        

        if(textrange == nil)

            [self addEnglishTextView:textView location:location];

        else//中文  但是会使用了两次这个数据信息

            [self addChineseTextView:textView location:location];

        

        [self showInView];

    }

    所有代理就在这里了,然后再在界面上显示出来,就达到想要的效果了。

     再发几张效果图:

  • 相关阅读:
    3d服务器配置
    Can't connect to postgres on centos with psycopg
    flask快速入门
    nohup: cannot run command “/bin/java”:
    linux 上redis的启动口令
    CentOS网络设置 couldn't resolve host 'mirrorlist.centos.org问题解决
    CentOS下使用Mysql
    解决nodejs跨域的一个中间件
    JS实现禁用滑动条但滑动条不消失的效果
    JQ实现下拉加载更多
  • 原文地址:https://www.cnblogs.com/jiaoxiangjie/p/5947991.html
Copyright © 2011-2022 走看看