iOS UITextView 根据输入text自适应高度

转载自:http://www.cnblogs.com/tmf-4838/p/5380495.html 

#import "ViewController.h"

  

@interface ViewController ()<UITextViewDelegate>

// KVO和动态自适应尺寸

@property (nonatomic, strong)UITextView *txtView;

// KVO测试

@property (nonatomic, strong)Person *person;

@end

@implementation ViewController

- (void)viewDidLoad

{

    [super viewDidLoad];

    

    // 1.字数大于label长度时循环播放

    // 创建第三方控件lbl对象

    BBFlashCtntLabel *BFLbl = [[BBFlashCtntLabel alloc] initWithFrame:CGRectMake(107, 100, 200, 50)];

    // 更换背景色

    BFLbl.backgroundColor = [UIColor purpleColor];

    // 属性字符串

    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:@"2016年1月,随着9.2.1版本的发布,苹果修复了一个存在了3年的漏洞。该漏洞在iPhone或iPad用户在酒店或者机场等访问带强制门户的网络时,登录页面会通过未加密的HTTP连接显示网络使用条款。在用户接受条款后,即可正常上网,但嵌入浏览器会将未加密的Cookie分享给Safari浏览器。利用这种分享的资源,黑客可以创建自主的虚假强制门户,并将其关联至WiFi网络,从而窃取设备上保存的任何未加密Cookie"];

    

    // 字体

    [attStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:25] range:NSMakeRange(0, attStr.length)];

    // 字体背景色

    [attStr addAttribute:NSBackgroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0, attStr.length)];

    // 字体前景色

    [attStr addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(0, attStr.length)];

    // 倒圆角

    BFLbl.layer.cornerRadius = 15;

    // 为label添加属性字符串

    BFLbl.attributedText = attStr;

    // 为label限定播放速度

    BFLbl.speed = BBFlashCtntSpeedMild;

    // 循环滚动次数(为0时无限滚动)

    BFLbl.repeatCount = 0;

    

    [self.view addSubview:BFLbl];

    

    // 2.字数大于label长度时自定义尺寸

    // 创建label对象

    UILabel *lbl = [[UILabel alloc] init];

    // 自适应大小因此行数为0

    lbl.numberOfLines = 0;

    // 设置背景色

    lbl.backgroundColor = [UIColor redColor];

    // 文本内容

    lbl.text = @"2016年1月,随着9.2.1版本的发布,苹果修复了一个存在了3年的漏洞。该漏洞在iPhone或iPad用户在酒店或者机场等访问带强制门户的网络时,登录页面会通过未加密的HTTP连接显示网络使用条款。在用户接受条款后,即可正常上网,但嵌入浏览器会将未加密的Cookie分享给Safari浏览器。利用这种分享的资源,黑客可以创建自主的虚假强制门户,并将其关联至WiFi网络,从而窃取设备上保存的任何未加密Cookie";

    // 设置文本颜色

    lbl.textColor = [UIColor whiteColor];

    // 设置自适应段落

    NSMutableParagraphStyle *para = [NSMutableParagraphStyle new];

    para.lineBreakMode = NSLineBreakByWordWrapping;

    // 创建字典携带属性:尺寸取决于字体的font

    NSDictionary *dic = @{NSFontAttributeName:lbl.font, NSParagraphStyleAttributeName:para};

    //  获取字体的尺寸:lbl的font计算出来的

    CGRect rect = [lbl.text boundingRectWithSize:CGSizeMake(300, CGRectGetHeight(self.view.frame)) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];

    // 只是label的尺寸(高度取负值即可实现往上移动啦)

    lbl.frame = CGRectMake(57, HEI-10, rect.size.width, -rect.size.height);

    

    [self.view addSubview:lbl];

    

    

    // 动态获取尺寸

    _txtView = [[UITextView alloc] init];

    _txtView.backgroundColor = [UIColor redColor];

    

    _txtView.delegate = self;

    

    _txtView.text = @"2016年1月,随着9.2.1版本的发布,苹果修复了一个存在了3年的漏洞。该漏洞在iPhone或iPad用户在酒店或者机场等访问带强制门户的网络时,登录页面会通过未加密的HTTP连接显示网络使用条款。";

   

    [self textViewDidChange:_txtView];

    

    NSLog(@"%@", self.txtView.font.fontDescriptor);

    NSLog(@"%@", self.txtView.font.description);

   

   [self.view addSubview:self.txtView];

  

    

    // KVO

    // 在UI中使用到KVO:一般系统控件都有事件监听大部分都是自定义类

    需要创建至少一个属性(即全局变量),如果是创建局部变量监听一次就会被销毁,再次改变程序就会崩溃

    因为在给监听者赋值是通过重写其setter来添加监听的,没有引用计数器加1,因此需要至少其一是全局变量

    

    // 重写setter只是实现了监听:因此被监听者对象需要赋值给监听者,触发添加监听事件监听开始

    self.person = [[Person alloc] init];

    // 添加监听

    Txt *tt = [Txt new];

    tt.per = self.person;;

    

    self.person.name = @"pp";

    

    // 只能监听到赋值,在屏幕上录入不能监听到

    tt.txtview = self.txtView;

    self.txtView.text = @"11";

}

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

