码迷,mamicode.com
首页 > 移动开发 > 详细

IOS7-TextKit

时间:2014-09-07 23:39:15      阅读:302      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   io   ar   for   div   

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

标签:style   blog   http   color   os   io   ar   for   div   

原文地址:http://www.cnblogs.com/huen/p/3954824.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!