iOS 自適應高度,改變字型顏色,ios字型顏色

來源:互聯網
上載者:User

iOS 自適應高度,改變字型顏色,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: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];}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.