YYText使用篇(七)

版本記錄

版本號 時間
V1.0 2017.06.08

前言

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

一拍埠、YYText示例

下面我們看另外一個例子,先看代碼土居。

#import "YYTextAttachmentExample.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"
#import "YYGestureRecognizer.h"

@interface YYTextAttachmentExample () <UIGestureRecognizerDelegate>

@property (nonatomic, strong) YYLabel *label;

@end

@implementation YYTextAttachmentExample

#pragma mark - Override Base Function

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];
    [YYTextExampleHelper addDebugOptionToViewController:self];

    NSMutableAttributedString *text = [NSMutableAttributedString new];
    UIFont *font = [UIFont systemFontOfSize:16];
    
    //第一行文字
    {
        NSString *title = @"This is UIImage attachment:";
        [text appendAttributedString:[[NSAttributedString alloc] initWithString:title attributes:nil]];
        
        UIImage *image = [UIImage imageNamed:@"dribbble64_imageio"];
        image = [UIImage imageWithCGImage:image.CGImage scale:2 orientation:UIImageOrientationUp];
        
        NSMutableAttributedString *attachText = [NSMutableAttributedString yy_attachmentStringWithContent:image contentMode:UIViewContentModeCenter attachmentSize:image.size alignToFont:font alignment:YYTextVerticalAlignmentCenter];
        [text appendAttributedString:attachText];
        [text appendAttributedString:[[NSAttributedString alloc] initWithString:@"\n" attributes:nil]];
    }
    
    //第二行文字
    {
        NSString *title = @"This is UIView attachment: ";
        [text appendAttributedString:[[NSAttributedString alloc] initWithString:title attributes:nil]];
        
        UISwitch *switcher = [UISwitch new];
        [switcher sizeToFit];
        
        NSMutableAttributedString *attachText = [NSMutableAttributedString yy_attachmentStringWithContent:switcher contentMode:UIViewContentModeCenter attachmentSize:switcher.size alignToFont:font alignment:YYTextVerticalAlignmentCenter];
        [text appendAttributedString:attachText];
        [text appendAttributedString:[[NSAttributedString alloc] initWithString:@"\n" attributes:nil]];
    }
    
    //第三行文字
    {
        NSString *title = @"This is Animated Image attachment:";
        [text appendAttributedString:[[NSAttributedString alloc] initWithString:title attributes:nil]];
        
        NSArray *names = @[@"001", @"022", @"019",@"056",@"085"];
        for (NSString *name in names) {
            NSString *path = [[NSBundle mainBundle] pathForScaledResource:name ofType:@"gif" inDirectory:@"EmoticonQQ.bundle"];
            NSData *data = [NSData dataWithContentsOfFile:path];
            YYImage *image = [YYImage imageWithData:data scale:2];
            image.preloadAllAnimatedImageFrames = YES;
            YYAnimatedImageView *imageView = [[YYAnimatedImageView alloc] initWithImage:image];
            
            NSMutableAttributedString *attachText = [NSMutableAttributedString yy_attachmentStringWithContent:imageView contentMode:UIViewContentModeCenter attachmentSize:imageView.size alignToFont:font alignment:YYTextVerticalAlignmentCenter];
            [text appendAttributedString:attachText];
        }
        
        YYImage *image = [YYImage imageNamed:@"pia"];
        image.preloadAllAnimatedImageFrames = YES;
        YYAnimatedImageView *imageView = [[YYAnimatedImageView alloc] initWithImage:image];
        imageView.autoPlayAnimatedImage = NO;
        [imageView startAnimating];
        
        NSMutableAttributedString *attachText = [NSMutableAttributedString yy_attachmentStringWithContent:imageView contentMode:UIViewContentModeCenter attachmentSize:imageView.size alignToFont:font alignment:YYTextVerticalAlignmentBottom];
        [text appendAttributedString:attachText];
        
        [text appendAttributedString:[[NSAttributedString alloc] initWithString:@"\n" attributes:nil]];
    }
    
    text.yy_font = font;
    
    _label = [YYLabel new];
    _label.userInteractionEnabled = YES;
    _label.numberOfLines = 0;
    _label.textVerticalAlignment = YYTextVerticalAlignmentTop;
    _label.size = CGSizeMake(260, 260);
    _label.center = CGPointMake(self.view.width / 2, self.view.height / 2);
    _label.attributedText = text;
    [self addSeeMoreButton];
    [self.view addSubview:_label];
    
    _label.layer.borderWidth = 0.5;
    _label.layer.borderColor = [UIColor colorWithRed:0.000 green:0.463 blue:1.000 alpha:1.000].CGColor;
    
    __weak typeof(_label) wlabel = _label;
    UIView *dot = [self newDotView];
    dot.center = CGPointMake(_label.width, _label.height);
    dot.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin;
    [_label addSubview:dot];
    
    YYGestureRecognizer *gesture = [YYGestureRecognizer new];
    gesture.action = ^(YYGestureRecognizer *gesture, YYGestureRecognizerState state) {
        if (state != YYGestureRecognizerStateMoved) return;
        CGFloat width = gesture.currentPoint.x;
        CGFloat height = gesture.currentPoint.y;
        wlabel.width = width < 30 ? 30 : width;
        wlabel.height = height < 30 ? 30 : height;
    };
    gesture.delegate = self;
    [_label addGestureRecognizer:gesture];
}

