首页 > 移动平台 > 详细

iOS 设置TextView 只能输入两行

时间:2017-11-08 12:56:58      阅读:373      评论:0      收藏:0      [点我收藏+]

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;

}

 

iOS 设置TextView 只能输入两行

原文:http://www.cnblogs.com/yecong/p/7803140.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!