zoukankan      html  css  js  c++  java
  • 04-UIKit(UINavigationController、NSAttributeString、UIImageView)

    目录:

    一、UINavigationController导航视图控制器

    二、NSAttributeString属性字符串

    三、UIImageView图像处理

    回到顶部

    一、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);
    }

  • 相关阅读:
    学习总结(二十六)
    学习总结(二十五)
    在知乎学习怎么参加工作
    连分数系列
    Kalman Filter
    五子棋的学习
    Dijkstra
    三等分角、化圆为方、倍立方体
    女朋友走丢数学模型
    传染病模型
  • 原文地址:https://www.cnblogs.com/yangmx/p/3516442.html
Copyright © 2011-2022 走看看