小问题 笔记

搜索中文并替换

搜索中文并替换
objecC
(@“["]*[\u4E00-\u9FA5]+[”\n]?")\s
替换
LocalizedString($1)

Swift 时搜索
(“["]*[\u4E00-\u9FA5]+[”\n]?")\s
替换
LocalizedString(key: $1)

1

将controller A 加到另一个controller B上,如果想执行A中的-(void)viewWillAppear:(BOOL)animated{}方法,
需要在添加A的时候调用 [self addChildViewController:A];方法。否则A中的-(void)viewWillAppear:(BOOL)animated{}不执行。

2 消除导航条底部的黑线黑道

self.navigationController.navigationBar.shadowImage = [[UIImage alloc] init];

3 按钮点击没有选中效果

midButton.adjustsImageWhenHighlighted = NO;

4 protobuf 使用

参考:https://www.jianshu.com/p/915875f17a76
安装完成之后使用,首先cd到verify.proto文件所在目录,然后使用命令
protoc verify.proto --objc_out="./" 会自动输出.h和.m文件到当前目录。

注意

使用protoc verify.proto --objc_out="./" 命令时,可能出错,最好是直接将命令复制到终端,然后修改文件名字。

5

读取本地含有中文的文件内容,为空的问题 或者是乱码的问题 或者 NSData转NSString时为空
原因可能有很多,但很大程度是是因为编码问题
明明拿到了NSData,确定此Data为NSString,可就是转换不成功。

NSString *path = [[NSBundle mainBundle] pathForResource:@"note" ofType:@"txt"];
    NSStringEncoding myEncoding = CFStringConvertEncodingToNSStringEncoding (kCFStringEncodingGB_18030_2000);
    NSData *  allFileData = [NSData dataWithContentsOfFile:path];
   NSString *rawString=[[NSString alloc]initWithData:data encoding:myEncoding];

原文链接:https://blog.csdn.net/args_/article/details/50888254

6添加点击tabeleview空白部分(未显示cell的部分)的手势

 UITableView * oneTableView  = [[UITableView alloc] initWithFrame:CGRectMake(0,0, SCREEN_WIDTH, SCREEN_HEIGHT-HYXF_StatusBarAndNavigationBarHeight) style:UITableViewStylePlain];
        oneTableView.delegate = self;
        oneTableView.dataSource = self;
        oneTableView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
        oneTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
        oneTableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
        oneTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        oneTableView.showsVerticalScrollIndicator = NO;
        oneTableView.showsHorizontalScrollIndicator = NO;
        [self.view addSubview:oneTableView];
        oneTableView.layer.cornerRadius = WMAKENEW(4);
        oneTableView.estimatedRowHeight = WMAKENEW(35);
        
        UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hiddenMytable)];
        //需要在controller中实现此方法
        [gesture setNumberOfTapsRequired:1];
        [oneTableView addGestureRecognizer:gesture];
        gesture.delegate = self;

#pragma mark---点击tabeleview空白部分(未显示cell的部分)的点击事件
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    if ([touch.view isKindOfClass:[UITableView class]]) {
        return true;
    }
    return false;
}

计算富文本的size

-(CGSize )getNeedSize:(NSAttributedString *)attStr{
    NSDictionary *attributesFromString = [attStr attributesAtIndex:0 longestEffectiveRange:nil inRange:NSMakeRange(0, attStr.length)];
    CGSize needSize = [attStr.string boundingRectWithSize:CGSizeMake(1000, 200) options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin attributes:attributesFromString context:nil].size;
    return needSize;
}

在6sp中,label自动换行问题

在6sp中,label设置自动换行之后,还是会出现省略号,没有根据实际需要的行数来自动改变高度而是少一行。此时,需要不仅仅同时设置下面两个属性,才可以实现自动换行并自动改变高度的效果。

middleLabel.adjustsFontSizeToFitWidth = YES;
middleLabel.numberOfLines = 0;

iOS中UICollectionView布局对齐方式的runtime实现

参考:https://www.jianshu.com/p/de08c2679241
经常遇到UI设计师设计出多个按钮的布局效果,但是系统默认的CollectionView布局总是不尽人意.例如这种标签式的控件,放到控制器上往往会变成了这样
在这里插入图片描述

