#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:.f];
_textView.showsHorizontalScrollIndicator = YES;
_textView.layer.cornerRadius = .f;
_textView.layer.masksToBounds = YES;
[self.view addSubview:_textView]; [_textView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.view).offset();
make.centerX.equalTo(self.view);
make.width.equalTo(self.view).multipliedBy(0.8);
make.height.offset();
}];
}
return _textView;
} /** 高亮文字 */
-(UITextField *)textField{
if (!_textField) {
_textField = [UITextField new];
_textField.placeholder = @"请输入高亮文字";
_textField.layer.cornerRadius = .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();
make.centerX.equalTo(self.view);
make.width.equalTo(self.view).multipliedBy(0.5);
make.height.offset();
}];
}
return _textField;
} /** 目标文字 */
-(UILabel *)lableContent{
if (!_lableContent) {
_lableContent = [UILabel new];
_lableContent.font = kFont;
_lableContent.layer.cornerRadius = .f;
_lableContent.layer.masksToBounds = YES;
_lableContent.layer.borderColor = [UIColor yellowColor].CGColor;
_lableContent.layer.borderWidth = .f;
_lableContent.backgroundColor = [UIColor whiteColor];
_lableContent.numberOfLines = ;
[self.view addSubview:_lableContent]; [_lableContent mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(_textField.mas_bottom).offset();
make.centerX.equalTo(self.view);
make.width.offset(kMainBoundsWidth-);
make.height.offset();
}];
}
return _lableContent;
} /** 更新数据 */
-(void)layoutLableContent:(NSString *)keyWord content:(NSString *)content{
CGFloat width = kMainBoundsWidth-;
CGSize contentSize = [self adaptContentStringSizeWithFont:kFont withWidth:width content:content];
CGSize size = CGSizeMake(width, contentSize.height+); [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(, [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 = ;
for(int i =; 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, };
[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<UITouch *> *)touches withEvent:(UIEvent *)event{
[self.textView resignFirstResponder];
[self.textField resignFirstResponder];
[self.lableContent resignFirstResponder];
}

iOS 自适应高度,改变字体颜色的更多相关文章

  1. markdown改变字体颜色和大小

    markdown中改变字体颜色与大小方法同html 先看例子 <font face="黑体">我是黑体字</font> 我是黑体字 <font fac ...

  2. 色彩缤纷的python(改变字体颜色及样式不完全版)

    色彩缤纷的python(改变字体颜色及样式) *补上昨天随笔中提到的改变字体颜色样式的方法,昨日随笔https://www.cnblogs.com/Du704/p/11265958.html 在项目过 ...

  3. 色彩缤纷的Python(改变字体颜色及样式)

    色彩缤纷的python(改变字体颜色及样式) 在项目过程中,我们常常会因为输出信息的颜色与样式过于单调以至于让人在视觉上感到很杂乱,所以看下文: 在Linux终端中,使用转义序列来进行如上所述的显示, ...

  4. Android NumberPicker 修改分割线颜色和高度及字体颜色大小

    (1)重写NumberPicker已达到修改显示字体颜色大小 public class TextColorNumberPicker extends NumberPicker { public Text ...

  5. RadioGroup 的 RadioButton 选择改变字体颜色和背景颜色

    RadioGroup <RadioGroup android:id="@+id/client_charge_radiogroup" android:layout_width= ...

  6. Android 改变字体颜色的三种方法

    在TextView中添加文本时有时需要改变一些文本字体的颜色,今天学到了三种方法,拿出来分享一下     1.在layout文件下的配置xml文件中直接设置字体颜色,通过添加android:textc ...

  7. Android RadioGroup的RadioButton 选择改变字体颜色和背景颜色

    RadioGroup <RadioGroup android:id="@+id/client_charge_radiogroup" android:layout_width= ...

  8. iOS 设置不同的字体颜色

    //设置不同字体颜色 -(void)fuwenbenLabel:(UILabel *)labell FontNumber:(UIFont *)font AndRange:(NSRange)range ...

  9. iOS设置状态栏的字体颜色

    设置statusBar的[前景色部分] 1.plist设置statusBar 在plist里增加一行 UIStatusBarStyle(或者是“Status bar style”也可以),这里可以设置 ...

  10. 关于richtextbox改变字体颜色,加下划线

    参考了三份有用的资料: 1.关于richtextbox设置字体颜色的问题 http://biancheng.dnbcw.net/c/180381.html 2.C#Winform使用扩展方法自定义富文 ...

随机推荐

  1. UTC与本地时间字符串互相转换

    #!/usr/bin/env python import time import datetime # 格式自改 UTC_FORMAT = '%Y-%m-%dT%H:%M:%SZ' LOCAL_FOR ...

  2. vs2012安装程序,无法注册ActiveX

    最近开发环境换成了vs2012,用C#写了一个ActiveX插件程序,然后添加一个安装程序,但是安装后,ie无法识别AcitveX,在ie的Manage add-ons中也找不到,这在vs2010是没 ...

  3. Mina学习之---mina整体流程介绍

    现在公司使用的NIO框架一直时候Mina,当然这也的框架还有Netty.虽然一直在用,但只是简单的停留在业务层面,最近面试的时候有问Mina相关的东西.在之前的博客中已经对BIO,NIO,AIO这三种 ...

  4. 二级ul li元素动态加载click事件

    一.代码 html代码: <ul class="id1" id="id1" style="width:84%; height:75%;overf ...

  5. 3.4-3.6 Hive Storage Format

    一.file format ORCFile在HDP 2:更好的压缩,更好的性能: https://zh.hortonworks.com/blog/orcfile-in-hdp-2-better-com ...

  6. 【网络爬虫】【python】网络爬虫(一):python爬虫概述

    python爬虫的实现方式: 1.简单点的urllib2 + regex,足够了,可以实现最基本的网页下载功能.实现思路就是前面java版爬虫差不多,把网页拉回来,再正则regex解析信息--总结起来 ...

  7. 【Hadoop】MapReduce笔记(一):MapReduce作业运行过程、任务执行

    一.MR作业运行过程 JobClient的runJob()方法:新建JobClient实例,并调用其submitJob()方法.提交作业后,runJob()每秒轮询作业进度,如果发现上次上报后信息有改 ...

  8. C#异步调用的应用实践浅谈

    C#异步调用的应用实践最经公司工作需要调用一个外部的webservice,同时要将传出的数据进行保存,以自己以前的习惯,就打算逐步操作,失败啊,完全没考虑过用户体验效果,在同事指点下,意识到使用C#异 ...

  9. [Xcode 实际操作]九、实用进阶-(18)图像人脸识别:对图片中的人像进行面部检测

    目录:[Swift]Xcode实际操作 本文将演示对图片中的人像,进行面部检测. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import UIKit //导入 ...

  10. 【BZOJ4548】小奇的糖果

    →原题传送门←(by Hzwer) 「题目背景」 小奇不小心让糖果散落到了地上,它对着满地的彩色糖果胡思乱想. 「问题描述」 有 N 个彩色糖果在平面上.小奇想在平面上取一条水平的线段,并拾起它上方或 ...