当前位置: 移动技术网 > 移动技术>移动开发>IOS > iOS 自适应高度,改变字体颜色

iOS 自适应高度,改变字体颜色

2018年03月06日  | 移动技术网移动技术  | 我要评论

 

 

#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:15.f];
        _textView.showsHorizontalScrollIndicator = YES;
        _textView.layer.cornerRadius = 4.f;
        _textView.layer.masksToBounds = YES;
        [self.view addSubview:_textView];
        
        [_textView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view).offset(80);
            make.centerX.equalTo(self.view);
            make.width.equalTo(self.view).multipliedBy(0.8);
            make.height.offset(100);
        }];
    }
    return _textView;
}

/** 高亮文字 */
-(UITextField *)textField{
    if (!_textField) {
        _textField = [UITextField new];
        _textField.placeholder = @"请输入高亮文字";
        _textField.layer.cornerRadius = 4.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(30);
            make.centerX.equalTo(self.view);
            make.width.equalTo(self.view).multipliedBy(0.5);
            make.height.offset(50);
        }];
    }
    return _textField;
}

/** 目标文字 */
-(UILabel *)lableContent{
    if (!_lableContent) {
        _lableContent = [UILabel new];
        _lableContent.font = kFont;
        _lableContent.layer.cornerRadius = 4.f;
        _lableContent.layer.masksToBounds = YES;
        _lableContent.layer.borderColor = [UIColor yellowColor].CGColor;
        _lableContent.layer.borderWidth = 1.f;
        _lableContent.backgroundColor = [UIColor whiteColor];
        _lableContent.numberOfLines = 0;
        [self.view addSubview:_lableContent];
        
        [_lableContent mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_textField.mas_bottom).offset(50);
            make.centerX.equalTo(self.view);
            make.width.offset(kMainBoundsWidth-40);
            make.height.offset(200);
        }];
    }
    return _lableContent;
}

/** 更新数据 */
-(void)layoutLableContent:(NSString *)keyWord content:(NSString *)content{
    CGFloat width = kMainBoundsWidth-40;
    CGSize contentSize = [self adaptContentStringSizeWithFont:kFont withWidth:width content:content];
    CGSize size = CGSizeMake(width, contentSize.height+25);
    
    [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(0, [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 = 0;
    for(int i =0; 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, 1};
            [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];
}

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网