#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. kafka数据可靠性深度解读【转】

    1 概述 Kakfa起初是由LinkedIn公司开发的一个分布式的消息系统,后成为Apache的一部分,它使用Scala编写,以可水平扩展和高吞吐率而被广泛使用.目前越来越多的开源分布式处理系统如Cl ...

  2. Invalid content was found starting with element 'mvc:exclude-mapping'.

    问题?Invalid content was found starting with element 'mvc:exclude-mapping'. 这是springmvc中显著的错误,在配置拦截器的时 ...

  3. hibernate Criteria中or和and的用法

    /s筛选去除无效数据 /*      detachedCriteria.add( Restrictions.or( Restrictions.like("chanpin", &qu ...

  4. error:: undefined reference to symbol '__glewBufferSubData' 未定义的引用 以及 error: main.o: undefined reference to symbol 'glTexImage2D'

    在把DSO移植到QT工程中,出现了 /usr/bin/ld: KeyFrameDisplay.o: undefined reference to symbol '__glewBufferSubData ...

  5. Java简单的数组用法尝试,和C语言很不一样

    public class Main { static int ARRY_LONGTH=100; static int[] getRandomArr(int n){ int[] randomArr; r ...

  6. vue中excel导入导出组件

    vue中导入导出excel,并根据后台返回类型进行判断,导入到数据库中 功能:实现js导入导出excel,并且对导入的excel进行展示,当excel标题名称和数据库的名称标题匹配时,则对应列导入的数 ...

  7. Git之常用的命令操作

    set LESSCHARSET=utf-8 git设置文件名大小写敏感 git branch -rgit checkout 'TestMaster'git checkout -b 'TestMaste ...

  8. Codeforces691A【读题-水】

    妈蛋wa了两次.. 时尚的定义是length大于1的要破个洞,一定要破个洞.. According to rules of the Berland fashion, a jacket should b ...

  9. Node.js 自定义模块

    Node.js内置多个模块,也可以使用第三方模块,今天学习一下如何使用自己定义的模块 在同级目录下定义两个js文件 第一个:custom1.js "use strict"; fun ...

  10. web框架原理,http 协议

    目录 web框架原理 web框架是什么东西 执行代码用浏览器访问一下 输出结果 http 协议 http 协议简介 http 协议概述 http 工作原理 http请求方法 http 状态码 url介 ...