YYText使用篇(六)

版本記錄

版本號 時間
V1.0 2017.06.08

前言

YYText是一個專門處理文字的框架,有了它處理文字變得非常方便,這一篇我繼續介紹YYText的使用方法,希望對大家能有所幫助。大家如感興趣還可以參考:
1.YYText使用篇(一)
2.YYText使用篇(二)
3.YYText使用篇(三)
4.YYText使用篇(四)
5.YYText使用篇(五)

一、YYText示例

下面看這個示例

#import "YYTextTagExample.h"
#import "YYText.h"
#import "UIView+YYAdd.h"
#import "YYTextExampleHelper.h"

@interface YYTextTagExample () <YYTextViewDelegate>
@property (nonatomic, assign) YYTextView *textView;
@end

@implementation YYTextTagExample

#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 new];
    NSArray *tags = @[@"?red", @"?orange", @"?yellow", @"?green", @"?blue", @"?purple", @"?gray"];
    NSArray *tagStrokeColors = @[
        UIColorHex(fa3f39),
        UIColorHex(f48f25),
        UIColorHex(f1c02c),
        UIColorHex(54bc2e),
        UIColorHex(29a9ee),
        UIColorHex(c171d8),
        UIColorHex(818e91)
    ];
    NSArray *tagFillColors = @[
        UIColorHex(fb6560),
        UIColorHex(f6a550),
        UIColorHex(f3cc56),
        UIColorHex(76c957),
        UIColorHex(53baf1),
        UIColorHex(cd8ddf),
        UIColorHex(a4a4a7)
    ];

    UIFont *font = [UIFont boldSystemFontOfSize:16];
    
    for (int i = 0; i < tags.count; i++) {
        NSString *tag = tags[i];
        UIColor *tagStrokeColor = tagStrokeColors[i];
        UIColor *tagFillColor = tagFillColors[i];
        NSMutableAttributedString *tagText = [[NSMutableAttributedString alloc] initWithString:tag];
        [tagText yy_insertString:@"   " atIndex:0];
        [tagText yy_appendString:@"   "];
        tagText.yy_font = font;
        tagText.yy_color = [UIColor whiteColor];
        [tagText yy_setTextBinding:[YYTextBinding bindingWithDeleteConfirm:NO] range:tagText.yy_rangeOfAll];
        
        YYTextBorder *border = [YYTextBorder new];
        border.strokeWidth = 1.5;
        border.strokeColor = tagStrokeColor;
        border.fillColor = tagFillColor;
        border.cornerRadius = 100; // a huge value
        
        /* Line join styles. */
//        typedef CF_ENUM(int32_t, CGLineJoin) {
//            kCGLineJoinMiter,
//            kCGLineJoinRound,
//            kCGLineJoinBevel
//        };
        border.lineJoin = kCGLineJoinBevel; //線的鏈接方式
        border.insets = UIEdgeInsetsMake(-2, -5.5, -2, -8);
        [tagText yy_setTextBackgroundBorder:border range:[tagText.string rangeOfString:tag]];
        
        [text appendAttributedString:tagText];
    }
    text.yy_lineSpacing = 10;
    text.yy_lineBreakMode = NSLineBreakByWordWrapping;
    
    [text yy_appendString:@"\n"];
    [text appendAttributedString:text]; // repeat for test
    
    YYTextView *textView = [YYTextView new];
    textView.attributedText = text;
    textView.size = self.view.size;
    textView.textContainerInset = UIEdgeInsetsMake(10 + 64, 10, 10, 10);
    textView.allowsCopyAttributedString = YES;
    textView.allowsPasteAttributedString = YES;
    textView.delegate = self;
    if (kiOS7Later) {
        textView.keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive;
    } else {
        textView.height -= 64;
    }
    textView.scrollIndicatorInsets = textView.contentInset;
    textView.selectedRange = NSMakeRange(text.length, 0);
    [self.view addSubview:textView];
    self.textView = textView;
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.6 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [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


二、YYText示例結果

根據上面的代碼,我們給出效果示意圖。

YYText示例結果1
YYText示例結果2

后記

未完,待續~~~~

一棵樹
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 版本記錄 前言 YYText是一個專門處理文字的框架,有了它處理文字變得非常方便,這一篇我繼續介紹YYText的使...
    刀客傳奇閱讀 2,904評論 0 2
  • 版本記錄 前言 YYText是一個專門處理文字的框架,有了它處理文字變得非常方便,這一篇我繼續介紹YYText的使...
    刀客傳奇閱讀 1,853評論 9 4
  • 版本記錄 前言 YYText是一個專門處理文字的框架,有了它處理文字變得非常方便,這一篇我繼續介紹YYText的使...
    刀客傳奇閱讀 2,052評論 0 1
  • 版本記錄 前言 YYText是一個專門處理文字的框架,有了它處理文字變得非常方便,這一篇我繼續介紹YYText的使...
    刀客傳奇閱讀 2,096評論 0 2
  • 版本記錄 前言 YYText是一個專門處理文字的框架,有了它處理文字變得非常方便,這一篇我繼續介紹YYText的使...
    刀客傳奇閱讀 1,690評論 0 0