#pragma mark - Object Private Function

- (void)addSeeMoreButton
{
    __weak typeof(self) _self = self;
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:@"...more"];
    
    YYTextHighlight *hi = [YYTextHighlight new];
    [hi setColor:[UIColor colorWithRed:0.578 green:0.790 blue:1.000 alpha:1.000]];
    hi.tapAction = ^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect) {
        YYLabel *label = _self.label;
        [label sizeToFit];
    };
    
    [text yy_setColor:[UIColor colorWithRed:0.000 green:0.449 blue:1.000 alpha:1.000] range:[text.string rangeOfString:@"more"]];
    [text yy_setTextHighlight:hi range:[text.string rangeOfString:@"more"]];
    text.yy_font = _label.font;
    
    YYLabel *seeMore = [YYLabel new];
    seeMore.attributedText = text;
    [seeMore sizeToFit];
    
    NSAttributedString *truncationToken = [NSAttributedString yy_attachmentStringWithContent:seeMore contentMode:UIViewContentModeCenter attachmentSize:seeMore.size alignToFont:text.yy_font alignment:YYTextVerticalAlignmentCenter];
    _label.truncationToken = truncationToken;
}

- (UIView *)newDotView
{
    UIView *view = [UIView new];
    view.size = CGSizeMake(50, 50);
    
    UIView *dot = [UIView new];
    dot.size = CGSizeMake(10, 10);
    dot.backgroundColor = [UIColor colorWithRed:0.000 green:0.463 blue:1.000 alpha:1.000];
    dot.clipsToBounds = YES;
    dot.layer.cornerRadius = dot.height / 2;
    dot.center = CGPointMake(view.width / 2, view.height / 2);
    [view addSubview:dot];
    
    return view;
}

#pragma mark - UIGestureRecognizerDelegate

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    CGPoint p = [gestureRecognizer locationInView:_label];
    if (p.x < _label.width - 20)
        return NO;
    if (p.y < _label.height - 20)
        return NO;
    return YES;
}

@end

二、實現(xiàn)效果

下面看一下這個例子的實現(xiàn)效果嬉探。

實現(xiàn)效果gif
實現(xiàn)效果

后記

??個人覺著這些例子都很有意思擦耀,感興趣的大家可以自己寫寫,其實還是很好玩的涩堤,希望對大家有所幫助眷蜓。

