IOS adaptive height, font color change, ios font color

Source: Internet
Author: User

IOS adaptive height, font color change, ios font color

 

 

# Define kMainBoundsWidth ([UIScreen mainScreen]. bounds). size. width // screen width # define kFont [UIFont systemFontOfSize: 17.f] // font size # define kLineSpacing 7 // row spacing
-(Void) viewDidLoad {[super viewDidLoad]; // change the view background color to yellow 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 shoshould weakify it and strongify it again in the block so you don't get into a retain cycle. "; // text content self. textField. text = @ "regarding"; // highlight content self. lableContent. text = self. textView. text; // display Content @ weakify (self); [self. textView. rac_textSignal subscribeNext: ^ (NSString * _ Nullable x) {@ strongify (self); self. lableContent. text = x; [self layoutLableContent: self. textField. text content: x] ;}]; // highlight text [self. textField. rac_textSignal subscribeNext: ^ (NSString * _ Nullable x) {[self layoutLableContent: self. textField. text content: self. lableContent. text] ;}] ;}/ ** text content */-(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. similar to (self. view ). offset (80); make. centerX. similar to (self. view); make. width. Similar to (self. view ). multi-pliedby (0.8); make. height. offset (100);}];} return _ textView;}/** highlighted text */-(UITextField *) textField {if (! _ TextField) {_ textField = [UITextField new]; _ textField. placeholder = @ "Please enter the highlighted text"; _ 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. similar to (_ textView. mas_bottom ). offset (30); make. centerX. similar to (self. view); make. width. similar to (self. view ). multi-pliedby (0.5); make. height. offset (50) ;}] ;}return _ textField ;}/ ** target text */-(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. pointer to (_ textField. mas_bottom ). offset (50); make. centerX. similar to (self. view); make. width. offset (kMainBoundsWidth-40); make. height. offset (200);}];} return _ lableContent;}/** update data */-(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) ;}]; // you must call this method to obtain the animation effect [self. view layoutIfNeeded]; NSMutableAttributedString * attributedStrContent = [[NSMutableAttributedString alloc] initWithString: self. lableContent. text]; NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init]; paragraphStyle. lineSpacing = attributes; [attributedStrContent addattriange: Attributes value: paragraphStyle range: NSMakeRange (0, [attributedStrContent length])]; [self attributes: keyWord inString: content tatgetString: attributedStrContent]; self. lableContent. attributedText = attributedStrContent;}/** in the color attribute of the specified string */-(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 ishangtostring: subStr]) {nsange 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; nsange range = {I, 1}; [attributedString addAttribute: NSForegroundColorAttributeName value: [UIColor blueColor] range: range] ;}}- (CGSize) adaptContentStringSizeWithFont :( UIFont *) font withWidth :( CGFloat) width content :( NSString *) content {// row spacing NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init]; paragraphStyle. lineSpacing = kLineSpacing; NSDictionary * attributes =@{ attributes: font, attributes: paragraphStyle}; CGSize contentSize = [content attributes: CGSizeMake (width, MAXFLOAT) options: attributes context: nil]. size; return contentSize;} // cancel the keyboard-(void) touchesBegan :( NSSet <UITouch *> *) touches withEvent :( UIEvent *) event {[self. textView resignFirstResponder]; [self. textField resignFirstResponder]; [self. lableContent resignFirstResponder];}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.