iOS编程常用方法和常见bug修复

1.去除NSString字符串里面的特殊符号
-(NSString*)deleteSpecificSymbolForString:(NSString*)text{
    //先创建NSCharacterSet,把需要去除的字符串放在里面
    NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@"@/::;;¥「」"、[]{}#%-*+=_\\|~<>$€^•'@#$%^&*()()_+'\""];
    //该方法返回的就是去除特殊符号后的字符串
    NSString *trimmedString = [text stringByTrimmingCharactersInSet:set];
    return trimmedString;
}
2.判断NSString字符串里面有没有汉字
-(BOOL)estimateStringContainChinese:(NSString*)text{
    if(text){
        for (int i=0; i
3.判断NSString字符串里面有没有英文字母
-(BOOL)estimateStringContainEnglish:(NSString*)text{
    if(text){
        for (int i=0; i
4.在指定控制器里面更改状态栏文字颜色
  每一个应用程序,应该都会有统一的导航栏和状态栏风格。但是有时候,也有需要在指定控制器里面修改
状态栏的风格,如:播放视频。
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:YES];
}

5.点击进入搜索控制器时,自动弹出键盘,让搜索框进入编辑状态

当点击搜索时,进入搜索控制器,使用系统自带的搜索框,searchController.searchBar,进入时,自动实现如下图的效果


iOS编程常用方法和常见bug修复_第1张图片
效果图.png

找了很多方法,下面这个是有效方法:

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self performSelector:@selector(showKeyboard) withObject:nil afterDelay:0];
}

- (void)showKeyboard {
    [self.searchController.searchBar becomeFirstResponder];
}
6.修改系统自带搜索框的UI
  系统自带的搜索框,使用方便快捷,但是有时候它的外观与要求不符,这时候就需要我们来对系统搜索框
进行一些修改
for (UIView* subview in [[self.searchController.searchBar.subviews lastObject] subviews]) {
        //相当于UITextField,输入框,可以修改背景颜色等
        if ([subview isKindOfClass:NSClassFromString(@"UISearchBarTextField")]) {
            UITextField *textField = (UITextField*)subview;
            [textField setBackgroundColor:COLOR_RGB(238, 238, 238)];
        }
        //相当于UIButton,搜索框右侧的Cancle按钮,可以修改默认文字等
        if ([subview isKindOfClass:NSClassFromString(@"UINavigationButton")]) {
            UIButton* cancle = (UIButton*)subview;
            [cancle setTitle:@"取消" forState:UIControlStateNormal];
            [cancle setTitleColor:COLOR_RGB(82, 184, 255) forState:UIControlStateNormal];
        }
        //相当于UIView,搜索框的背景view,一般将它去掉,然后自己添加一层背景view
        if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
            [subview removeFromSuperview];
            UIView* back = [[UIView alloc]initWithFrame:self.searchController.searchBar.bounds];
            back .backgroundColor = [UIColor whiteColor];
            [self.searchController.searchBar insertSubview:back atIndex:0];
        }
    }
7.在指定控制器中隐藏导航栏
  有时候,需要在指定控制器中隐藏导航栏,试过好几个方法,或多或少有些坑,下面这个是我认为比较好的:
  首先让该控制器成为导航栏的代理:
  self.navigationController.delegate = self;
  然后,实现指定的代理方法:
  -(void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
    if ([viewController isKindOfClass:[self class]]) {
        [self.navigationController setNavigationBarHidden:YES animated:YES];
    }
    //返回时,在指定控制器重新让导航栏显示
    if ([viewController isKindOfClass:[ContactVC class]]) {
        [self.navigationController setNavigationBarHidden:NO animated:YES];
    }
}
8.根据指定文本,字体,固定宽度或高度,求尺寸
// 根据指定文本,字体和最大高度计算尺寸
- (CGSize)sizeWithText:(NSString *)text font:(UIFont *)font maxHeight:(CGFloat)height
{
    NSMutableDictionary *attrDict = [NSMutableDictionary dictionary];
    attrDict[NSFontAttributeName] = font;
    CGSize size = [text boundingRectWithSize:CGSizeMake(MAXFLOAT, height) options:NSStringDrawingUsesLineFragmentOrigin attributes:attrDict context:nil].size;
    return size;
}
// 根据指定文本,字体和最大宽度计算尺寸
- (CGSize)sizeWithText:(NSString *)text font:(UIFont *)font maxWidth:(CGFloat)width
{
    NSMutableDictionary *attrDict = [NSMutableDictionary dictionary];
    attrDict[NSFontAttributeName] = font;
    CGSize size = [text boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:attrDict context:nil].size;
    return size;
}
9.给UILabel设置行间距,字间距等富文本属性,并计算高度
//给UILabel设置行间距和字间距
-(void)setLabelSpace:(UILabel*)label withValue:(NSString*)str withFont:(UIFont*)font {
    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
    paraStyle.lineBreakMode = NSLineBreakByCharWrapping; //结尾部分的文字省略方式
    paraStyle.alignment = NSTextAlignmentLeft; //文本对齐方式
    paraStyle.lineSpacing = 7; //设置行间距
    paraStyle.hyphenationFactor = 1.0; //连字属性 在iOS,唯一支持的值分别为0和1
    paraStyle.firstLineHeadIndent = 0.0; //首行缩进
    paraStyle.paragraphSpacingBefore = 0.0; //段首行空白空间
    paraStyle.headIndent = 0; //整体缩进(首行除外)
    paraStyle.paragraphSpacing = 15; //段与段之间的间距
    
    //设置字间距 NSKernAttributeName:@1.5f
    NSDictionary *dic = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paraStyle, NSKernAttributeName:@1.5f};
    NSAttributedString *attributeStr = [[NSAttributedString alloc] initWithString:str attributes:dic];
    label.attributedText = attributeStr;
}

//计算UILabel的高度(带有行间距的情况)
-(CGFloat)getSpaceLabelHeight:(NSString*)str withFont:(UIFont*)font withWidth:(CGFloat)width {
    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
    paraStyle.lineBreakMode = NSLineBreakByCharWrapping;
    paraStyle.alignment = NSTextAlignmentLeft;
    paraStyle.lineSpacing = 7;
    paraStyle.hyphenationFactor = 1.0;
    paraStyle.firstLineHeadIndent = 0.0;
    paraStyle.paragraphSpacingBefore = 0.0;
    paraStyle.headIndent = 0;
    paraStyle.paragraphSpacing = 15;
    
    NSDictionary *dic = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paraStyle, NSKernAttributeName:@1.5f};
    CGSize size = [str boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;
    
    return size.height;
}

你可能感兴趣的:(iOS编程常用方法和常见bug修复)