相信大部分人也用过此类控件,如果想让标签左对齐,往往要写很多代码去自定义UICollectionViewLayout.我也遇到过这种问题,我在自定义UICollectionViewLayout后,发现在iOS9下,这种布局配合AutoLayout时,reloadData后会导致Crash,改了很久未果.偶然发现了一个方法,简单快捷,使用到了UICollectionViewFlowLayout的私有方法,可以简单快捷的实现我要的效果.只需要加上几行代码即可.

下面这个方法是UICollectionViewFlowLayout的私有方法.下面来跟大家解释一下options需要传的参数

  • (void)_setRowAlignmentsOptions:(NSDictionary *)options;

options根据runtime可以获取到默认key有3个,其value为NSTextAlignment的NSNumber值:

UIFlowLayoutCommonRowHorizontalAlignmentKey //水平对齐方式

UIFlowLayoutLastRowHorizontalAlignmentKey //当前行最后一个cell的对齐方式

UIFlowLayoutRowVerticalAlignmentKey //垂直对齐方式

经过试验,前两个key在scrollDirection为UICollectionViewScrollDirectionVertical时生效,最后一个在UICollectionViewScrollDirectionVertical模式下不生效.

UIFlowLayoutCommonRowHorizontalAlignmentKey默认值为NSTextAlignmentJustified,意思就是水平分布,这就是为什么每行cell的间距都不一样的罪魁祸首了

UIFlowLayoutLastRowHorizontalAlignmentKey的默认值为NSTextAlignmentCenter,意思就是最后一个cell居中对齐,这就是为什么一行只有一个cell的时候,这个cell都是居中的罪魁祸首了.

当我们改变了这两个默认值后

SEL sel = NSSelectorFromString(@“_setRowAlignmentsOptions:”);

if ([self.collectionView.collectionViewLayout respondsToSelector:sel]) {

    ((void(*)(id,SEL,NSDictionary*))objc_msgSend)(self.collectionView.collectionViewLayout,sel,

                @{@"UIFlowLayoutCommonRowHorizontalAlignmentKey":@(NSTextAlignmentLeft),

                @"UIFlowLayoutLastRowHorizontalAlignmentKey" : @(NSTextAlignmentLeft),

                @"UIFlowLayoutRowVerticalAlignmentKey" : @(NSTextAlignmentCenter)});   

}

我们看看效果

在这里插入图片描述

ok达到了我们的效果.

我们再来看一点好玩的.把UIFlowLayoutCommonRowHorizontalAlignmentKey的对齐方式改成right会怎样呢?

在这里插入图片描述

ok了,key的释义出来了,UIFlowLayoutCommonRowHorizontalAlignmentKey的对齐方式为right时,如果UIFlowLayoutLastRowHorizontalAlignmentKey为left,那么一行只有一个cell时,这个cell会左对齐.

注:此方法为UIKit框架下的公有类的私有方法,可以上架,经过测试,可以适配从iOS 9至iOS 12的所有机型.对此方法还有不懂的地方,可以联系我或者直接在下方留言,我看到会第一时间回复你们.

更新:

swift写法

导入

Import ObjectiveC

collectionView.collectionViewLayout.perform(Selector.init((“_setRowAlignmentsOptions:”)),with:NSDictionary.init(dictionary:[“UIFlowLayoutCommonRowHorizontalAlignmentKey”:NSNumber.init(value:NSTextAlignment.left.rawValue)]));

7 设置TextView的边界与文字距离

UITextView 
textView.textContainerInset默认值是UIEdgeInsetsMake(8, 0, 8, 0),就是文字会有一个距离上下8的距离。可以自行设置取消这个距离。
并且textView.textContainer.lineFragmentPadding 默认是5,就是默认文字会有一个距左边5的距离,可以设为0,就会取消这个距离。

8 选择文件、选择图片、选择通讯录时 设置返回以及取消按钮颜色

  [[UIBarButtonItem appearance] setTintColor:UIColor.blackColor];

9 将view生成图片

//其中0.0 就是控制清晰度的
  UILabel *jobLabel;
    UIGraphicsBeginImageContextWithOptions(jobLabel.bounds.size, NO, 0.0);
       [jobLabel.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

10 设置button中图片、文字对齐方式
Button.contentHorizontalAlignment
Button.contentVerticalAlignment

10、调用接口时出现“发生了SSL错误,无法建立与该服务器的安全连接”问题,导致无法继续;

原因:在iOS 9.0以后,强制执行TLS1.2,但是连接的服务器不支持TLS1.2,导致出现此问题;
解决方法:在info.plist文件中添加配置,将对应地址执行TLS1.1,以避免出现此问题;
代码如下:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值