在UITableViewCell中自动调整KILabel

时间:2015-01-28 02:01:11

标签: ios objective-c uitableview uilabel autoresize

我有一个从Github下载的自定义UILabel(称为KILabel)。我得到它的原因是因为我想复制Instagram的活动源(分段控件和带有2组不同信息的UITableViews)。在Instagram的应用程序中,我知道有能力点击每个UITableViewCell中包含的标签内的人们用户名(这是KILabel发挥作用的地方)。 KILabel识别" @ usernames"," #hashtags"和URL,使它们几乎像UIButton一样可以点播。

我之前没有使用KILabel并使用常规的UILabel,它能够在iOS 8和iOS 7中扩展。现在,当我使用KILabel时,它会自动扩展(动态更改其高度以适应文本)在iOS 7上, NOT 开始展开。当我滚动我的UITableViewController时,就是标签展开的时候。它从正常高度开始。

截至目前,这就是Custom Cell的样子

#import <UIKit/UIKit.h>
#import "KILabel.h"

@interface NewsCell : UITableViewCell

@property (strong, nonatomic) IBOutlet KILabel *cellLabel;
@property (strong, nonatomic) IBOutlet UIImageView *cellImageView;

@end

我将界面(storyboard)中的标签连接到自定义KILabel类以及ImageView。

接下来,我在UITableViewController中实现了单元格。我用一大段文本测试它,看看单元格和标签是否会扩展到正确的大小

