首页 > 代码库 > IOS7-TextKit

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;}

 Demo下载:https://github.com/forrHuen/UITextKit

IOS7-TextKit