1. #define kMainBoundsWidth ([UIScreen mainScreen].bounds).size.width //屏幕的宽度
  2. #define kFont [UIFont systemFontOfSize:17.f] //字体大小
  3. #define kLineSpacing 7 //行间距
  1. - (void)viewDidLoad {
  2. [super viewDidLoad];
  3.  
  4. //将view背景颜色变更为黄色
  5. self.view.backgroundColor = [UIColor grayColor];
  6.  
  7. 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.";//文字内容
  8. self.textField.text = @"regarding";//高亮内容
  9. self.lableContent.text = self.textView.text;//显示内容
  10.  
  11. @weakify(self);
  12. [self.textView.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
  13. @strongify(self);
  14. self.lableContent.text = x;
  15. [self layoutLableContent:self.textField.text content:x];
  16. }];
  17.  
  18. //高亮文字
  19. [self.textField.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
  20. [self layoutLableContent:self.textField.text content:self.lableContent.text];
  21. }];
  22. }
  23.  
  24. /** 文字内容 */
  25. -(UITextView *)textView{
  26. if (!_textView) {
  27. _textView = [[UITextView alloc] init];
  28. _textView.font = [UIFont systemFontOfSize:.f];
  29. _textView.showsHorizontalScrollIndicator = YES;
  30. _textView.layer.cornerRadius = .f;
  31. _textView.layer.masksToBounds = YES;
  32. [self.view addSubview:_textView];
  33.  
  34. [_textView mas_makeConstraints:^(MASConstraintMaker *make) {
  35. make.top.equalTo(self.view).offset();
  36. make.centerX.equalTo(self.view);
  37. make.width.equalTo(self.view).multipliedBy(0.8);
  38. make.height.offset();
  39. }];
  40. }
  41. return _textView;
  42. }
  43.  
  44. /** 高亮文字 */
  45. -(UITextField *)textField{
  46. if (!_textField) {
  47. _textField = [UITextField new];
  48. _textField.placeholder = @"请输入高亮文字";
  49. _textField.layer.cornerRadius = .f;
  50. _textField.layer.masksToBounds = YES;
  51. _textField.textAlignment = NSTextAlignmentCenter;
  52. _textField.backgroundColor = [UIColor whiteColor];
  53. _textField.keyboardType = UITextAutocorrectionTypeNo;
  54. [self.view addSubview:_textField];
  55.  
  56. [_textField mas_makeConstraints:^(MASConstraintMaker *make) {
  57. make.top.equalTo(_textView.mas_bottom).offset();
  58. make.centerX.equalTo(self.view);
  59. make.width.equalTo(self.view).multipliedBy(0.5);
  60. make.height.offset();
  61. }];
  62. }
  63. return _textField;
  64. }
  65.  
  66. /** 目标文字 */
  67. -(UILabel *)lableContent{
  68. if (!_lableContent) {
  69. _lableContent = [UILabel new];
  70. _lableContent.font = kFont;
  71. _lableContent.layer.cornerRadius = .f;
  72. _lableContent.layer.masksToBounds = YES;
  73. _lableContent.layer.borderColor = [UIColor yellowColor].CGColor;
  74. _lableContent.layer.borderWidth = .f;
  75. _lableContent.backgroundColor = [UIColor whiteColor];
  76. _lableContent.numberOfLines = ;
  77. [self.view addSubview:_lableContent];
  78.  
  79. [_lableContent mas_makeConstraints:^(MASConstraintMaker *make) {
  80. make.top.equalTo(_textField.mas_bottom).offset();
  81. make.centerX.equalTo(self.view);
  82. make.width.offset(kMainBoundsWidth-);
  83. make.height.offset();
  84. }];
  85. }
  86. return _lableContent;
  87. }
  88.  
  89. /** 更新数据 */
  90. -(void)layoutLableContent:(NSString *)keyWord content:(NSString *)content{
  91. CGFloat width = kMainBoundsWidth-;
  92. CGSize contentSize = [self adaptContentStringSizeWithFont:kFont withWidth:width content:content];
  93. CGSize size = CGSizeMake(width, contentSize.height+);
  94.  
  95. [self.lableContent mas_updateConstraints:^(MASConstraintMaker *make) {
  96. make.height.offset(size.height);
  97. }];
  98. //必须调用此方法,才能出动画效果
  99. [self.view layoutIfNeeded];
  100.  
  101. NSMutableAttributedString *attributedStrContent = [[NSMutableAttributedString alloc]initWithString:self.lableContent.text];
  102. NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
  103. paragraphStyle.lineSpacing = kLineSpacing;
  104. [attributedStrContent addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(, [attributedStrContent length])];
  105. [self addAttributeColorOfSubString:keyWord inString:content tatgetString:attributedStrContent];
  106. self.lableContent.attributedText = attributedStrContent;
  107. }
  108.  
  109. /** 在指定字符串的颜色属性 */
  110. - (void)addAttributeColorOfSubString:(NSString*)subStr inString:(NSString*)content tatgetString:(NSMutableAttributedString*)attributedString {
  111. NSString*string1 = [content stringByAppendingString:subStr];
  112. NSString *temp;
  113. bool iscnChar = NO;
  114. int cnIndex = ;
  115. for(int i =; i < content.length; i++) {
  116. temp = [string1 substringWithRange:NSMakeRange(i, subStr.length)];
  117. if ([temp isEqualToString:subStr]) {
  118. NSRange range = {i,subStr.length};
  119. [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range];
  120. [attributedString addAttribute:NSFontAttributeName value:kFont range:range];
  121. }
  122.  
  123. unichar c = [string1 characterAtIndex:i];
  124. if (c >=0x4E00 && c <=0x9FA5){
  125. cnIndex = i;
  126. iscnChar = YES;
  127. NSRange range = {i, };
  128. [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:range];
  129. }
  130. }
  131. }
  132.  
  133. - (CGSize)adaptContentStringSizeWithFont:(UIFont*)font withWidth:(CGFloat)width content:(NSString *)content
  134. {
  135. //行间距
  136. NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
  137. paragraphStyle.lineSpacing = kLineSpacing;
  138.  
  139. NSDictionary *attributes = @{
  140. NSFontAttributeName:font,
  141. NSParagraphStyleAttributeName:paragraphStyle
  142. };
  143. CGSize contentSize = [content boundingRectWithSize:CGSizeMake(width, MAXFLOAT)
  144. options:NSStringDrawingUsesLineFragmentOrigin
  145. attributes:attributes
  146. context:nil].size;
  147. return contentSize;
  148. }
  149.  
  150. //取消键盘
  151. -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
  152. [self.textView resignFirstResponder];
  153. [self.textField resignFirstResponder];
  154. [self.lableContent resignFirstResponder];
  155. }

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. doc命令大全

    不是原创的,但基本上收入了各个网站dos命令了基本上可以作为电子书使用,希望对各位有用net use \\ip\ipc$ " " /user:" " 建立IPC ...

  2. py-day17-jquery

    jquery实现案例 案例: 1.点赞 <!DOCTYPE html> <html lang="en"> <head> <meta cha ...

  3. bzoj2718

    二分图匹配 首先有个定理:最长反链=最小链覆盖 最小链覆盖可以重复经过点 所以我们不能直接建图 那么我们用floyd判断是否相连 然后建图就行了 #include<bits/stdc++.h&g ...

  4. UnicodeEncodeError: 'ascii' codec can't encode character u'\u65e0' in position 1: ordinal not in range(128)

    UnicodeEncodeError: 'ascii' codec can't encode character u'\u65e0' in position 1: ordinal not in ran ...

  5. python 之队列

    进程和线程模块下都有队列类. 线程队列: # 后进先出->堆栈 q=queue.LifoQueue(3) # 优先级队列,数字越小优先级越高 q=queue.PriorityQueue(3) 进 ...

  6. c++中IO输入输出流总结<一>

    1 io类图关系 1.1 简化形式 1.1.2补充 iostream: istream:从流中读取 ostream:写入到流 iosteram:读写流 fstream: ifstream:从文件读 o ...

  7. python 并发编程之IO 模型

    首先说一下 IO 发生时涉及的对象和步骤.以read 为例,会经历两个阶段: 1)等待数据准备 2)将数据从内核拷贝到进程中 二,阻塞Io(blocking IO) 在 Linux中  默认情况下所有 ...

  8. C#项目中一些文件类型说明

    designer.cs  是窗体设计器生成的代码文件,作用是对窗体上的控件做初始化工作 (在函数InitializeComponent()中)VS2003以前都把这部分代码放到窗体的cs文件中,由于这 ...

  9. JAVA企业级开发--jsp,el,jstl(14)

    三. EL表达式语言 EL 全名为Expression Language.表达式语言.jsp2.0之后才引入的. EL主要作用:替代:<%= %>     out.print(); 获取数 ...

  10. github新手使用

    1.首先要先在github的官网注册一个属于自己的账号.https://github.com/ 2.注册完成后需要一些简单的设置,先创建一个属于自己的仓库,repository 3.创建仓库 4.如何 ...