YYText使用篇(十)

版本记录

版本号 时间
V1.0 2017.06.11

前言

YYText是一个专门处理文字的框架,有了它处理文字变得非常方便,这一篇我继续介绍YYText的使用方法,希望对大家能有所帮助。大家如感兴趣还可以参考:
1.YYText使用篇(一)
2.YYText使用篇(二)
3.YYText使用篇(三)
4.YYText使用篇(四)
5.YYText使用篇(五)
6.YYText使用篇(六)
7.YYText使用篇(七)
8.YYText使用篇(八)
9.YYText使用篇(九)
下面给出的实例就是Text-Binding和copy-paste展示。

一、YYText示例之text-binding

下面看示例代码

#import "YYTextBindingExample.h"
#import "YYText.h"
#import "YYImage.h"
#import "UIImage+YYWebImage.h"
#import "UIView+YYAdd.h"
#import "NSBundle+YYAdd.h"
#import "NSString+YYAdd.h"
#import "YYTextExampleHelper.h"


@interface YYTextExampleEmailBindingParser :NSObject <YYTextParser>

@property (nonatomic, strong) NSRegularExpression *regex;

@end

@implementation YYTextExampleEmailBindingParser

#pragma mark - Override Base Function

- (instancetype)init
{
    self = [super init];
    
    NSString *pattern = @"[-_a-zA-Z@\\.]+[ ,\\n]";
    self.regex = [[NSRegularExpression alloc] initWithPattern:pattern options:kNilOptions error:nil];
    return self;
}

#pragma mark - YYTextParser

- (BOOL)parseText:(NSMutableAttributedString *)text selectedRange:(NSRangePointer)range
{
    __block BOOL changed = NO;
    [_regex enumerateMatchesInString:text.string options:NSMatchingWithoutAnchoringBounds range:text.yy_rangeOfAll usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
        if (!result) return;
        NSRange range = result.range;
        if (range.location == NSNotFound || range.length < 1) return;
        if ([text attribute:YYTextBindingAttributeName atIndex:range.location effectiveRange:NULL]) return;
        
        NSRange bindlingRange = NSMakeRange(range.location, range.length - 1);
        YYTextBinding *binding = [YYTextBinding bindingWithDeleteConfirm:YES];
        [text yy_setTextBinding:binding range:bindlingRange]; /// Text binding
        [text yy_setColor:[UIColor colorWithRed:0.000 green:0.519 blue:1.000 alpha:1.000] range:bindlingRange];
        changed = YES;
    }];
    return changed;
}

@end

@interface YYTextBindingExample () <YYTextViewDelegate>

@property (nonatomic, strong) YYTextView *textView;
@property (nonatomic, assign) BOOL isInEdit;

@end

@implementation YYTextBindingExample

#pragma mark - Override Base Function

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    if ([self respondsToSelector:@selector(setAutomaticallyAdjustsScrollViewInsets:)]) {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:@"sjobs@apple.com, apple@apple.com, banana@banana.com, pear@pear.com "];
    text.yy_font = [UIFont systemFontOfSize:17];
    text.yy_lineSpacing = 5;
    text.yy_color = [UIColor blackColor];
    
    YYTextView *textView = [YYTextView new];
    textView.attributedText = text;
    textView.textParser = [YYTextExampleEmailBindingParser new];
    textView.size = self.view.size;
    textView.textContainerInset = UIEdgeInsetsMake(10, 10, 10, 10);
    textView.delegate = self;
    if (kiOS7Later) {
        textView.keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive;
    }
    textView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);
    textView.scrollIndicatorInsets = textView.contentInset;
    [self.view addSubview:textView];
    self.textView = textView;
    [self.textView becomeFirstResponder];
    
}

#pragma mark - Action && Notification

- (void)edit:(UIBarButtonItem *)item
{
    if (_textView.isFirstResponder) {
        [_textView resignFirstResponder];
    }
    else {
        [_textView becomeFirstResponder];
    }
}

#pragma mark - YYTextViewDelegate

- (void)textViewDidChange:(YYTextView *)textView
{
    if (textView.text.length == 0) {
        textView.textColor = [UIColor blackColor];
    }
}

- (void)textViewDidBeginEditing:(YYTextView *)textView
{
    UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
                                                                                target:self
                                                                                action:@selector(edit:)];
    self.navigationItem.rightBarButtonItem = buttonItem;
}

- (void)textViewDidEndEditing:(YYTextView *)textView
{
    self.navigationItem.rightBarButtonItem = nil;
}

@end

下面看效果示意图

效果示意图

二、YYText示例之copy-paste

下面看示例代码

#import "YYTextCopyPasteExample.h"
#import "YYText.h"
#import "YYImage.h"
#import "UIImage+YYWebImage.h"
#import "UIView+YYAdd.h"
#import "NSBundle+YYAdd.h"
#import "NSString+YYAdd.h"
#import "YYTextExampleHelper.h"

@interface YYTextCopyPasteExample ()<YYTextViewDelegate>

@property (nonatomic, assign) YYTextView *textView;

@end

@implementation YYTextCopyPasteExample

#pragma mark - Override Base Function

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    if ([self respondsToSelector:@selector(setAutomaticallyAdjustsScrollViewInsets:)]) {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    
    NSString *text = @"You can copy image from browser or photo album and paste it to here. It support animated GIF and APNG. \n\nYou can also copy attributed string from other YYTextView.\n";
    
    YYTextSimpleMarkdownParser *parser = [YYTextSimpleMarkdownParser new];
    [parser setColorWithDarkTheme];
    
    YYTextView *textView = [YYTextView new];
    textView.text = text;
    textView.font = [UIFont systemFontOfSize:17];
    textView.size = self.view.size;
    textView.textContainerInset = UIEdgeInsetsMake(10, 10, 10, 10);
    textView.delegate = self;
    textView.allowsPasteImage = YES; /// Pasts image
    textView.allowsPasteAttributedString = YES; /// Paste attributed string
    if (kiOS7Later) {
        textView.keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive;
    }
    textView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);
    textView.scrollIndicatorInsets = textView.contentInset;
    [self.view addSubview:textView];
    self.textView = textView;
    
    textView.selectedRange = NSMakeRange(text.length, 0);
    [textView becomeFirstResponder];
}

#pragma mark - Action && Notification

- (void)edit:(UIBarButtonItem *)item
{
    if (_textView.isFirstResponder) {
        [_textView resignFirstResponder];
    }
    else {
        [_textView becomeFirstResponder];
    }
}

#pragma mark - YYTextViewDelegate

- (void)textViewDidBeginEditing:(YYTextView *)textView
{
    UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
                                                                                target:self
                                                                                action:@selector(edit:)];
    self.navigationItem.rightBarButtonItem = buttonItem;
}

- (void)textViewDidEndEditing:(YYTextView *)textView
{
    self.navigationItem.rightBarButtonItem = nil;
}

@end

下面看示意图

示意图

  上面可以看到:这里可以从浏览器和相册中粘贴图片,支持gif和APNG格式的图片。

后记

未完,待续~~~

山水
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容