cell = [self.tableView dequeueReusableCellWithIdentifier:cellIdentifier2 forIndexPath:indexPath];

        cell.cellImageView.layer.cornerRadius = 6.0f;
        cell.cellImageView.clipsToBounds = YES;
        cell.cellImageView.layer.borderWidth = 1.0f;
        cell.cellImageView.layer.borderColor = [[UIColor blackColor] CGColor];


        UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] init];
        longPress.numberOfTapsRequired = 1;
        [cell.cellLabel addGestureRecognizer:longPress];
        //Create the cell label going into the cell
        cell.cellLabel.linkTapHandler = ^(KILinkType linkType, NSString *string, NSRange range) {
            NSString *mString = [string stringByReplacingOccurrencesOfString:@"@" withString:@""];
            if (linkType == KILinkTypeURL) {
                // Open URLs
                //[self attemptOpenURL:[NSURL URLWithString:string]];
            } else if (linkType == KILinkTypeUserHandle) {
                if (longPress.state == UIGestureRecognizerStatePossible) {
                    [self tapOnUsername:longPress username:mString];
                }

            } else {
                // Put up an alert with a message if it's not an URL
                NSString *linkTypeString = @"Username";
                if (linkType == KILinkTypeHashtag)
                {
                    linkTypeString = @"Hashtag";
                }

                NSString *message = [NSString stringWithFormat:@"You tapped %@ which is a %@", string, linkTypeString];
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Hello"
                                                                message:message
                                                               delegate:nil
                                                      cancelButtonTitle:@"Dismiss"
                                                      otherButtonTitles:nil];
                [alert show];
            }

            };

        PFObject *eachNews = [self.followingNews objectAtIndex:indexPath.row];
        PFUser *notifier = [eachNews objectForKey:@"Notifier"];
        PFUser *notified = [eachNews objectForKey:@"Notified"];
        [notifier fetchIfNeeded];
        [notified fetchIfNeeded];

        NSString *notifierString = [[NSString alloc] init];
        NSString *notifiedString = [[NSString alloc] init];
        NSString *grammer = [[NSString alloc] init];

        NSDate *timeStamp = eachNews.createdAt;
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"MMM d, yyyy h:mm a"];

        NSString *timeString = [timeStamp formattedAsTimeAgo];

        if ([notifier.username isEqualToString:_loggedInUser.username]) {
            notifierString = @"You";
            grammer = @"are";
        } else {
            notifierString = [NSString stringWithFormat:@"@%@", notifier.username];
            grammer = @"is";
        }

        if ([notified.username isEqualToString: _loggedInUser.username]) {
            notifiedString = @"you";
        } else {
            notifiedString = [NSString stringWithFormat:@"@%@", notified.username];
        }


        if (notifier[@"profileImage"] == nil) {
            UIImage *hermet = [UIImage imageNamed:@"user"];
            [cell.cellImageView setImage:hermet];
        } else {
            PFFile *imageFile = notifier[@"profileImage"];
            [cell.cellImageView setImage:[UIImage imageWithData:[imageFile getData]]];
        }


        NSMutableString *newsText = [[NSMutableString alloc] init];

        if ([eachNews[@"Type"] isEqualToString:@"Follow"]) {
            [newsText appendString:[NSString stringWithFormat:@"%@ aaaaaaaaaaaaaaasdasafsfsdgsdgsdfgsdfsodjnfsaioefgnarpuoigbweuifbsdpugbdsfiougbsdiugosbdgiusobfasdioFPBSADUBVSIUDVBSAIUDBSDIUVBSDUVIBDSFIUVBSDIUVSBAVIPUBDSIUVaaaaaaaaaaaaaaasdasafsfsdgsdgsdfgsdfsodjnfsaioefgnarpuoigbweuifbsdpugbdsfiougbsdiugosbdgiusobfasdioFPBSADUBVSIUDVBSAIUDBSDIUVBSDUVIBDSFIUVBSDIUVSBAVIPUBDSIUVaaaaaaaaaaaaaaasdasafsfsdgsdgsdfgsdfsodjnfsaioefgnarpuoigbweuifbsdpugbdsfiougbsdiugosbdgiusobfasdioFPBSADUBVSIUDVBSAIUDBSDIUVBSDUVIBDSFIUVBSDIUVSBAVIPUBDSIUVaaaaaaaaaaaaaaasdasafsfsdgsdgsdfgsdfsodjnfsaioefgnarpuoigbweuifbsdpugbdsfiougbsdiugosbdgiusobfasdioFPBSADUBVSIUDVBSAIUDBSDIUVBSDUVIBDSFIUVBSDIUVSBAVIPUBDSIUVaaaaaaaaaaaaaaasdasafsfsdgsdgsdfgsdfsodjnfsaioefgnarpuoigbweuifbsdpugbdsfiougbsdiugosbdgiusobfasdioFPBSADUBVSIUDVBSAIUDBSDIUVBSDUVIBDSFIUVBSDIUVSBAVIPUBDSIUVaaaaaaaaaaaaaaasdasafsfsdgsdgsdfgsdfsodjnfsaioefgnarpuoigbweuifbsdpugbdsfiougbsdiugosbdgiusobfasdioFPBSADUBVSIUDVBSAIUDBSDIUVBSDUVIBDSFIUVBSDIUVSBAVIPUBDSIUVaaaaaaaaaaaaaaasdasafsfsdgsdgsdfgsdfsodjnfsaioefgnarpuoigbweuifbsdpugbdsfiougbsdiugosbdgiusobfasdioFPBSADUBVSIUDVBSAIUDBSDIUVBSDUVIBDSFIUVBSDIUVSBAVIPUBDSIUVaaaaaaaaaaaaaaasdasafsfsdgsdgsdfgsdfsodjnfsaioefgnarpuoigbweuifbsdpugbdsfiougbsdiugosbdgiusobfasdioFPBSADUBVSIUDVBSAIUDBSDIUVBSDUVIBDSFIUVBSDIUVSBAVIPUBDSIUVaaaaaaaaaaaaaaasdasafsfsdgsdgsdfgsdfsodjnfsaioefgnarpuoigbweuifbsdpugbdsfiougbsdiugosbdgiusobfasdioFPBSADUBVSIUDVBSAIUDBSDIUVBSDUVIBDSFIUVBSDIUVSBAVIPUBDSIUVaaaaaaaaaaaaaaasdasafsfsdgsdgsdfgsdfsodjnfsaioefgnarpuoigbweuifbsdpugbdsfiougbsdiugosbdgiusobfasdioFPBSADUBVSIUDVBSAIUDBSDIUVBSDUVIBDSFIUVBSDIUVSBAVIPUBDSIUVaaaaaaaaaaaaaaasdasafsfsdgsdgsdfgsdfsodjnfsaioefgnarpuoigbweuifbsdpugbdsfiougbsdiugosbdgiusobfasdioFPBSADUBVSIUDVBSAIUDBSDIUVBSDUVIBDSFIUVBSDIUVSBAVIPUBDSIUVaaaaaaaaaaaaaaasdasafsfsdgsdgsdfgsdfsodjnfsaioefgnarpuoigbweuifbsdpugbdsfiougbsdiugosbdgiusobfasdioFPBSADUBVSIUDVBSAIUDBSDIUVBSDUVIBDSFIUVBSDIUVSBAVIPUBDSIUVaaaaaaaaaaaaaaasdasafsfsdgsdgsdfgsdfsodjnfsaioefgnarpuoigbweuifbsdpugbdsfiougbsdiugosbdgiusobfasdioFPBSADUBVSIUDVBSAIUDBSDIUVBSDUVIBDSFIUVBSDIUVSBAVIPUBDSIUVaaaaaaaaaaaaaaasdasafsfsdgsdgsdfgsdfsodjnfsaioefgnarpuoigbweuifbsdpugbdsfiougbsdiugosbdgiusobfasdioFPBSADUBVSIUDVBSAIUDBSDIUVBSDUVIBDSFIUVBSDIUVSBAVIPUBDSIUV%@ %@.\n", notifierString, eachNews[@"Messages"], notifiedString]];
        } else if ([eachNews[@"Type"] isEqualToString:@"New Founder"]) {
            [newsText appendString:[NSString stringWithFormat:@"%@ %@ %@.\n", notifierString, grammer, eachNews[@"Messages"]]];
        } else if ([eachNews[@"Type"] isEqualToString:@"Club Create"]) {
            if([notified.username isEqualToString:_loggedInUser.username]) {
                notifiedString = @"You";
            } else {
                notifiedString = notified.username;
            }
            if (notified[@"profileImage"] == nil) {
                UIImage *hermet = [UIImage imageNamed:@"user"];
                [cell.cellImageView setImage:hermet];
            } else {
                PFFile *imageFile = notified[@"profileImage"];
                [cell.cellImageView setImage:[UIImage imageWithData:[imageFile getData]]];
            }
            [newsText appendString:[NSString stringWithFormat:@"%@ %@\n", notifiedString, eachNews[@"Messages"]]];
        }


        [newsText appendString:timeString];
        NSArray *appendedString = [newsText componentsSeparatedByString:@"\n"];
        NSRange dateRange = [newsText rangeOfString:appendedString[1]];
        NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:newsText];

        [attrString beginEditing];
        [attrString addAttribute: NSForegroundColorAttributeName
                           value:[UIColor lightGrayColor]
                           range:dateRange];

        [attrString endEditing];

        cell.cellLabel.attributedText = attrString;

        CGSize sizeForLabel = CGSizeMake(cell.cellLabel.frame.size.width, 0);
        CGRect labelRect = CGRectMake(cell.cellLabel.frame.origin.x, cell.cellLabel.frame.origin.y, sizeForLabel.width, CGFLOAT_MAX);

        // Use a dummy label and its textRectForBounds method to calculate the height
        // of a real label.
        KILabel *measureLabel = [[KILabel alloc] initWithFrame:CGRectMake(cell.cellLabel.frame.origin.x, cell.cellLabel.frame.origin.y, labelRect.size.width, 0)];
        measureLabel.numberOfLines = 0;

        measureLabel.attributedText = attrString;
        labelRect = [measureLabel textRectForBounds:labelRect limitedToNumberOfLines:0];

        cell.cellLabel.frame = labelRect; 

UITableViewCell的高度非常好,可以完美展开。标签是唯一的问题。它在iOS 8上根本没有扩展,只在后扩展 我在iOS 7上的UITableViewController中滚动。

我会标记KILabel,但是我不能,如果有人使用了像KILabel这样的其他UILabel,并且能够让它们自动扩展,请转介给我们。

GitHub上的KILabel:https://github.com/Krelborn/KILabel

另外,我已经尝试过GitHub上发布的创作者Gist,这将让我的单元格完美展开。

0 个答案:

没有答案