添加“...阅读更多”到UILabel的尽头 [英] Add "...Read More" to the end of UILabel

查看:91
本文介绍了添加“...阅读更多”到UILabel的尽头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UILabel ,在某些情况下,文本比 UILabel 本身要长,所以我看到了文字为bla bla bla ...我想添加 ...阅读更多按钮文字结束 UILabel ..

I have a UILabel and in some cases the text is longer then the UILabel itself, so I see the text as "bla bla bla..." I want to add a ...Read More button text at the end of the UILabel..

我看过一些帖子,但他们提供的解决方案对我不利,例如:计算将输入 UILabel 的字符数,但使用我使用的字体时,每个字符的宽度都不同。

I've read some posts but they offer solutions that are not good to me, for example: to calculate how many characters will enter the UILabel, but with the font i'm using each character has a different width.

我该如何设法做到这一点?

How can I manage to do that?

提前致谢!

推荐答案

这就是我将 Read More ... 按钮添加到 UITextView UITextField UILabel

So this is what I did to add the Read More... button to the UITextView, UITextField or UILabel:

- (void)addReadMoreStringToUILabel:(UILabel*)label
{
    NSString *readMoreText = @" ...Read More";
    NSInteger lengthForString = label.text.length;
    if (lengthForString >= 30)
    {
        NSInteger lengthForVisibleString = [self fitString:label.text intoLabel:label];
        NSMutableString *mutableString = [[NSMutableString alloc] initWithString:label.text];
        NSString *trimmedString = [mutableString stringByReplacingCharactersInRange:NSMakeRange(lengthForVisibleString, (label.text.length - lengthForVisibleString)) withString:@""];
        NSInteger readMoreLength = readMoreText.length;
        NSString *trimmedForReadMore = [trimmedString stringByReplacingCharactersInRange:NSMakeRange((trimmedString.length - readMoreLength), readMoreLength) withString:@""];
        NSMutableAttributedString *answerAttributed = [[NSMutableAttributedString alloc] initWithString:trimmedForReadMore attributes:@{
                                                                                                                                        NSFontAttributeName : label.font
                                                                                                                                        }];

        NSMutableAttributedString *readMoreAttributed = [[NSMutableAttributedString alloc] initWithString:readMoreText attributes:@{
                                                                                                                                        NSFontAttributeName : Font(TWRegular, 12.),
                                                                                                                                        NSForegroundColorAttributeName : White
                                                                                                                                        }];

        [answerAttributed appendAttributedString:readMoreAttributed];
        label.attributedText = answerAttributed;

        UITagTapGestureRecognizer *readMoreGesture = [[UITagTapGestureRecognizer alloc] initWithTarget:self action:@selector(readMoreDidClickedGesture:)];
        readMoreGesture.tag = 1;
        readMoreGesture.numberOfTapsRequired = 1;
        [label addGestureRecognizer:readMoreGesture];

        label.userInteractionEnabled = YES;
    }
    else {

        NSLog(@"No need for 'Read More'...");

    }
}

可以使用 fitString:intoLabel 可以找到的方法这里

There is a use of fitString:intoLabel method which can be found here.

至于 UITagTapGestureRecognizer 只是一个正常的 UITapGestureRecognizer 具有 NSInteger 属性的子类名为tag。我这样做是因为我想确定哪个 Read More ... 被点击了我的情况我在同一个 UIViewController 。您可以使用普通的 UITapGestureRecognizer

As for the UITagTapGestureRecognizer is just a normal UITapGestureRecognizer subclass with a NSInteger property called tag. I did that because I want to identify which Read More... were clicked in I case I have more than one in the same UIViewController. You can use a normal UITapGestureRecognizer.

享受!

这篇关于添加“...阅读更多”到UILabel的尽头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