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

iOS 可延展视图(点击前显示部分文字,点击后显示全部)的更多相关文章

  1. ios 透过上层视图点击相应下方视图的点击事件

    - (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent *)event{ UIView *hitView = [super hitTest:point ...

  2. MYSQL不能显示中文字,显示错误“ERROR 1366 (HY000): Incorrect string value: '\xE5\xBC\xA0\xE4\xB8\x89'”

    或者建表时带上编码utf8 CREATE TABLE `students`( `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY, `name` VARCHAR( ...

  3. 李洪强iOS开发之-实现点击单行View显示和隐藏Cell

    李洪强iOS开发之-实现点击单行View显示和隐藏Cell 实现的效果:  .... ....

  4. iOS开发系列--视图切换

    概述 在iOS开发中视图的切换是很频繁的,独立的视图应用在实际开发过程中并不常见,除非你的应用足够简单.在iOS开发中常用的视图切换有三种,今天我们将一一介绍: UITabBarController ...

  5. IOS开发之视图和视图控制器

    视图(View), 视图控制器(ViewController)是IOS开发UI部分比较重要的东西.在学习视图这一块的东西的时候,感觉和Java Swing中的Panel差不多.在UIKit框架中都有一 ...

  6. Xamarin.IOS之多视图

    欢迎大家加入以下开源社区 Xamarin-Cn:https://github.com/Xamarin-Cn Mvvmcross-Cn:https://github.com/Mvvmcross-Cn  ...

  7. iOS学习之视图控制器

    一.自定义视图(label-textField组合视图)      1.自定义视图:系统标准UI之外,自己组合出的新的视图.      2.优点:iOS提供了很多UI组件,借助它们我们可以实现不同的功 ...

  8. Swift - iOS中各种视图控制器(View Controller)的介绍

    在iOS中,不同的视图控制器负责不同的功能,采用不同的风格向用户呈现信息.下面对各个视图控制器做个总结: 1,标准视图控制器 - View Controller 这个控制器只是用来呈现内容.通常会用来 ...

  9. iOS:UITableView表格视图控件

    UITableView:表格视图控件,继承滚动视图控件UIScrollView,(类似于UIPickerView选择器,它主要通过设置数据源代理和行为代理实现协议来设置单元格)    对表格的操作主要 ...

随机推荐

  1. UVALive 2635 匈牙利算法

    题意 给出k块地 规模n*m 需要在每块地中找至多一块h*w的地 这些地中如果包含字母 只能包含一种字母 如果一块地中选地使用了A 其余的地就不能使用A 但是全0可以重复 问 最后能最多选出来多少块地 ...

  2. FZU 1018 枚举dp

    题意 给出一个数字组成的立方体 在其中选取一个体 使这个体中的数字之和最小 不可以不选 fzu的题目分类动态规划里面不是按难度排得 是按照题号..记得以前做题碰到过算 矩阵里面求子矩阵的最大和的 不会 ...

  3. php开源项目

    论坛社区:Discuz.PHPWind.ThinkSAAS.phpBB CMS内容管理:DedeCMS.PHPCMS.帝国CMS.齐博CMS.Drupal 企业建站:CmsEasy.KingCMS.P ...

  4. 9.PHP内核探索:通过mod_php5支持PHP

    Apache对PHP的支持是通过Apache的模块mod_php5来支持的.如果希望Apache支持PHP的话,在./configure步 骤需要指定--with-apxs2=/usr/local/a ...

  5. jQuery之换肤与cookie插件

    有时候一个网页可以有多个皮肤进行选择,也就是不同的背景,或是一整套新的css,能使整个页面变成另一种风格. 这个功能可以用jQuery来实现.外加cookie插件.有了cookie,就可以长时间的保存 ...

  6. P1010 幂次方

    这么难得题,居然普及-?做了好久 #include <bits/stdc++.h> using namespace std; int fact[21]; void solve(int n) ...

  7. 原生js实现跑马灯抽奖效果

    目前好多的微信活动都有一些抽奖活动,其中就有跑马灯. <!DOCTYPE html> <html> <head> <title>跑马灯效果</ti ...

  8. 【php学习】时间函数

    手工画了一张图,来大体概括php中对于时间的处理函数 首先时间戳是这样“1441202665”的一串数字,虽然人看起来麻烦,但是计算机却很容易识别这样的时间表示形式. 所以给计算机看的时间是时间戳,给 ...

  9. QWidget 键盘事件 焦点(QApplication源码)

    在Qt中,键盘事件和QWidget的focus密不可分:一般来说,一个拥有焦点(focus)的QWidget或者grabKeyboard()的QWidget才可以接受键盘事件. 键盘事件派发给谁? 如 ...

  10. KVO机制浅析和实例演示

    什么是KVO? KVO是Key-Value-Observing的缩写,通过KVO这种机制对象可以通过它得到其他对象的某个属性的变更通知.这种机制在MVC模式下显得更为重要,KVO可以让视图对象经过控制 ...