在8行之后阅读textview末尾的更多可点击文字

时间:2016-05-23 07:01:20

标签: ios iphone uitableview textview label

我有问题在8行末尾的textview中添加readmore clickable文本。我的textview在自定义单元格中。根据文字的高度变化。 我已将8最大行设置为textcontainer

我使用布局管理器获取8行结束文本并替换为阅读更多文本。它在模拟器中工作得很好。当我使用实际的设备布局管理器进行测试时,内容大小无法正常工作。

我使用下面的函数获取8行文本

int counter=0;

NSRange replacewordRange =NSMakeRange(0, 0);

[txtView_status.layoutManager enumerateLineFragmentsForGlyphRange:NSMakeRange(0, Str.length) usingBlock:^(CGRect rect, CGRect usedRect, NSTextContainer *textContainer, NSRange glyphRange, BOOL *stop) {

            counter++;

            if (counter==kTotalLineShow &&glyphRange.length>30 )

            {
                replacewordRange.location =glyphRange.location+glyphRange.length-kKeyReadMoreText.length;

                replacewordRange.length=kKeyReadMoreText.length;

            }

            NSLog(@"rect %@ - usedRect %@ - glymph Rangle %lu %lu -",NSStringFromCGRect(rect),NSStringFromCGRect(usedRect),(unsigned long)glyphRange.location,(unsigned long)glyphRange.length);

        }];

我想生成以下结果:enter image description here enter image description here

对此有任何建议或帮助。

提前致谢。

1 个答案:

答案 0 :(得分:0)

请参阅此代码并根据单元格/标签的大小进行更改。

NSDictionary *attribs = @{
                              NSForegroundColorAttributeName: BODY_FONT_COLOR,
                              NSFontAttributeName: [UIFont fontWithName:FontHelvetica size:AppFont16]
                              };
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:ACCIDENT_DETECTION_AUTOMATIC_TEXT attributes:attribs];
    [attributedString addAttribute:NSLinkAttributeName
                             value:@"Accident detection"
                             range:[[attributedString string] rangeOfString:@"Read More"]];

NSDictionary *linkAttributes = @{NSForegroundColorAttributeName: LINKS_FONT_COLOR,
                                 NSUnderlineColorAttributeName: [UIColor clearColor],
                                 NSUnderlineStyleAttributeName: @(NSUnderlineStyleNone)};

// assume that textView is a UITextView previously created (either by code or Interface Builder)
self.accidentDetectionText.linkTextAttributes = linkAttributes; // customizes the appearance of links
self.accidentDetectionText.attributedText = attributedString;