zoukankan      html  css  js  c++  java
  • IOS7-TextKit

    -------------------------------------------TextKitDemo---------------------------------------

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        
        _textView = [[UITextView alloc]initWithFrame:CGRectMake(0,20,320, 200)];
        _textView.text = @"sldjdsflsdflsflskfls水电费了解多少分 双方就发生了地方我 都是浪费了多少积分离开多少份简历上发动机我收到了房间里的减肥了手机费我lfsdjfdfks发生的楼房的i地上了飞机了时间了i是的房间里的手机发送了地方历史的房价都是浪费";
        _textView.font = [UIFont systemFontOfSize:14];
        [self.view addSubview:_textView];
        
        NSTextStorage *textStorage = [[NSTextStorage alloc]initWithString:_textView.text];//用于文本存储字符和相关属性
        
        
        NSLayoutManager *layoutManager = [[NSLayoutManager alloc]init];//用于管理文本存储
        [textStorage addLayoutManager:layoutManager];
        
        
        CGRect textViewRect = CGRectInset(self.view.bounds, 10.0, 20.0);//(10, 20,300, 440),view的宽减20,高减40。
        NSLog(@"rect = %@",NSStringFromCGRect(textViewRect));
        _textContainer = [[NSTextContainer alloc]initWithSize:textViewRect.size];//排版区域
        [layoutManager addTextContainer:_textContainer];
        
        
        [_textView removeFromSuperview];
        _textView = [[UITextView alloc]initWithFrame:textViewRect textContainer:_textContainer];/*根据排版区域初始化*/
        [self.view addSubview:_textView];
        
        //设置印刷效果
        [textStorage beginEditing];
        
        NSDictionary *attrsDic = @{NSTextEffectAttributeName: NSTextEffectLetterpressStyle};
        NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc]initWithString:_textView.text  attributes:attrsDic];
        [textStorage setAttributedString:attrStr];
        
        [self markWord:@"" inTextStorage:textStorage];
        [self markWord:@"i" inTextStorage:textStorage];
        
        [textStorage endEditing];
        
        //2.嵌入图片
        _tImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"combg_btn@2x"]];
        _tImageView.frame = CGRectMake(80,46,150, 70);
        [self.view addSubview:_tImageView];
        //
        [self.view insertSubview:_textView belowSubview:_tImageView];
        _textView.textContainer.exclusionPaths = @[[self translateBezierPath]];/*设置环绕路径*/
        
        //3监听用户设置字体大小
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(preferredContentSizeChanged:) name:UIContentSizeCategoryDidChangeNotification object:nil];
    }
    
    -(void)preferredContentSizeChanged:(NSNotification *)notification
    {
        self.textView.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
    }
    
    -(void)markWord:(NSString *)word inTextStorage:(NSTextStorage *)textStorage
    {
        NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:word options:0 error:nil];
        NSArray *matches = [regex matchesInString:_textView.text options:0 range:NSMakeRange(0, [_textView.text length])];
        
        for (NSTextCheckingResult *match in matches)
        {
            NSRange matchRange = [match range];
            //改变属性匹配出来的i和我字符
            [textStorage addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:matchRange];
        }
        
    }
    
    -(UIBezierPath *)translateBezierPath
    {
        CGRect imageRect = [self.textView convertRect:_tImageView.frame fromView:self.view];
        UIBezierPath *newPath = [UIBezierPath bezierPathWithRect:imageRect];
        return newPath;
    }

     

    2.label设置内容样式

      NSString *content=[NSString stringWithFormat:@"订货卡片条款号 : %@",text];
                //设置段落样式
                NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
                [paragraphStyle setAlignment:NSTextAlignmentCenter];
                [paragraphStyle setParagraphSpacing:34];
                NSDictionary *para = @{NSParagraphStyleAttributeName:paragraphStyle};
                //设置文字样式
                NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:content
                                                                                               attributes:para];
                NSRange range = [content rangeOfString:text];
                [attrString addAttribute:NSForegroundColorAttributeName
                                   value:[UIColor cyanColor]
                                   range:range];
                UIFont *font = [UIFont systemFontOfSize:23];
                [attrString addAttribute:NSFontAttributeName
                                   value:font
                                   range:range];
                self.tipLabel.attributedText = attrString;
                weakSelf.tipLabel.text =content;
  • 相关阅读:
    Entity Framework 批量操作
    Tsak多线程学习记录
    .net webservice 动态更换地址
    .NET EF Core2.0 (DBFirst)使用与配置
    MVC发布IIS后提示未配置默认文档
    未能加载文件或程序集“Microsoft.Web.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”或它的某一个依赖项。系统找不到指定的文件。
    Redis教程
    使用git将项目上传到github
    Redis集群(一)搭建Cluster模式[超简单]
    Redis 常见5大数据类型结构,附录3个数据类型
  • 原文地址:https://www.cnblogs.com/huen/p/3954824.html
Copyright © 2011-2022 走看看