ios – Theos, how to add UILabel to UICollectionView

0
134


I’m trying to create a new tweak with Theos by patching Instagram to add a new label to a UICollectionView. When a user clicks on the “Followers” list on their profile a new label will be added to the cells and show if someone is following you.

With the code I have now I get two errors:

proper ‘viewController’ not found on object of type ‘IGFollowListCollectionCell *’ IGFollowListCollection *vc = (IGFollowListCollection *)self.viewController;

no visible @interface for ‘IGFollowListCollectionCell’ declares the selector ‘_rootView’ IGWindow *rootView = (IGWindow *)[self _rootView];

Tweak.x

#import <UIKit/UIKit.h>
#import <Tweak.h>




// Hooking a class method
%hook IGFollowListCollectionCell
    %property (nonatomic, retain) UILabel *isFollowingYouLabel;
    %property (nonatomic, retain) UIView *isFollowingYouBadge;
    %property (nonatomic, retain) IGUser *user;

     - (id)initWithFrame:(CGRect)arg1 {
      self = %orig;

      dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ 
      // ensure user's relationship is fetched
        @try {
            IGFollowListViewController *vc = (IGFollowListViewController *)self.viewController;
        } @catch (NSException *e) { }
      });

                IGWindow *rootView = (IGWindow *)[self _rootView];
          IGUser *currentUser = rootView.userSession.user;
            [self addIsFollowingYouBadgeView];
            return self;
    }



    %new
    - (void)addIsFollowingYouBadgeView {

      self.isFollowingYouBadge = [[UIView alloc] init];
      self.isFollowingYouBadge.frame = CGRectMake(155, 75, 70, 16);
      self.isFollowingYouBadge.alpha = 1;
      self.isFollowingYouBadge.layer.cornerRadius = 4;

      [self addSubview:self.isFollowingYouBadge];

      UIFont *customFont = [UIFont fontWithName:@"Arial-BoldMT" size:10]; //custom font
      self.isFollowingYouLabel = [[UILabel alloc] initWithFrame:CGRectMake(2.0, 0.0, 66.0, 16.0)];
      self.isFollowingYouLabel.translatesAutoresizingMaskIntoConstraints = false;
      self.isFollowingYouLabel.text = @"Follows you";
      self.isFollowingYouLabel.font = customFont;
      self.isFollowingYouLabel.adjustsFontSizeToFitWidth = true;
      self.isFollowingYouLabel.textAlignment = NSTextAlignmentCenter;
      [self.isFollowingYouBadge addSubview:self.isFollowingYouLabel];

    }

%end

Tweak.h

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "lib/InstaZoomImageViewController.h"

@interface IGUser : NSObject
@property BOOL followsCurrentUser;
@end

@interface IGProfileBioModel
@property(readonly, copy, nonatomic) IGUser *user;
@end

@interface IGFollowListViewController : UIViewController {
  IGProfileBioModel *_bioModel;
}
@end

@interface IGFollowListCollectionCell : UICollectionViewCell
@property(nonatomic, retain) UIView *isFollowingYouBadge; // new property
@property(nonatomic, retain) UILabel *isFollowingYouLabel; // new property
- (void)addIsFollowingYouBadgeView; // new
@end

@interface IGUserSession : NSObject
@property(readonly, nonatomic) IGUser *user;
@end

@interface IGWindow : UIWindow
@property(nonatomic) __weak IGUserSession *userSession;
@end

The original source code has the functionality that already adds a label to a user’s profile: https://github.com/haoict/instagram-no-ads