In meiner App habe ich verwendet, um Tabellen anzuzeigen. In meiner Liste habe ich einige Zellenlabel Texte größer, so dass ich View more
oder read more
Knopf oder Etikett nach Etikett Text hinzufügen möchten. Bitte schlage mir vor, wie ich das machen kann.UILabel oder UIButton etwas wie "mehr anzeigen", wenn UILabel Textlänge größer
Ich habe damit getan. Es ist successuflly, aber es funktioniert nicht read more
Geste
- (void)addReadMoreStringToUILabel:(UILabel*)label
{
NSString *readMoreText = @"...Read More";
NSInteger lengthForString = label.text.length;
if (lengthForString >= 50)
{
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 :[UIFont fontWithName:@"Roboto-Light" size:13.0] ,NSForegroundColorAttributeName :[UIColor blueColor]}];
[answerAttributed appendAttributedString:readMoreAttributed];
label.attributedText = answerAttributed;
label.userInteractionEnabled = YES;
UITapGestureRecognizer *readMoreGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(readMoreDidClickedGesture:)];
readMoreGesture.numberOfTapsRequired = 1;
[label addGestureRecognizer:readMoreGesture];
}
else {
NSLog(@"No need for 'Read More'...");
}
}
- (NSUInteger)fitString:(NSString *)string intoLabel:(UILabel *)label
{
UIFont *font = label.font;
NSLineBreakMode mode = label.lineBreakMode;
CGFloat labelWidth = label.frame.size.width;
CGFloat labelHeight = label.frame.size.height;
CGSize sizeConstraint = CGSizeMake(labelWidth, CGFLOAT_MAX);
NSDictionary *attributes = @{ NSFontAttributeName : font };
NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:string attributes:attributes];
CGRect boundingRect = [attributedText boundingRectWithSize:sizeConstraint options:NSStringDrawingUsesLineFragmentOrigin context:nil];
{
if (boundingRect.size.height > labelHeight)
{
NSUInteger index = 0;
NSUInteger prev;
NSCharacterSet *characterSet = [NSCharacterSet whitespaceAndNewlineCharacterSet];
do
{
prev = index;
if (mode == NSLineBreakByCharWrapping)
index++;
else
index = [string rangeOfCharacterFromSet:characterSet options:0 range:NSMakeRange(index + 1, [string length] - index - 1)].location;
}
while (index != NSNotFound && index < [string length] && [[string substringToIndex:index] boundingRectWithSize:sizeConstraint options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size.height <= labelHeight);
return prev;
}
}
/*if (SYSTEM_VERSION_GREATER_THAN(iOS7))
{
}
else
{
if ([string sizeWithFont:font constrainedToSize:sizeConstraint lineBreakMode:mode].height > labelHeight)
{
NSUInteger index = 0;
NSUInteger prev;
NSCharacterSet *characterSet = [NSCharacterSet whitespaceAndNewlineCharacterSet];
do
{
prev = index;
if (mode == NSLineBreakByCharWrapping)
index++;
else
index = [string rangeOfCharacterFromSet:characterSet options:0 range:NSMakeRange(index + 1, [string length] - index - 1)].location;
}
while (index != NSNotFound && index < [string length] && [[string substringToIndex:index] sizeWithFont:font constrainedToSize:sizeConstraint lineBreakMode:mode].height <= labelHeight);
return prev;
}
}*/
return [string length];
}
- (void)readMoreDidClickedGesture:(UITapGestureRecognizer*)sender
{
UIView *view = sender.view;
NSLog(@"%ld", (long)view.tag); //By tag, you can find out where you had tapped.
}
Ich weiß nicht, was hier passiert. Wenn irgendwas falsch in diesem Code so bitte helfen Sie mir
dies sehen, wenn es Ihnen hilft http://stackoverflow.com/questions/32309247/add-read-more-to-the-end-of-uilabel –
Ja mein Code wurde mit dieser Referenz –
Wurde readMoreDidClickedGesture: Methode aufgerufen? –