iOS 自适应高度,改变字体颜色
#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 自适应高度,改变字体颜色的更多相关文章
- markdown改变字体颜色和大小
markdown中改变字体颜色与大小方法同html 先看例子 <font face="黑体">我是黑体字</font> 我是黑体字 <font fac ...
- 色彩缤纷的python(改变字体颜色及样式不完全版)
色彩缤纷的python(改变字体颜色及样式) *补上昨天随笔中提到的改变字体颜色样式的方法,昨日随笔https://www.cnblogs.com/Du704/p/11265958.html 在项目过 ...
- 色彩缤纷的Python(改变字体颜色及样式)
色彩缤纷的python(改变字体颜色及样式) 在项目过程中,我们常常会因为输出信息的颜色与样式过于单调以至于让人在视觉上感到很杂乱,所以看下文: 在Linux终端中,使用转义序列来进行如上所述的显示, ...
- Android NumberPicker 修改分割线颜色和高度及字体颜色大小
(1)重写NumberPicker已达到修改显示字体颜色大小 public class TextColorNumberPicker extends NumberPicker { public Text ...
- RadioGroup 的 RadioButton 选择改变字体颜色和背景颜色
RadioGroup <RadioGroup android:id="@+id/client_charge_radiogroup" android:layout_width= ...
- Android 改变字体颜色的三种方法
在TextView中添加文本时有时需要改变一些文本字体的颜色,今天学到了三种方法,拿出来分享一下 1.在layout文件下的配置xml文件中直接设置字体颜色,通过添加android:textc ...
- Android RadioGroup的RadioButton 选择改变字体颜色和背景颜色
RadioGroup <RadioGroup android:id="@+id/client_charge_radiogroup" android:layout_width= ...
- iOS 设置不同的字体颜色
//设置不同字体颜色 -(void)fuwenbenLabel:(UILabel *)labell FontNumber:(UIFont *)font AndRange:(NSRange)range ...
- iOS设置状态栏的字体颜色
设置statusBar的[前景色部分] 1.plist设置statusBar 在plist里增加一行 UIStatusBarStyle(或者是“Status bar style”也可以),这里可以设置 ...
- 关于richtextbox改变字体颜色,加下划线
参考了三份有用的资料: 1.关于richtextbox设置字体颜色的问题 http://biancheng.dnbcw.net/c/180381.html 2.C#Winform使用扩展方法自定义富文 ...
随机推荐
- Win32编程点滴5 - 响应ActiveX控件的事件
在最近的一篇文章中说到了,如何创建ActiveX,这次我们来响应事件.这次,我们将创建一个类:CGeneralEventSink,它能够响应任何Dispatch事件(事件的接口继承与IDispatch ...
- HttpClient入门教程
HttpClient使用详解与实战一:https://www.jianshu.com/p/375be5929bed
- sublimelinter-php 错误代码提示
先安装 SublimeLinter 如同其他插件一样使用 Package Control 来安装. 按下 Ctrl+Shift+p 进入 Command Palette 输入install进入 Pac ...
- Python_XML的三种解析方法
什么是XML? XML 指可扩展标记语言(eXtensible Markup Language). XML 被设计用来传输和存储数据. XML是一套定义语义标记的规则,这些标记将文档分成许多部件并对这 ...
- World CodeSprint 10
C: 题意: 给定一个长度为 $n$ 的序列 $a_i$,从 $a$ 序列中选出一个大小为 $k$ 的子序列使得子序列数字的 bitwise AND 值最大. 求问最大值是多少,并求出有多少个最大值 ...
- Jacob开发文件转PDF
这三种方法我都有试过word转PDF,第2种.第3种对于图片,表格.中文转换效果都不好,方法1效果最好.但方法1 只支持Windows环境下. 1.开发环境 Windows系统: 2.准备工作: st ...
- java单例的几种写法
转载出处:http://cantellow.javaeye.com/blog/838473 第一种(懒汉,线程不安全): public class Singleton { private static ...
- 13.详解oauth2授权码流程
13.详解oauth2授权码流程 把登陆系统单独独立出来,可以给自己写的微服务用,也可以给第三方的系统调用我们的服务 显式的和隐式的,两种方式,
- HDU3279【水】
思路: 求数组里的第三大: #include <bits/stdc++.h> using namespace std; typedef long long LL; int a[15]; i ...
- LightOJ1197【数学】
引自:WONTER 题意: 给你两个数,a,b,让你求区间[a,b]里面有多少个素数: 思路: 首先要知道,我们要想筛 [1, b] 中所有的素数,只需要用到 [1, sqrt(b)] 中的所有素数来 ...