04-UIKit(UINavigationController、NSAttributeString、UIImageView)
目录:
一、UINavigationController导航视图控制器
一、UINavigationController导航视图控制器
1 定义:导航视图控制器是控制另外控制器的控制器
2 作用:导航,管理多个视图控制器的跳转,比如我们自己控制视图控制器的跳转更清晰
3 怎么用:创建UINavigationController有一个初始化方法initWithRootViewController:被控制的控制器
* 把navigationcontroller设置成window的根视图
* 从navigationcontroller中跳转到另一个视图控制器:pushViewController
* 从navigationcontroller返回上一个视图控制器:popViewController
在storyboard中设置navigation
editor -> embedin -> navigationController
4 在empty中使用代码添加navigation
在AppDelegate.m中
//UINavigationController
MXFistViewController* fistViewController = [[MXFistViewController alloc] initWithNibName:@"MXFistViewController" bundle:nil];
UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:fistViewController];
self.window.rootViewController = navi;
5 内部原理
* navigation根view不能pop
* navigation维护着一个VC栈,先进后出
* navi必须有一个根视图,创建navi时一般会直接指定
* push一个VC时,上一个VC不会被释放
* navi会保存push进去的VC引用计数器
* pop出来的VC会立即被释放
* 不能pop根视图
6 深入理解navi
* .title .leftBarButtonItem .leftBarButtonItems .toolBarItems这些属性设置在具体的某一个被navi包含的VC,不是设置在navi上
* 具体的VC 上的toolbar默认是隐藏的,如果显示需要调用一个方法[self.navigationController setToolbarHidden:NO]
* UIBarButtonItem 不管是Navigation还是toolbar其中的按键都要用UIBarButton
initWithSystemItem:...设置系统内置按钮
initWithTitle:...文字按钮
initWithImage...图片按钮
- (void)viewDidLoad
{
[super viewDidLoad];
//设置navigation标题
self.title = @"好友圈";
//设置navigation左右按钮
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:self action:@selector(writeWeibo:)];
//self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:nil action:nil];
UIBarButtonItem *button1 = [[UIBarButtonItem alloc] initWithTitle:@"one" style:UIBarButtonItemStyleBordered target:nil action:nil];
UIBarButtonItem *button2 = [[UIBarButtonItem alloc] initWithTitle:@"two" style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.rightBarButtonItems = @[button1,button2];
//工具条
UIBarButtonItem* button3 = [[UIBarButtonItem alloc] initWithTitle:@"首页" style:UIBarButtonItemStyleBordered target:nil action:nil];
UIBarButtonItem* button4 = [[UIBarButtonItem alloc] initWithTitle:@"消息" style:UIBarButtonItemStyleBordered target:nil action:nil];
//fixed固定
UIBarButtonItem* fixed = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
fixed.width = 20;//设置固定宽度
//flexible灵活
UIBarButtonItem* flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
//添加工具条中的按钮
self.toolbarItems = @[fixed,button3,flexible,button4,fixed];
// Do any additional setup after loading the view from its nib.
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
//显示工具条
[self.navigationController setToolbarHidden:NO animated:YES];
}
-(void)writeWeibo:(UIBarButtonItem*)button{
MXSecondViewController* second = [[MXSecondViewController alloc] initWithNibName:@"MXSecondViewController" bundle:nil];
//跳转到另一个view
//[self.navigationController pushViewController:second animated:YES];
//跳转到被navigation包含的一个view
UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:second];
[self presentViewController:navi animated:YES completion:nil];
}
二、NSAttributeString属性字符串
1 ios6中开始ios7加强
2 在NSString的基础上,加入了属性的功能,字体,颜色,加粗,描边,等
MXAttributeViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[self setup];
UIBarButtonItem* button = [[UIBarButtonItem alloc] initWithTitle:@"stats" style:UIBarButtonItemStyleBordered target:self action:@selector(statsTap:)];
//设置导航控制器的右边的button
self.navigationItem.rightBarButtonItem = button;
}
-(void)statsTap:(UIBarButtonItem*)button{
MXTextStatsViewController *text = [[MXTextStatsViewController alloc] initWithNibName:@"MXTextStatsViewController" bundle:nil];
text.textToAnglyze = self.body.textStorage;//传值
//
[self.navigationController pushViewController:text animated:YES];
}
-(void)setup{
NSMutableAttributedString *title = [[NSMutableAttributedString alloc] initWithString:@"outline" attributes:@{NSStrokeWidthAttributeName: @-3,NSStrokeColorAttributeName:self.outlineButton.tintColor}];
[self.outlineButton setAttributedTitle:title forState:UIControlStateNormal];
}
//改变textView中被选择字体颜色
- (IBAction)changeSelectedColor:(UIButton *)sender {
//Storage中文是存储 仓库
[self.body.textStorage addAttribute:NSForegroundColorAttributeName value:sender.backgroundColor range:self.body.selectedRange];
}
//加粗textView中被选择字体
- (IBAction)outlineSelect:(UIButton *)sender {
//[self.body.textStorage addAttributes:@{NSStrokeColorAttributeName: [UIColor blackColor],NSStrokeWidthAttributeName:[NSNumber numberWithInteger:3]} range:self.body.selectedRange];
[self.body.textStorage addAttributes:@{NSStrokeColorAttributeName: [UIColor blackColor],NSStrokeWidthAttributeName:@-3} range:self.body.selectedRange];
}
//取消加粗
- (IBAction)unoutlineSelect:(UIButton *)sender {
[self.body.textStorage removeAttribute:NSStrokeWidthAttributeName range:self.body.selectedRange];
}
MXTextStatsViewController.m
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self updateUI];
}
-(void)updateUI{
self.colorfulLabel.text = [NSString stringWithFormat:@"%d colorful charactrors",[self charactersWithAttribute:NSForegroundColorAttributeName].length];
self.outlineLabel.text = [NSString stringWithFormat:@"%d outline charactors",[self charactersWithAttribute:NSStrokeWidthAttributeName].length];
}
//
-(NSAttributedString*)charactersWithAttribute:(NSString*)attributeName{
//创建属性字符串
NSMutableAttributedString *characters = [[NSMutableAttributedString alloc] init];
NSUInteger index = 0;
while ([self.textToAnglyze length] > index) {
NSRange range;
//把下标为index有attributeName这个属性的位置和长度 保存在range指向的地址中
id value = [self.textToAnglyze attribute:attributeName atIndex:index effectiveRange:&range];
if (value) {
//添加range范围内的属性字符串到characters
[characters appendAttributedString:[self.textToAnglyze attributedSubstringFromRange:range]];
index = range.location + range.length;
}else{
index++;
}
}
return characters;
}
知识点
设置label属性使用attributedText
设置textView属性使用textStorage
三、UIImageView图像处理
contentMode属性,设置图片形状
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIImage *image = [UIImage imageNamed:@"0.png"];
UIImageView *imageView = [[UIImageView alloc]initWithImage:image];
//设置
imageView.frame = self.view.frame;
imageView.contentMode = UIViewContentModeScaleAspectFit;
[self.view addSubview:imageView];
}
1 代码创建UIImageView,frame默认是图片的大小,.contentMode默认是拉伸,frame如果和图片大小不一致拉伸图片
mode风格
UIViewContentModeScaleToFill,默认拉伸
UIViewContentModeScaleAspectFit, 保持宽高比 ,完全显示,留边
UIViewContentModeScaleAspectFill, 保持宽高比,铺满imageview
Redraw做绘制时使用
UIImageView图片视图控件
* 在界面上的有限空间中显示超过界面大小的内容,(图片,文本)UITextView 是UIScrollView的子类
* 重要属性
.frame -> scrollView在界面上的实际大小和坐标
.contentSize -> scrollView需要显示的内容大小
一般情况下,内容的大小大于frame的大小,以达到滚动效果
注意:这两个属性一定都要设置,否则看不到
* 支持缩放的属性
minimumZoomScale:设置最小的缩放比例
maximumZoomScale:设置最大的缩放比例,一般为1.0
//水平方向缩放比例
float horizontalScale = scrollView.frame.size.width / self.imageView.frame.size.width;
//垂直方向缩放比例
float vertical = scrollView.frame.size.height / self.imageView.frame.size.height;
//比较水平和垂直方向哪个最小
float minimumZoomScale = MIN(horizontalScale, vertical);
//缩放最小值
scrollView.minimumZoomScale = minimumZoomScale;
//缩放最大值
scrollView.maximumZoomScale = MAX_SCALE;
scrollView.delegate = self;
//scrollview代理中进行缩放的方法,返回一个view进行缩放 按住alt健进行缩放操作
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
return self.imageView;
}
* 其他属性
. showsHorizontalScrollIndicator显示横条
. showsVerticalScrollIndicato 显示竖条
. pagingEnable启动分页控制
. contentInset 内边距
. scrollIndicatorInsets 滚轴的内边距
scrollView.scrollIndicatorInsets = UIEdgeInsetsMake(10, 0, 0, 0);
* 委托协议UIScrollViewDelegate
返回哪一个子视图进行缩放:
缩放开始时调用的方法/结束时调用的方法
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
作业:视力检查表,
Label.font = [UIFont systemFontSize:100];
界面1:label的字体大小是100 E
按钮 看得见 点击进入
界面2:90 E
按钮 看得见 点击进入
界面3:80 E
。。。
看不见显示结果
- (void)viewDidLoad
{
[super viewDidLoad];
self.lableE.font = [UIFont systemFontOfSize:100 - self.size2];
// Do any additional setup after loading the view from its nib.
}
- (IBAction)lookSee:(UIButton *)sender {
self.size2 += 10;
MXViewController *viewController = [[MXViewController alloc] initWithNibName:@"MXViewController" bundle:nil];
viewController.size2 = self.size2;
[self.navigationController pushViewController:viewController animated:YES];
}
- (IBAction)noLookSee:(UIButton *)sender {
NSLog(@"看不见了,您的视力是:%d",100 - self.size2);
}
04-UIKit(UINavigationController、NSAttributeString、UIImageView)的更多相关文章
- PHP Laravel系列之环境搭建( VirtualBox+Vagrant+Homestead+系列网址)
搭建环境从来都是阻挡一门新技能的最致命的硬伤,为了这个环境,我又是花费了半天的时间,各种问题层出不穷,下面基于网上的一些教程(我看到的都多少有些问题) 开始的时候是在实验楼这个平台上开始学习的,不过 ...
- VMware12安装虚拟机教程、Ubuntu16.04安装教程(包括vmware tools的安装)
转自https://jingyan.baidu.com/article/c275f6ba07e269e33d756714.html 方法/步骤 1 虚拟机.Linux操作系统介绍及下载地址 虚拟机VM ...
- ubuntu16.04系统深度学习开发环境、常用软件环境(如vscode、wine QQ、 360wifi驱动(第三代暂无))搭建相关资料
事后补充比较全面的(找对资料真的省一半功夫):https://www.jianshu.com/p/5b708817f5d8?from=groupmessage Ubuntu16.04 + 1080Ti ...
- MySQL学习笔记(一)Ubuntu16.04中MySQL安装配置(5.6优化、错误日志、DNS解决)
目录 第一部分.5.6安装.配置.自动备份 第二部分.5.7源码安装.配置.自动备份 第一部分.5.6安装 1.安装mysql sudo apt-get install mysql-server su ...
- 美化你的GRUB,全面支持中文(菜单、提示、帮助)适用7.04-9.04
本文根据网络资料整理而成,在此鸣谢各位作者. 本方法适合 7.04-9.04版本,9.10使用了grub2,请看这里. http://forum.ubuntu.org.cn/viewtopic.php ...
- PHP7 学习笔记(一)Ubuntu 16.04 编译安装Nginx-1.10.3、 PHP7.0.9、Redis3.0 扩展、Phalcon3.1 扩展、Swoole1.9.8 扩展、ssh2扩展(全程编译安装)
==================== PHP 7.0 编译安装================== wget http://cn2.php.net/get/php-7.0.9.tar.bz2/fr ...
- Django 04 模板标签(if、for、url、with、autoeacape、模板继承于引用、静态文件加载)
Django 04 模板标签(if.for.url.with.autoeacape.模板继承于引用.静态文件加载) 一.if.for.url.with.autoescape urlpatterns = ...
- spark 1.6.0 安装与配置(spark1.6.0、Ubuntu14.04、hadoop2.6.0、scala2.10.6、jdk1.7)
前几天刚着实研究spark,spark安装与配置是入门的关键,本人也是根据网上各位大神的教程,尝试配置,发现版本对应最为关键.现将自己的安装与配置过程介绍如下,如有兴趣的同学可以尝试安装.所谓工欲善其 ...
- QTableWidget详解(样式、右键菜单、表头塌陷、多选等) 2013-10-23 10:54:04
一.设置表单样式 点击(此处)折叠或打开 table_widget->setColumnCount(4); //设置列数 table_widget->horizontalHeader()- ...
随机推荐
- Lamd表达式
1. 普通绑定: public void button1_Click(object sender, EventArgs e) { MessageBox.Show("ok"); } ...
- BZOJ 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚( 线段树 )
线段树.. -------------------------------------------------------------------------------------- #includ ...
- 如何查看.Net源代码vs版本号以及C#项目中各文件的含义
查看.Net源代码vs版本号以及C#项目中各文件的含义 用记事本打开vs项目的.sln文件. 第2行就是这个源代码包的开发软件vs版本号了 注意了,如果是vs2003的sln文件通常没有这行,可以判断 ...
- Hbase split的过程以及解发条件
一.Split触发条件 1. 有任一一个Hfile的大小超过默认值10G时,都会进行split 2. 达到这个值不在拆分,默认为int_max,不进行拆分 3.compact ...
- matlab实现协同过滤之pdist、squareform
实现协同过滤算法的第一步是:计算用户或项目之间的相似度.接下来介绍pdist和squareform 用法: D = pdist(X) D = pdist(X,distance) D = pdist ...
- 使用BeautifulSoup解析XML文档
有200多个XML文档,每个文档类似如下: <?xml version="1.0"?> <VehicleInfo> <FileHeader> & ...
- LinuxC安装gcc
使用centos进行C编程的时候使用gcc hello.c提示 bash:gcc:command not found 此时需要给Linux安装gcc命令如下 1 yum -y install gcc ...
- C语言之三大查找算法
查找算法 1.二分查找 二分查找就是折半查找,其基本思想是:首先选取表中间位置的记录,将其关键字与给定关键字key进行比较,若相等,则查找成功.若key值比该关键字值大,则要找的元素一定在右子表中,则 ...
- 让innerHTML方法添加到元素里的js可以被解析执行
<!DOCTYPE html> <html> <head> </head> <body> <div id="test&quo ...
- haproxy 访问www.zjdev.com 自动跳转到appserver_8001 对应的nginx
# # acl zjdev_7_req hdr_beg(host) -i www.zjdev.com # use_backend appserver_8001 if zjdev_7_req