代码改变世界

iOS 可延展视图(点击前显示部分文字,点击后显示全部)

2016-01-18 12:51  甘雨路  阅读(553)  评论(0编辑  收藏  举报

                              

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;


@end
#import "AppDelegate.h"
#import "RootViewController.h"
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    
    self.window.rootViewController = [[RootViewController alloc] init];
    
    [self.window makeKeyAndVisible];
    return YES;
}


@end
#import <UIKit/UIKit.h>

@interface RootViewController : UIViewController

@end
#import "RootViewController.h"
#import "ExpandableView.h"
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
@interface RootViewController ()<refreshDelagate>
{
    UILabel *label;
    ExpandableView *expandableView;
}
@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor lightGrayColor];
    expandableView = [[ExpandableView alloc] initWithFrame:CGRectMake(0, 100, SCREEN_WIDTH, 0)];
    expandableView.introdution.text = @"至尊宝被月光宝盒带回到五百年前,遇见紫霞仙子被对方打上烙印成为对方的人,并发觉自己已变成孙悟空。紫霞与青霞本是如来佛祖座前日月神灯的灯芯(白天是紫霞,晚上是青霞),二人虽然同一肉身却仇恨颇深,因此紫霞立下誓言,谁能拔出她手中的紫青宝剑,谁就是她的意中人。紫青宝剑被至尊宝于不经意间拔出,紫霞决定以身相许,却遭一心记挂白晶晶的至尊宝拒绝。后牛魔王救下迷失在沙漠中的紫霞,并逼紫霞与他成婚,关键时刻,至尊宝现身。";
    expandableView.delegate = self;
    [self.view addSubview:expandableView];
    
    label = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(expandableView.frame), SCREEN_WIDTH, 200)];
    label.backgroundColor = [UIColor redColor];
//    label.alpha = 0.0;
    [self.view addSubview:label];
}
/**
 *  改变视图的尺寸
 */
- (void)refreshSubView{
    label.frame = CGRectMake(0, CGRectGetMaxY(expandableView.frame), SCREEN_WIDTH, 200);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
#import <UIKit/UIKit.h>

@protocol refreshDelagate <NSObject>

- (void)refreshSubView;

@end

@interface ExpandableView : UIView

@property(nonatomic,weak) id<refreshDelagate> delegate;
@property(nonatomic, strong) UILabel *introdution;

@end
#import "ExpandableView.h"
#define self_height 70
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define fontSize 12
#define top_gap 7
#define left_gap 20
#define label_buttom_gap 5
#define direction_width 20
#define direction_height 10
#define direction_buttom_gap 5
#define color [UIColor whiteColor]
#define backgroundView_height (label_buttom_gap+direction_height+direction_buttom_gap)
@interface ExpandableView ()

@property(nonatomic, strong)UIButton *button;
@property(nonatomic, strong)UIImageView *direction;
@property(nonatomic, strong)UIView *backgroundView;

@end

@implementation ExpandableView


- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = color;
        self.userInteractionEnabled = YES;
        self.clipsToBounds = YES;
        self.frame = CGRectMake(frame.origin.x, frame.origin.y, SCREEN_WIDTH, self_height);
        
        self.introdution = [[UILabel alloc] init];
        self.introdution.backgroundColor = [UIColor clearColor];
        self.introdution.textColor = [UIColor grayColor];
        self.introdution.numberOfLines = 0;
        self.introdution.font = [UIFont systemFontOfSize:fontSize];
        [self addSubview:self.introdution];
        
        self.backgroundView = [[UIView alloc] init];
        self.backgroundView.backgroundColor = color;
        [self addSubview:self.backgroundView];
        
        self.direction = [[UIImageView alloc] init];
        self.direction.image = [UIImage imageNamed:@"向下图标"];
        [self addSubview:self.direction];
        
        self.button = [UIButton buttonWithType:UIButtonTypeCustom];
        self.button.backgroundColor = [UIColor clearColor];
        [self.button addTarget:self action:@selector(isExpandView:) forControlEvents:UIControlEventTouchUpInside];
        self.button.selected = NO;
        
        [self addSubview:self.button];
        
    }
    return self;
}

- (void)layoutSubviews{
    [super layoutSubviews]; 
    CGSize size = [self computeHeightByText];
    self.introdution.frame = CGRectMake(left_gap, top_gap, SCREEN_WIDTH-left_gap*2, size.height/*self.frame.size.height - top_gap-label_buttom_gap-direction_height*/);
    
    self.backgroundView.frame = CGRectMake(0, self.bounds.size.height -backgroundView_height, SCREEN_WIDTH, backgroundView_height);
    
    self.direction.frame = CGRectMake((SCREEN_WIDTH - direction_width)/2.0, self.bounds.size.height - direction_height-direction_buttom_gap, direction_width, direction_height);
    
    self.button.frame = self.bounds;
    
}
/**
 *  根据文字计算高度
 */
- (CGSize)computeHeightByText{
    UIFont *font = [UIFont systemFontOfSize:fontSize];
    CGSize contraint = CGSizeMake(SCREEN_WIDTH-left_gap*2, 20000);
    CGSize size = [self.introdution.text sizeWithFont:font constrainedToSize:contraint lineBreakMode:NSLineBreakByWordWrapping];
    return size;
}
/**
 *  重新布局UI
 *
 *  @param size 根据文字计算出的尺寸
 */
- (void)rebuildViewWithSize:(CGSize)size{
    CGRect frame = self.frame;
    float height = size.height+top_gap+label_buttom_gap+direction_height+direction_buttom_gap;
    self.frame = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, height);
    self.introdution.frame = CGRectMake(left_gap, 0, SCREEN_WIDTH-left_gap*2, 129);
    self.button.frame = self.bounds;
}
/**
 *  按钮触发的事件
 */
- (void)isExpandView:(UIButton*)sender{
    self.button.selected = !self.button.selected;
    if (self.button.selected) {
        self.direction.image = [UIImage imageNamed:@"向下图标_down"];
        CGSize size = [self computeHeightByText];
        [self rebuildViewWithSize:size];
    }else{
        self.direction.image = [UIImage imageNamed:@"向下图标"];
        CGRect frame = self.frame;
        self.frame = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, self_height);
    }
    [self.delegate refreshSubView];
}

@end