iOS开发小技巧

.UITableView的plain样式下,取消区头停滞效果

  • (void)scrollViewDidScroll:(UIScrollView *)scrollView
    {
    CGFloat sectionHeaderHeight = sectionHead.height;
    if (scrollView.contentOffset.y=0)
    {
    scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
    }
    else if(scrollView.contentOffset.y>=sectionHeaderHeight)
    {
    scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
    }
    }

iOS 获取汉字的拼音

  • (NSString *)transform:(NSString *)chinese
    {
    //将NSString装换成NSMutableString
    NSMutableString *pinyin = [chinese mutableCopy];
    //将汉字转换为拼音(带音标)
    CFStringTransform((__bridge CFMutableStringRef)pinyin, NULL, kCFStringTransformMandarinLatin, NO);
    NSLog(@"%@", pinyin);
    //去掉拼音的音标
    CFStringTransform((__bridge CFMutableStringRef)pinyin, NULL, kCFStringTransformStripCombiningMarks, NO);
    NSLog(@"%@", pinyin);
    //返回最近结果
    return pinyin;
    }

//阿拉伯数字转中文格式

  • (NSString *)translation:(NSString *)arebic
    {
    NSString *str = arebic;
    NSArray *arabic_numerals = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"0"];
    NSArray *chinese_numerals = @[@"一",@"二",@"三",@"四",@"五",@"六",@"七",@"八",@"九",@"零"];
    NSArray *digits = @[@"个",@"十",@"百",@"千",@"万",@"十",@"百",@"千",@"亿",@"十",@"百",@"千",@"兆"];
    NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:chinese_numerals forKeys:arabic_numerals];
    NSMutableArray *sums = [NSMutableArray array];
    for (int i = 0; i < str.length; i ++) {
    NSString *substr = [str substringWithRange:NSMakeRange(i, 1)];
    NSString *a = [dictionary objectForKey:substr];
    NSString *b = digits[str.length -i-1];
    NSString *sum = [a stringByAppendingString:b];
    if ([a isEqualToString:chinese_numerals[9]])
    {
    if([b isEqualToString:digits[4]] || [b isEqualToString:digits[8]])
    {
    sum = b;
    if ([[sums lastObject] isEqualToString:chinese_numerals[9]])
    {
    [sums removeLastObject];
    }
    }else
    {
    sum = chinese_numerals[9];
    }
    if ([[sums lastObject] isEqualToString:sum])
    {
    continue;
    }
    }

      [sums addObject:sum];
    

    }
    NSString *sumStr = [sums componentsJoinedByString:@""];
    NSString *chinese = [sumStr substringToIndex:sumStr.length-1];
    NSLog(@"%@",str);
    NSLog(@"%@",chinese);
    return chinese;
    }

图片添加水印
-(UIImage *)aspjpegImage:(UIImage *)img withWord:(NSString )word
{
NSString
mark = word;

int w = img.size.width;

int h = img.size.height;
//UIGraphicsBeginImageContext创建一个基于位图的上下文(context),并将其设置为当前上下文(context)
UIGraphicsBeginImageContext(img.size);

[img drawInRect:CGRectMake(0, 0, w, h)];

NSDictionary *attr = @{
                       
                       NSFontAttributeName: [UIFont boldSystemFontOfSize:20],   //设置字体
                       
                       NSForegroundColorAttributeName : [UIColor redColor]      //设置字体颜色
                       
                       };
//文本绘制 attr代表文本属性
[mark drawInRect:CGRectMake(0, 10, 100, 50) withAttributes:attr];                 //左上角

[mark drawInRect:CGRectMake(w - 200, 10, 200, 50) withAttributes:attr];            //右上角

[mark drawInRect:CGRectMake(w - 200, h - 50 - 10, 200, 50) withAttributes:attr];   //右下角

[mark drawInRect:CGRectMake(0, h - 50 - 10, 200, 50) withAttributes:attr];        //左下角

UIImage *newImg = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return newImg;

}

取上整与取下整
floor(x),有时候也写做Floor(x),其功能是“下取整”,即取不大于x的最大整数 例如:
x=3.14,floor(x)=3
y=9.99999,floor(y)=9
与floor函数对应的是ceil函数,即上取整函数。

ceil函数的作用是求不小于给定实数的最小整数。
ceil(2)=ceil(1.2)=cei(1.5)=2.00
floor函数与ceil函数的返回值均为double型

给UIView设置图片
UIImage *image = [UIImage imageNamed:@"image"];
self.MYView.layer.contents = (__bridge id _Nullable)(image.CGImage);
self.MYView.layer.contentsRect = CGRectMake(0, 0, 0.5, 0.5);

41.防止scrollView手势覆盖侧滑手势
[scrollView.panGestureRecognizerrequireGestureRecognizerToFail:self.navigationController.interactivePopGestureRecognizer];

//字符串中是否含有中文

  • (BOOL)checkIsChinese:(NSString *)string
    {
    for (int i=0; i {
    unichar ch = [string characterAtIndex:i];
    if (0x4E00 <= ch && ch <= 0x9FA5)
    {
    return YES;
    }
    }
    return NO;
    }

你可能感兴趣的:(iOS开发小技巧)