zoukankan      html  css  js  c++  java
  • 技巧篇 一、文字处理技巧

    1. 给文字加阴影

    最近在做一个直播的项目,本来一切顺利,结果UI妹子说要给透明背景下的文字添加阴影效果,第一次遇到这样的需求,于是呢就搜索了一下,木有找到满意的办法。转念一想,属性字符串应该是可以解决这个问题,毕竟下划线什么的都能加,阴影应该也可以。在

    NSAttributedString.h里果然找到了shadow的字段——NSShadowAttributeName,然后在帮助文档里看到这个key对应的是NSShadow对象,NSShadow里面可以设置阴影的颜色、offset,blurRadius等,剩下的部分就看代码咯。ps:要是经常用到的话,可以写一个分类,就能避免重复代码了。 

    - (void)addShadowToTextView:(UITextView *)textView
    
    {
    
        NSShadow *shadow = [[NSShadow alloc] init];
    
        shadow.shadowOffset = CGSizeMake(1, 1);
    
        
    
        NSDictionary *attr = @{
    
                                       NSFontAttributeName : textView.font,
    
                                       NSForegroundColorAttributeName : textView.textColor,
    
                                       NSShadowAttributeName : shadow
    
                                       };
    
        
    
        NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:textView.text attributes:attr];
    
        textView.attributedText = attributedString;
    
    }

    同理,自己绘制字符串的时候同样可以利用Attribute来实现阴影,代码如下

    - (void)drawStringWithShadow:(NSString *)string fontColor:(UIColor *)fontColor shadowColor:(UIColor *)shadowColor font:(UIFont *)font rect:(CGRect)rect
    {
        NSShadow *shadow = [[NSShadow alloc] init];
        shadow.shadowOffset = CGSizeMake(1, 1);
        shadow.shadowColor = shadowColor;
        NSMutableDictionary *attr = [@{
                                       NSFontAttributeName : font,
                                       NSForegroundColorAttributeName : fontColor,
                                       NSShadowAttributeName : shadow
                                       } mutableCopy];
        
        [string drawInRect:rect withAttributes:attr];
    }

     2.限制UITextView的字数

      如果直接通过UITextView的代理方法- (void)textViewDidChange:(UITextView *)textView 来限制字数的话,在输入英文的时候不会有问题,但在输入中文的时候,当在一直输入字符的情况下就会有问题,当联想的字符超过字数限制时就会默认出现英文字符。解决办法就是利用通知监听,字符变化,代码如下

    - (void)textChanged:(NSNotification *)noti
    {
        UITextView *textView = (UITextView *)noti.object;
        NSInteger maxLength = 45 ;
     
        
        NSString *toBeString = textView.text;
        NSArray *current = [UITextInputMode activeInputModes];
        UITextInputMode *currentInputMode = [current firstObject];
        NSString *lang = [currentInputMode primaryLanguage]; // 键盘输入模式
        if ([lang isEqualToString:@"zh-Hans"]) { // 简体中文输入,包括简体拼音,健体五笔,简体手写
            UITextRange *selectedRange = [textView markedTextRange];
            //获取高亮部分
            UITextPosition *position = [textView positionFromPosition:selectedRange.start offset:0];
            // 没有高亮选择的字,则对已输入的文字进行字数统计和限制
            if (!position) {
                if (toBeString.length > maxLength) {
                    textView.text = [toBeString substringToIndex:maxLength];
                }
            }
            // 有高亮选择的字符串,则暂不对文字进行统计和限制
            else{
                
            }
        }
        // 中文输入法以外的直接对其统计限制即可,不考虑其他语种情况
        else{
            if (toBeString.length > maxLength) {
                textView.text = [toBeString substringToIndex:maxLength];
            }
        }
    }
  • 相关阅读:
    移动端摇一摇与重力感应事件
    百度的js日历
    wow.js
    pc网页中嵌入百度地图
    微信小程序之倒计时插件 wxTimer
    IE常见的兼容处理
    particles.js使用及配置
    微信小程序之swiper组件高度自适应
    js获取元素的滚动高度,和距离顶部的高度
    vue实现移动端触屏拖拽功能
  • 原文地址:https://www.cnblogs.com/pretty-guy/p/7999261.html
Copyright © 2011-2022 走看看