博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS 自适应高度,改变字体颜色
阅读量:6408 次
发布时间:2019-06-23

本文共 6330 字,大约阅读时间需要 21 分钟。

 

 

#define kMainBoundsWidth    ([UIScreen mainScreen].bounds).size.width //屏幕的宽度#define kFont               [UIFont systemFontOfSize:17.f] //字体大小#define kLineSpacing        7 //行间距
- (void)viewDidLoad {    [super viewDidLoad];        //将view背景颜色变更为黄色    self.view.backgroundColor = [UIColor grayColor];    self.textView.text = @"I got a question regarding objc blocks. If you want to use self in a block you should weakify it and strongify it again in the block so you don't get into a retain cycle.";//文字内容    self.textField.text = @"regarding";//高亮内容    self.lableContent.text = self.textView.text;//显示内容        @weakify(self);    [self.textView.rac_textSignal subscribeNext:^(NSString * _Nullable x) {        @strongify(self);        self.lableContent.text = x;        [self layoutLableContent:self.textField.text content:x];    }];        //高亮文字    [self.textField.rac_textSignal subscribeNext:^(NSString * _Nullable x) {        [self layoutLableContent:self.textField.text content:self.lableContent.text];    }];}/** 文字内容 */-(UITextView *)textView{    if (!_textView) {        _textView = [[UITextView alloc] init];        _textView.font = [UIFont systemFontOfSize:15.f];        _textView.showsHorizontalScrollIndicator = YES;        _textView.layer.cornerRadius = 4.f;        _textView.layer.masksToBounds = YES;        [self.view addSubview:_textView];                [_textView mas_makeConstraints:^(MASConstraintMaker *make) {            make.top.equalTo(self.view).offset(80);            make.centerX.equalTo(self.view);            make.width.equalTo(self.view).multipliedBy(0.8);            make.height.offset(100);        }];    }    return _textView;}/** 高亮文字 */-(UITextField *)textField{    if (!_textField) {        _textField = [UITextField new];        _textField.placeholder = @"请输入高亮文字";        _textField.layer.cornerRadius = 4.f;        _textField.layer.masksToBounds = YES;        _textField.textAlignment = NSTextAlignmentCenter;        _textField.backgroundColor = [UIColor whiteColor];        _textField.keyboardType = UITextAutocorrectionTypeNo;        [self.view addSubview:_textField];                [_textField mas_makeConstraints:^(MASConstraintMaker *make) {            make.top.equalTo(_textView.mas_bottom).offset(30);            make.centerX.equalTo(self.view);            make.width.equalTo(self.view).multipliedBy(0.5);            make.height.offset(50);        }];    }    return _textField;}/** 目标文字 */-(UILabel *)lableContent{    if (!_lableContent) {        _lableContent = [UILabel new];        _lableContent.font = kFont;        _lableContent.layer.cornerRadius = 4.f;        _lableContent.layer.masksToBounds = YES;        _lableContent.layer.borderColor = [UIColor yellowColor].CGColor;        _lableContent.layer.borderWidth = 1.f;        _lableContent.backgroundColor = [UIColor whiteColor];        _lableContent.numberOfLines = 0;        [self.view addSubview:_lableContent];                [_lableContent mas_makeConstraints:^(MASConstraintMaker *make) {            make.top.equalTo(_textField.mas_bottom).offset(50);            make.centerX.equalTo(self.view);            make.width.offset(kMainBoundsWidth-40);            make.height.offset(200);        }];    }    return _lableContent;}/** 更新数据 */-(void)layoutLableContent:(NSString *)keyWord content:(NSString *)content{    CGFloat width = kMainBoundsWidth-40;    CGSize contentSize = [self adaptContentStringSizeWithFont:kFont withWidth:width content:content];    CGSize size = CGSizeMake(width, contentSize.height+25);        [self.lableContent mas_updateConstraints:^(MASConstraintMaker *make) {        make.height.offset(size.height);    }];    //必须调用此方法,才能出动画效果    [self.view layoutIfNeeded];        NSMutableAttributedString *attributedStrContent = [[NSMutableAttributedString alloc]initWithString:self.lableContent.text];    NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];    paragraphStyle.lineSpacing = kLineSpacing;    [attributedStrContent addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [attributedStrContent length])];    [self addAttributeColorOfSubString:keyWord inString:content tatgetString:attributedStrContent];    self.lableContent.attributedText = attributedStrContent;}/** 在指定字符串的颜色属性 */- (void)addAttributeColorOfSubString:(NSString*)subStr inString:(NSString*)content tatgetString:(NSMutableAttributedString*)attributedString {    NSString*string1 = [content stringByAppendingString:subStr];    NSString *temp;    bool iscnChar = NO;    int cnIndex = 0;    for(int i =0; i < content.length; i++) {        temp = [string1 substringWithRange:NSMakeRange(i, subStr.length)];        if ([temp isEqualToString:subStr]) {            NSRange range = {i,subStr.length};            [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range];            [attributedString addAttribute:NSFontAttributeName value:kFont range:range];        }                unichar c = [string1 characterAtIndex:i];        if (c >=0x4E00 && c <=0x9FA5){            cnIndex = i;            iscnChar = YES;            NSRange range = {i, 1};            [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:range];        }    }}- (CGSize)adaptContentStringSizeWithFont:(UIFont*)font withWidth:(CGFloat)width content:(NSString *)content{    //行间距    NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];    paragraphStyle.lineSpacing = kLineSpacing;        NSDictionary *attributes = @{                                 NSFontAttributeName:font,                                 NSParagraphStyleAttributeName:paragraphStyle                                 };    CGSize contentSize = [content boundingRectWithSize:CGSizeMake(width, MAXFLOAT)                                               options:NSStringDrawingUsesLineFragmentOrigin                                            attributes:attributes                                               context:nil].size;    return contentSize;}//取消键盘-(void)touchesBegan:(NSSet
*)touches withEvent:(UIEvent *)event{ [self.textView resignFirstResponder]; [self.textField resignFirstResponder]; [self.lableContent resignFirstResponder];}

 

转载于:https://www.cnblogs.com/xujinzhong/p/8514645.html

你可能感兴趣的文章
ftp下载指定日期文件(文件名中含日期)
查看>>
Exchange 2010 实用小技巧
查看>>
2018年最新人工智能书单,总有一本你爱的
查看>>
B2G编译前的准备
查看>>
糟糕的程序员有哪些招牌特质?
查看>>
[npm] 一个命令解决Unexpected end of JSON input... 问题
查看>>
HTTPS的实现及其原理
查看>>
Android Browser学习五 多窗口: Tab 整体结构
查看>>
PHP读取csv文件内容的方法详解
查看>>
jQuery装饰者模式绑定DOM事件
查看>>
Python术语表
查看>>
aar包 生成 以及相关处理
查看>>
使用kindeditor 实现 Play FrameWork的html编辑器
查看>>
marathon constraints 的花式用法
查看>>
Mac OSX 下 mysql 影响关系的问题处理
查看>>
JavaScript邮箱系统开发(二)
查看>>
【解决】Caused by: org.apache.tiles.definition.DefinitionsFactoryException: I/O
查看>>
发布karaf的features
查看>>
Android 多线程处理之多线程用法大集合
查看>>
属性动画总结(Property Animation)
查看>>