zoukankan      html  css  js  c++  java
  • iOS.TextKit.01.凸版印刷效果

    1、案例视图,如下图

    2、代码

    TextKit01ViewController.h
    #import <UIKit/UIKit.h>
    
    @interface TextKit01ViewController : UIViewController
    
    @property (nonatomic,strong) IBOutlet UITextView *textView;
    // 文本可以排版的区域
    @property (nonatomic,strong) NSTextContainer *textContainer;
    // 设置文本风格
    - (void) markWord:(NSString*)word inTextStorage:(NSTextStorage*)textStorage;
    
    @end
    TextKit01ViewController.m
    #import "TextKit01ViewController.h"
    
    @interface TextKit01ViewController ()
    
    @end
    
    @implementation TextKit01ViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        
        CGRect textViewRect = CGRectInset(self.view.bounds, 10.0, 20.0);
        
        // 1、创建储存文本对象textStorage
        NSTextStorage *textStorage = [[NSTextStorage alloc] initWithString:self.textView.text];
        
        // 2、创建文字排版对象layoutManager
        NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
        
        // 3、创建文本排版的区域textContainer
        self.textContainer = [[NSTextContainer alloc] initWithSize:textViewRect.size];
        
        // 4、设置textStorage与layoutManager的关系
        [textStorage addLayoutManager:layoutManager];
        
        // 5、设置layoutManager与textContainer的关系
        [layoutManager addTextContainer:self.textContainer];
        
        // 6、重新构建原来的textview控件
        [self.textView removeFromSuperview];
        self.textView = [[UITextView alloc] initWithFrame:textViewRect textContainer:self.textContainer];
        [self.view addSubview:self.textView];
        
        // 7、设置textStorage中文本的风格
        [textStorage beginEditing];
        
        NSDictionary *attrsDic = @{NSTextEffectAttributeName:NSTextEffectLetterpressStyle};
        NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:self.textView.text attributes:attrsDic];
        [textStorage setAttributedString:attrStr];
        
        [self markWord:@"" inTextStorage:textStorage];
        [self markWord:@"I" inTextStorage:textStorage];
        
        [textStorage endEditing];
        
    }
    
    -(void)markWord:(NSString *)word inTextStorage:(NSTextStorage *)textStorage
    {
        // 1、创建正则表达式regex
        NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:word options:0 error:nil];
        
        // 2、扫描textStorage中的文本
        NSArray *matches = [regex matchesInString:self.textView.text options:0 range:NSMakeRange(0, [self.textView.text length])];
        
        // 3、为找到的文本内容设置风格
        for (NSTextCheckingResult *match in  matches) {
            NSRange matchRange = [match range];
            [textStorage addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:matchRange];
        }
    }
    
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    @end
     
  • 相关阅读:
    utools很方便的软件,你的生产力工具集
    uos、deepin安装nfs、查看nfs日志路径
    91--spring cloud (luceneAPI-solr)
    91--spring cloud (Config配置中心--客户端)
    90--spring cloud (Config配置中心)
    90--IDEA整合Git
    解决 IDEA控制台中不能输入数据
    89--spring cloud (zuul-API网关/与feign的比较)
    88--spring cloud (Springboot 整合RabbitMQ)
    88--spring cloud (Turbine聚合监控)
  • 原文地址:https://www.cnblogs.com/cqchen/p/3776176.html
Copyright © 2011-2022 走看看