如何写好一个TextView使其文字不抖动

参考链接
废话不多说直接上代码

#define textFont [UIFont systemFontOfSize:16]
@interface ViewController ()<UITextViewDelegate>
@property (nonatomic) UITextView *textView;
@property (nonatomic, assign, getter=isMark) BOOL mark;
@end

创建textView,并设置属性

- (UITextView *)textView {
    if (!_textView) {
        _textView = [[UITextView alloc] initWithFrame:CGRectMake(50, 100, 300, 30)];
        _textView.font = textFont;
        //文本内容必须要设置,不然行间距的设置不起作用,这里以输入一个空格为例
        _textView.text = @" ";
        //设置整个控件文字的上下距离
        _textView.textContainerInset = UIEdgeInsetsMake(5, 0, 5, 0);
        //NSMutableParagraphStyle 设置段落风格
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        //设置段落的行间距
        paragraphStyle.lineSpacing = 5;
        NSDictionary *attributes = @{
                                     NSFontAttributeName:textFont,
                                     NSParagraphStyleAttributeName:paragraphStyle};
        //NSAttributedString 富文本用来设置文字的样式
        _textView.attributedText = [[NSAttributedString alloc] initWithString:_textView.text attributes:attributes];
        _textView.delegate = self;
        _textView.backgroundColor = [UIColor grayColor];
        [self.view addSubview:_textView];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewTextDidChange:) name:UITextViewTextDidChangeNotification object:_textView];
    }
    return _textView;
}

通知事件

/**
 *  设置输入超过三行(高度78,高度计算所得,字体大小的和+行间距的和)自动滚入不可见区域
 */
- (void)textViewTextDidChange:(NSNotification *)notification {
    //hasPrefix:方法的功能是判断创建的字符串内容是否以某个字符开始
    if ([self.textView.text hasPrefix:@" "]) {
        self.textView.text = [self.textView.text stringByReplacingOccurrencesOfString:@" " withString:@" "];
    }
    CGFloat height =self.textView.contentSize.height > 78 ? 78 : self.textView.contentSize.height;
    self.textView.frame = CGRectMake(50, CGRectGetMaxY(self.textView.frame) - height, self.textView.frame.size.width, height);
    return;
}

代理方法

#pragma mark - UITextViewDelegate
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    _mark = YES;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    _mark = NO;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    //判断是否拖动来设置frame
    if (!self.isMark) {
        NSLog(@"%lf", self.textView.contentSize.height);
        if (self.textView.contentSize.height > 78) {
            [self.textView setContentOffset:CGPointMake(0, self.textView.contentSize.height - 78)];
        } else {
            [self.textView setContentOffset:CGPointMake(0, 0)];
        }
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,676评论 25 709
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,273评论 19 139
  • 国家电网公司企业标准(Q/GDW)- 面向对象的用电信息数据交换协议 - 报批稿:20170802 前言: 排版 ...
    庭说阅读 11,311评论 6 13
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,301评论 4 61
  • 1.我们毕业了 走出了高中的大门,回首望去,就这样度过了我们一生中最美好的青春,十八岁的青春祭奠了那命运的高三,...
    清色茉莉阅读 96评论 0 0