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使用扩展方法自定义富文 ...
随机推荐
- 如何使用 Jmeter 发送 Json 请求
公司最近有一个项目,需要持续发送大量的 Json 请求到服务器,从而测试服务器可靠性. 我就发送 Json 请求部分发布这个博客. 一般来说, Json 请求的数据都保存到 CSV 文件中,然后使用 ...
- fragment error
error: android.view.InflateException: Binary XML file line #6: Error inflating class fragment 解决办法 ...
- JavaScript-Tool-导向:wizard-un
ylbtech-JavaScript-Tooll-导向:wizard 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 6.返回顶部 作者:ylbtec ...
- ubuntu12.04下安装搜狗拼音
Ubuntu 12.04 LTS 版本 由于 Ubuntu 12.04 LTS 自带的 Fcitx 版本较旧,需要先通过 PPA 升级,才能安装下载的 deb 软件包. 1. 点击左上角的图标 ...
- Ubuntu 下编译Android 源代码
1.配置JDK 1.6 或者1.7(看情况配置,有的Android版本不能在1.7下运行) 2.配置环境:终端:(CTRL+ALT+T) $ sudo apt-get install git gnup ...
- Python_两种导入模块的方法异同
Python中有两种导入模块的方法 1:import module 2:from module import * 使用from module import *方法可以导入独立的项,也可以用from m ...
- css一些不为人所熟知的知识点
1.设置a标签内字体水平居中:text-algin:center 2.设置a标签内字体水平居中:line-height:height 3.如何设置td宽度固定<table style=" ...
- JAVA通过信号量避免死锁
死锁是这样一种情形:多个线程同时被阻塞,它们中的一个或者全部都在等待某个资源被释放.由于线程被无限期地阻塞,因此程序不可能正常终止. 导致死锁的根源在于不适当地运用"synchronized ...
- 利用JavaScript选择GridView行
本篇技巧和诀窍记录的是:利用JavaScript选择GridView行. 当我们想在GridView中添加删除.选择功能时,我们通常的做法是利用模板功能在每行添加一个按钮控件或者超链接按钮控件,单击按 ...
- 一个github搞定微信小程序支付系列
详情请前往github下载示例代码 源码中包含 支付.退款 功能 so easy,项目经理再也不用担心微信支付啦 是的,已经over了