家里
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市胎围,隨后出現(xiàn)的幾起案子吁系,更是在濱河造成了極大的恐慌德召,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,372評論 6 498
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件汽纤,死亡現(xiàn)場離奇詭異上岗,居然都是意外死亡,警方通過查閱死者的電腦和手機蕴坪,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,368評論 3 392
  • 文/潘曉璐 我一進店門肴掷,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人背传,你說我怎么就攤上這事呆瞻。” “怎么了径玖?”我有些...
    開封第一講書人閱讀 162,415評論 0 353
  • 文/不壞的土叔 我叫張陵痴脾,是天一觀的道長。 經(jīng)常有香客問我梳星,道長赞赖,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,157評論 1 292
  • 正文 為了忘掉前任丰泊,我火速辦了婚禮薯定,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘瞳购。我一直安慰自己话侄,他們只是感情好,可當我...
    茶點故事閱讀 67,171評論 6 388
  • 文/花漫 我一把揭開白布学赛。 她就那樣靜靜地躺著年堆,像睡著了一般。 火紅的嫁衣襯著肌膚如雪盏浇。 梳的紋絲不亂的頭發(fā)上变丧,一...
    開封第一講書人閱讀 51,125評論 1 297
  • 那天,我揣著相機與錄音绢掰,去河邊找鬼痒蓬。 笑死,一個胖子當著我的面吹牛滴劲,可吹牛的內(nèi)容都是我干的攻晒。 我是一名探鬼主播,決...
    沈念sama閱讀 40,028評論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼班挖,長吁一口氣:“原來是場噩夢啊……” “哼鲁捏!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起萧芙,我...
    開封第一講書人閱讀 38,887評論 0 274
  • 序言:老撾萬榮一對情侶失蹤给梅,失蹤者是張志新(化名)和其女友劉穎假丧,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體动羽,經(jīng)...
    沈念sama閱讀 45,310評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡包帚,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,533評論 2 332
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了曹质。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片婴噩。...
    茶點故事閱讀 39,690評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖羽德,靈堂內(nèi)的尸體忽然破棺而出几莽,到底是詐尸還是另有隱情,我是刑警寧澤宅静,帶...
    沈念sama閱讀 35,411評論 5 343
  • 正文 年R本政府宣布章蚣,位于F島的核電站,受9級特大地震影響姨夹,放射性物質(zhì)發(fā)生泄漏纤垂。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,004評論 3 325
  • 文/蒙蒙 一磷账、第九天 我趴在偏房一處隱蔽的房頂上張望峭沦。 院中可真熱鬧,春花似錦逃糟、人聲如沸吼鱼。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽菇肃。三九已至,卻和暖如春取募,著一層夾襖步出監(jiān)牢的瞬間琐谤,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,812評論 1 268
  • 我被黑心中介騙來泰國打工玩敏, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留斗忌,地道東北人。 一個月前我還...
    沈念sama閱讀 47,693評論 2 368
  • 正文 我出身青樓旺聚,卻偏偏與公主長得像飞蹂,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子翻屈,可洞房花燭夜當晚...
    茶點故事閱讀 44,577評論 2 353

推薦閱讀更多精彩內(nèi)容

  • 版本記錄 前言 YYText是一個專門處理文字的框架,有了它處理文字變得非常方便妻坝,這一篇我繼續(xù)介紹YYText的使...
    刀客傳奇閱讀 2,026評論 0 2
  • 版本記錄 前言 YYText是一個專門處理文字的框架伸眶,有了它處理文字變得非常方便惊窖,這一篇我繼續(xù)介紹YYText的使...
    刀客傳奇閱讀 1,819評論 9 4
  • 版本記錄 前言 YYText是一個專門處理文字的框架,有了它處理文字變得非常方便厘贼,這一篇我繼續(xù)介紹YYText的使...
    刀客傳奇閱讀 2,813評論 0 2
  • 版本記錄 前言 YYText是一個專門處理文字的框架界酒,有了它處理文字變得非常方便,這一篇我繼續(xù)介紹YYText的使...
    刀客傳奇閱讀 1,823評論 0 1
  • 版本記錄 前言 YYText是一個專門處理文字的框架嘴秸,有了它處理文字變得非常方便毁欣,這一篇我繼續(xù)介紹YYText的使...
    刀客傳奇閱讀 1,652評論 0 0