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使用扩展方法自定义富文 ...
随机推荐
- AutoIt中ControlFocus的使用
在使用AutoIt最控件做自动化操作的时候,经常性的会碰到无法使用Windows Info工具获取控件的属性,但是我们又需要获取该控件的焦点,我们该怎么办呢? 方法1: 应用controlFocus方 ...
- .Net框架中的CLR,CTS,ClS的解释
CLR的全称(Common Language Runtime) 公共语言运行时 可以把它理解为包含运行.Net程序的引擎 和 一堆符合公用语言基础(CLI)的类库的集合,他是一个规范的实现,我们开发的 ...
- HDOJ-2058
The sum problem Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- g2o使用bug总结
g2o进行3d2d优化的时候,设置优化图的边时,注意setVertex()中顶点的顺序. void setVertex(size_t i, Vertex* v) { assert(i < _ve ...
- Flutter实战视频-移动电商-27.列表页_现有Bug修复和完善
27.列表页_现有Bug修复和完善 小解决小bug 默认右侧的小类没有被加载 数据加载完成后,就list的第一个子对象传递给provide进行赋值,这样右侧的小类就刷新了数据 默认加载了第一个类别 调 ...
- Flutter实战视频-移动电商-39.路由_Fluro的路由配置和静态化
39.路由_Fluro的路由配置和静态化 handler只是单个路由的配置,这节课我们要学习路由的整体配置 整体配置 新建routers.dart文件来做整体配置 detailsHandler就是我们 ...
- 真正可用的Asdoc生成工具及技巧和注意事项
经过一个上午的折腾,终于总结出一套完全能够正确运行得到asdoc api的工具.使用方便,里面写了许多注意事项,帮你顺利完成 api 的生成. :: 注意: :: 1.所以 ...
- VS中用C#开发应用程序的调试入门、技巧和实例(转载)
入门篇 假设你是有着.Net平台的程序员,并且使用Visual Studio 做为开发工具. 断点:最简单的一种,设置一个断点,程序执行到那一句就自动中断进入调试状态.设置断点,在你觉得有问题的代码行 ...
- 利用StringBuffer来替换内容
package com.test.java; public class StringBufferTest { public static void main(String[] args) { Stri ...
- ZOJ3158 【DFS】
题意: 从上往下切割,不能切边缘,然后问你两个差值,要小于给出的值. 思路: 直接爆搜,枚举每一行的劈开位置: #include<bits/stdc++.h> using namespac ...