浏览代码

add `see more` example

ibireme 9 年之前
父节点
当前提交
3fa39d5b8b
共有 3 个文件被更改,包括 31 次插入0 次删除
  1. 25 0
      Demo/YYTextDemo/YYTextAttachmentExample.m
  2. 3 0
      YYText/YYLabel.m
  3. 3 0
      YYText/YYTextView.m

+ 25 - 0
Demo/YYTextDemo/YYTextAttachmentExample.m

@@ -89,9 +89,11 @@
     _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;
@@ -115,6 +117,29 @@
     [_label addGestureRecognizer:gesture];
 }
 
+- (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);

+ 3 - 0
YYText/YYLabel.m

@@ -454,6 +454,9 @@ static dispatch_queue_t YYLabelGetReleaseQueue() {
         return _innerLayout.textBoundingSize;
     }
     
+    if (!_verticalForm && size.width <= 0) size.width = YYTextContainerMaxSize.width;
+    if (_verticalForm && size.height <= 0) size.height = YYTextContainerMaxSize.height;
+    
     if ((!_verticalForm && size.width == self.bounds.size.width) ||
         (_verticalForm && size.height == self.bounds.size.height)) {
         [self _updateIfNeeded];

+ 3 - 0
YYText/YYTextView.m

@@ -2467,6 +2467,9 @@ typedef NS_ENUM(NSUInteger, YYTextMoveDirection) {
 }
 
 - (CGSize)sizeThatFits:(CGSize)size {
+    if (!_verticalForm && size.width <= 0) size.width = YYTextContainerMaxSize.width;
+    if (_verticalForm && size.height <= 0) size.height = YYTextContainerMaxSize.height;
+    
     if ((!_verticalForm && size.width == self.bounds.size.width) ||
         (_verticalForm && size.height == self.bounds.size.height)) {
         [self _updateIfNeeded];