{

    // text得到的是每次录入的字符,range.location为字符位置

    return YES;

}

// 代理监听:当text改变即调用该方法

- (void)textViewDidChange:(UITextView *)textView

{

    NSMutableParagraphStyle *para = [NSMutableParagraphStyle new];

    para.lineBreakMode = NSLineBreakByWordWrapping;

    // 创建字典携带属性:尺寸取决于字体的font

    NSDictionary *dic = @{NSFontAttributeName:_txtView.font, NSParagraphStyleAttributeName:para};

    //  获取字体的尺寸:lbl的font计算出来的

    CGRect rect = [_txtView.text boundingRectWithSize:CGSizeMake(0,0) options:NSStringDrawingUsesFontLeading attributes:dic context:nil];

    

    CGSize size = [_txtView sizeThatFits:CGSizeMake(100, rect.size.height)];

    

    _txtView.frame = CGRectMake(100, 500, 100, -size.height);

    

    

#import <Foundation/Foundation.h>

@class UITextView, Person;

@interface Txt : NSObject

// 重写setter只是实现了监听:因此被监听者对象需要赋值给监听者,触发添加监听事件监听开始

@property (nonatomic, strong)UITextView *txtview;

@property (nonatomic, strong)Person *per;

#import "Txt.h"

#import "Person.h"

#import <UIKit/UIKit.h>

@implementation Txt

// 在UI中使用到KVO:一般系统控件都有事件监听大部分都是自定义类

需要创建至少一个属性(即全局变量),如果是创建局部变量监听一次就会被销毁,再次改变程序就会崩溃

因为在飞监听者赋值是通过重写其setter来添加监听的,没有引用计数器加1,因此需要至少其一是全局变量

// 添加监听

// 重写setter只是实现了监听:因此被监听者对象需要赋值给监听者,触发添加监听事件监听开始

- (void)setPer:(Person *)per

{

    _per = per;

    [self.per addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];

}

// 添加监听

- (void)setTxtview:(UITextView *)txtview

{

    _txtview = txtview;

    [self.txtview addObserver:self forKeyPath:@"text" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];

}

// 注册监听

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context

{

    if ([keyPath isEqualToString:@"name"])

    {

        NSLog(@"pp旧值.%@", [change objectForKey:NSKeyValueChangeOldKey]);

        NSLog(@"pp新值.%@", [change objectForKey:NSKeyValueChangeNewKey]);

        

    }else if ([keyPath isEqualToString:@"text"])

    {

        NSLog(@"txt旧值.%@", [change objectForKey:NSKeyValueChangeOldKey]);

        NSLog(@"txt新值.%@", [change objectForKey:NSKeyValueChangeNewKey]);

    }

}

// 移除监听:将指针设置为空

- (void)delete:(id)sender

{

    [self.per removeObserver:self forKeyPath:@"name" context:nil];

    self.per = nil;

    

    [self.txtview removeObserver:self forKeyPath:@"text"];

    self.txtview = nil;

}

#import <Foundation/Foundation.h>

@interface Person : NSObject

@property (nonatomic, strong)NSString *name;

@end

  

#import "Person.h"

@implementation Person

/*

- (instancetype)init

{

    if (self = [super init])

    {

        _name = @"****";

    }

    return self;

}

*/

- (NSString *)description

{

    return [NSString stringWithFormat:@"%@", _name];

}

@end

    

}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event

{

    [self.view endEditing:YES];

}

原文地址:https://www.cnblogs.com/wujie123/p/5380557.html