zoukankan      html  css  js  c++  java
  • uitextview根据内容算高度

     

    UITextView根据内容自动改变frame

    分类: iOS 190人阅读 评论(0) 收藏 举报

    注意点:

    textview中计算string占据的高度不能使用[NSStringsizeWithFont:constrainedToSize:],因为textView显示文字有自己的样式,在上下左右都有一定的偏移,所以先设置textView.text属性,然后调用[UITextView sizeThatFits:(CGSize)size] 此函数返回的size就是在textviewtext显示的区域大小。

    [cpp] view plaincopy
     
    1. - (void)textViewDidChange:(UITextView *)textView  
    2. {  
    3.     [textView flashScrollIndicators];   // 闪动滚动条  
    4.       
    5.     static CGFloat maxHeight = 130.0f;  
    6.     CGRect frame = textView.frame;  
    7.     CGSize constraintSize = CGSizeMake(frame.size.width, MAXFLOAT);  
    8.     CGSize size = [textView sizeThatFits:constraintSize];  
    9.     if (size.height >= maxHeight)  
    10.     {  
    11.         size.height = maxHeight;  
    12.         textView.scrollEnabled = YES;   // 允许滚动  
    13.     }  
    14.     else  
    15.     {  
    16.         textView.scrollEnabled = NO;    // 不允许滚动,当textview的大小足以容纳它的text的时候,需要设置scrollEnabed为NO,否则会出现光标乱滚动的情况  
    17.     }  
    18.     textView.frame = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, size.height);  
    19. }  
  • 相关阅读:
    java生成验证码
    SpringBoot定时任务
    事务管理
    Windows 2008 Server R2双网卡负载均衡
    HP Proliant DL580 gen9 阵列卡P440AR 高速缓存 被禁用
    Kali Debian 修改时区
    First Py From Py
    C++头文件#include<bits/stdc++.h>
    排序算法
    运算符优先级
  • 原文地址:https://www.cnblogs.com/gcb999/p/3338670.html
Copyright © 2011-2022 走看看