zoukankan      html  css  js  c++  java
  • iOS 设置TextView 只能输入两行

    http://www.itstrike.cn/Question/c51c60a2-4ea9-4902-8ef9-0f14d1fcba9b.html

    - (void)viewDidLoad {

        [super viewDidLoad];

        // Do any additional setup after loading the view, typically from a nib.

        

        UITextView * view = [[UITextView alloc]init];

        

        view.frame = CGRectMake(20, 30, [UIScreen mainScreen].bounds.size.width-40, [UIScreen mainScreen].bounds.size.height-30);

        

        view.font= [UIFont systemFontOfSize:15];

        

        view.delegate =  self;

        

        [self.view addSubview:view];

        

        view.textContainer.maximumNumberOfLines = 2;

        

        

    }

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

    {

        NSString *newText = [textView.text stringByReplacingCharactersInRange:range withString:text];

        

        NSDictionary *textAttributes = @{NSFontAttributeName : textView.font};

        

        CGFloat textWidth = CGRectGetWidth(UIEdgeInsetsInsetRect(textView.frame, textView.textContainerInset));

        textWidth -= 2.0f * textView.textContainer.lineFragmentPadding;

        CGRect boundingRect = [newText boundingRectWithSize:CGSizeMake(textWidth, 0)

                                                    options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading

                                                 attributes:textAttributes

                                                    context:nil];

        

        NSUInteger numberOfLines = CGRectGetHeight(boundingRect) / textView.font.lineHeight;

        

        return newText.length <= 500 && numberOfLines <= 2;

    }

  • 相关阅读:
    Maven最全指南,看了必懂(转载)
    MySQL:数据库优化
    关于 MyBatis 10 种通用的写法
    Redis 性能问题分析(转载)
    前端对于服务端渲染(SSR)的理解
    JS 中的编码和解码(encode和decode)
    SEO、SPA、SSR的区别关系详解
    VUE 项目路由的两种模式(hash、history)
    Chrome和IE浏览器的渲染过程区别
    对于前端缓存的理解(缓存机制和缓存类型)
  • 原文地址:https://www.cnblogs.com/yecong/p/7803140.html
Copyright © 2011-2022 走看看