添加“...阅读更多"到 UILabel 的结尾 [英] Add "...Read More" to the end of 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
子类,带有一个名为 tag 的 NSInteger
属性.我这样做是因为我想确定哪个 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屋!