zoukankan      html  css  js  c++  java
  • iOS 学习

    思路:

      1.new 一个 Empty 后缀为 .txt 文件,内容随笔拷贝一段

      2.用 NSString 接收本地文件,再用一个标题拼接字符串

      3.创建一个 NSMutableParagraphStyle 实例,设置标题居中、标题距离正文间隔

      4.创建一个 NSMutableParagraphStyle 实例,设置正文左对齐、每段开头缩进 10

      5.创建一个基础动画的实例,用代理监听动画结束

    话不多说上代码

    #pragma mark -- 拼接字符
    - (void)loadData:(NSString *)filename {
        
        //第一个段落
        NSMutableParagraphStyle *first = [[NSMutableParagraphStyle alloc]init];
        first.alignment = NSTextAlignmentCenter;
        first.lineSpacing = 10;
        
        //第二个段落
        NSMutableParagraphStyle *second = [[NSMutableParagraphStyle alloc]init];
        //从左开始写
        second.alignment = NSTextAlignmentLeft;
        //首行缩进
        second.firstLineHeadIndent = 10;
        //间距
        second.lineSpacing = 10;
        
        UIFont *titleFont = [UIFont systemFontOfSize:16];
        UIFont *contextFont = [UIFont systemFontOfSize:13];
        
        NSMutableAttributedString *str1 = [[NSMutableAttributedString alloc]initWithString:@"游戏许可及服务协议" attributes:@{NSParagraphStyleAttributeName:first,NSFontAttributeName:titleFont}];
        
        //标题拼接正文,正文前加换行符
        NSString *str = [NSString stringWithFormat:@"
    %@",filename];
        
        NSMutableAttributedString *str2 = [[NSMutableAttributedString alloc]initWithString:str attributes:@{NSParagraphStyleAttributeName:second,NSFontAttributeName:contextFont}];
        
        [str1 appendAttributedString:str2];
        _textView.attributedText = str1;
    }
    
    #pragma mark -- 关闭动画
    - (void)closeAniamtion:(UIView *)view {
        CABasicAnimation *animation = [CABasicAnimation animation];
        animation.keyPath = @"transform.scale";
        animation.toValue = @0.9;
        animation.duration = 0.4;
        animation.delegate = self;
        animation.removedOnCompletion = NO;
        animation.fillMode = kCAFillModeForwards;
        [view.layer addAnimation:animation forKey:@"closeAnimation"];
    }
    
    #pragma mark -- 本地 txt 文件内容
    - (NSString *)filename {
        //本地 txt 文件
        NSString *path = [[NSBundle mainBundle]pathForResource:@"浅遇时光,静好无恙.txt" ofType:nil];
        //取出内容
        NSString *filename = [[NSString alloc]initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
        return filename;
    }
    #pragma mark -- 代理监听动画停止
    - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
    
        if ([_bgView.layer animationForKey:@"closeAnimation"] == anim) {
            [_bgView removeFromSuperview];
        }
    }

    字体间距,中文的文字宽度是相等的,英文和数字的宽度不一样

    - (void)baseChinese:(UILabel *)label loadFirst:(NSString *)firstStr second:(NSString *)secondStr
    {
        //第一个段落
        NSMutableParagraphStyle *first = [[NSMutableParagraphStyle alloc]init];
        //第二个段落
        NSMutableParagraphStyle *second = [[NSMutableParagraphStyle alloc]init];
        
        int a = (int)firstStr.length;
        int b = (int)secondStr.length;
        
        if (a>b) {
    
            NSDictionary *dict1 = @{NSParagraphStyleAttributeName:first};
            NSMutableAttributedString *firstAttr = [[NSMutableAttributedString alloc]initWithString:firstStr attributes:dict1];
            /** 计算间距 **/
            CGFloat lineGap = label.font.pointSize *(a-b)/(b-1);
            
            NSString *longSecondStr = [NSString stringWithFormat:@"
    %@",secondStr];
            NSMutableAttributedString *secondAttr = [[NSMutableAttributedString alloc]initWithString:longSecondStr attributes:@{NSParagraphStyleAttributeName:second}];
            NSRange range = {0,b};
            /** 添加间距属性 **/
            [secondAttr addAttribute:NSKernAttributeName value:[NSNumber numberWithFloat:lineGap] range:range];
            /** 拼接字符串 **/
            [firstAttr appendAttributedString:secondAttr];
            label.attributedText = firstAttr;
            
        }else if(b>a){
            
            NSRange range = {0,a};
            /** 计算间距 **/
            CGFloat lineGap = label.font.pointSize *(b-a)/(a-1);
            NSDictionary *dict1 = @{NSParagraphStyleAttributeName:first};
            NSMutableAttributedString *firstAttr = [[NSMutableAttributedString alloc]initWithString:firstStr attributes:dict1];
            /** 添加间距属性 **/
            [firstAttr addAttribute:NSKernAttributeName value:[NSNumber numberWithFloat:lineGap] range:range];
            
            NSString *longSecondStr = [NSString stringWithFormat:@"
    %@",secondStr];
            NSMutableAttributedString *secondAttr = [[NSMutableAttributedString alloc]initWithString:longSecondStr attributes:@{NSParagraphStyleAttributeName:second}];
            /** 拼接字符串 **/
            [firstAttr appendAttributedString:secondAttr];
            label.attributedText = firstAttr;
            
        }else{
            /** 如果相等,直接拼接 **/
            label.text = [NSString stringWithFormat:@"%@
    %@",firstStr,secondStr];
        }
    }

    完整代码在 github 上,传送门

  • 相关阅读:
    keycloak~管理平台的查询bug与自定rest中文检索
    gRPC四种模式、认证和授权,C#实战演示
    asp.net core 集成JWT
    从不同使用场景来介绍git的基础命令
    用scikit-learn进行LDA降维——以前没有注意,原来LDA降维竟然这么好用!对无监督的任务使用PCA进行降维,对有监督的则应用LDA。
    ID3/Cart/C4.5区别
    超参数调优——google Vizier采用迁移学习的思想,主要是从之前调参的经验中学习,为新算法提出最佳超参数
    sklearn 绘制roc曲线
    原来ROC曲线更加健壮地反映模型的效果,看来还是比较关键的(就像逻辑回归,你总是希望模型让0/1分类的数据尽可能都向两端靠对不对,ROC就是反映这个好坏的指标)
    word2vec的原理——根据单词的上下文去预测单词出现,然后让整体的概率最大化。学习这样一个神经网络,然后选择隐藏作为vector表示。最初单词是onehot编码!
  • 原文地址:https://www.cnblogs.com/asamu/p/5887989.html
Copyright © 2011-2022 走看看