本文主要是总结一下在开发的时候,用到的一些小技巧,希望能帮助你们,快速的完成某些功能~
1.设置navigationbar成透明的而不是带模糊的效果?
[self.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
self.navigationBar.shadowImage = [UIImage new];
self.navigationBar.translucent = YES;
2.滚动的时候隐藏导航栏
self.navigationController.hidesBarsOnSwipe = YES;
动态隐藏NavigationBar
//1.当我们的手离开屏幕时候隐藏- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{
if(velocity.y > 0)
{
[self.navigationController setNavigationBarHidden:YES animated:YES];
} else
{
[self.navigationController setNavigationBarHidden:NO animated:YES];
}
}
//2.在滑动过程中隐藏
//像safari
(1)self.navigationController.hidesBarsOnSwipe = YES;
(2)- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
CGFloat offsetY = scrollView.contentOffset.y + __tableView.contentInset.top;
CGFloat panTranslationY = [scrollView.panGestureRecognizer translationInView:self.tableView].y;
if (offsetY > 64) {
if (panTranslationY > 0) {
//下滑趋势,显示
[self.navigationController setNavigationBarHidden:NO animated:YES];
} else {
//上滑趋势,隐藏
[self.navigationController setNavigationBarHidden:YES animated:YES];
}
} else {
[self.navigationController setNavigationBarHidden:NO animated:YES];
}
}
3.计算方法耗时时间间隔
#define TICK CFAbsoluteTime start = CFAbsoluteTimeGetCurrent();
#define TOCK NSLog(@"Time: %f", CFAbsoluteTimeGetCurrent() - start)
4.NSDate与NSString的相互转化
-(NSString *)dateToString:(NSDate *)date {
// 初始化时间格式控制器
NSDateFormatter *matter = [[NSDateFormatter alloc] init];
// 设置设计格式
[matter setDateFormat:@"yyyy-MM-dd hh:mm:ss zzz"];
// 进行转换
NSString *dateStr = [matter stringFromDate:date]; return dateStr;
}
-(NSDate *)stringToDate:(NSString *)dateStr {
// 初始化时间格式控制器
NSDateFormatter *matter = [[NSDateFormatter alloc] init];
// 设置设计格式
[matter setDateFormat:@"yyyy-MM-dd hh:mm:ss zzz"];
// 进行转换
NSDate *date = [matter dateFromString:dateStr];
return date;
}
5.隐藏navigationBar下面的细线
// 但是有细线,这就需要我们做进一步处理,把线去掉,如下方法即可:
self.navigationController.navigationBar.shadowImage = [UIImage new];
6.设置navigationBar渐变透明效果
//navigationBar是一个复合视图,它是有许多个控件组成的,那么我们就可以从他的内部入手
[[self.navigationController.navigationBar subviews] objectAtIndex:0].alpha = 0;
//这里可以根据scrollView的偏移量来设置alpha就实现了渐变透明的效果
7.判断NSString中是否包含中文
-(BOOL)isChinese:(NSString *)str{
NSString *match=@"(^[\u4e00-\u9fa5]+$)";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF matches %@", match];
return [predicate evaluateWithObject:str];
}
8.判断相机是否可用
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
// 这里时刻用的情况
}
else {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"手机摄像头设备损坏" message:@"" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alertView show];
}
9.对textFiled进行字符个数的限制
在控制器里添加一个通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldEditChanged:) name:@"UITextFieldTextDidChangeNotification" object:nil];
实现通知中的方法
#pragma mark - Notification Method
-(void)textFieldEditChanged:(NSNotification *)obj{
UITextField *textField = (UITextField *)obj.object;
NSInteger maxLength = MAX_STARWORDS_LENGTH;// 设置最大的输入长度
NSString *toBeString = textField.text;
if (toBeString.length > maxLength) {
NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:maxLength];
if (rangeIndex.length == 1) {
textField.text = [toBeString substringToIndex:maxLength];
} else {
NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, maxLength)];
textField.text = [toBeString substringWithRange:rangeRange];
}
}
}
还有一种写法也可以
UITextField *textField = (UITextField *)obj.object;
NSString *toBeString = textField.text;
//获取高亮部分
UITextRange *selectedRange = [textField markedTextRange];
UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
// 没有高亮选择的字,则对已输入的文字进行字数统计和限制
if (!position) {
if (toBeString.length > MAX_STARWORDS_LENGTH) {
NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:MAX_STARWORDS_LENGTH];
if (rangeIndex.length == 1) {
textField.text = [toBeString substringToIndex:MAX_STARWORDS_LENGTH];
} else {
NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, MAX_STARWORDS_LENGTH)];
textField.text = [toBeString substringWithRange:rangeRange];
} }
}
10.对一个字符串进行MD5加密
- (NSString *)md5:(NSString *)str{
const char *cStr = [str UTF8String];
unsigned char result[16];
CC_MD5(cStr, strlen(cStr), result);
// This is the md5 call
return [NSString stringWithFormat: @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", result[0], result[1], result[2], result[3],result[4], result[5], result[6], result[7], result[8], result[9], result[10], result[11], result[12], result[13], result[14], result[15] ];
}
11.父控件半透明而子控件不透明
设置父控件的背景颜色时候用colorWithAlphaComponent:方法
fuView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
直接在上面添加子控件就ok了
12.通过文字内容来计算文字高度
/**
- 计算文本高度
- @param text 文本
- @param limitW 文本宽度
- @param font 字体
- @param lineSpacing 行高
- @param lineHeightMultiple 行间距
- @param lineBreakMode 段落样式
- @return
*/
-
(CGSize)calculateSizeWithText:(NSString *)text limitWidth:(CGFloat)limitW font:(UIFont *)font lineSpacing:(CGFloat)lineSpacing lineHeightMultiple:(CGFloat)lineHeightMultiple lineBreakMode:(NSLineBreakMode )lineBreakMode{
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = lineSpacing;
paragraphStyle.lineHeightMultiple = lineHeightMultiple;
paragraphStyle.lineBreakMode = lineBreakMode;
NSDictionary *cyZoneDocAttribute = @{NSFontAttributeName:font,NSParagraphStyleAttributeName: paragraphStyle};CGSize size;
size = [text boundingRectWithSize:CGSizeMake(limitW, MAXFLOAT) options: NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:cyZoneDocAttribute context:nil].size;
return size;
}
这个是设置这个UILabel的富文本属性
/**
- 富文本
- @param string 文本
- @param font 字体
- @param lineSpacing 行高
- @param lineHeightMultiple 行间距(1.5倍)
- @param lineBreakMode 段落样式
- @return
*/
- (NSMutableAttributedString *)getAttributedStringWithString:(NSString *)string font:(UIFont *)font lineSpacing:(CGFloat)lineSpacing lineHeightMultiple:(CGFloat)lineHeightMultiple lineBreakMode:(NSLineBreakMode )lineBreakMode{
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = lineSpacing;
paragraphStyle.lineHeightMultiple = lineHeightMultiple;
paragraphStyle.lineBreakMode = lineBreakMode;
NSDictionary *cyZoneDocAttribute = @{NSFontAttributeName:font,NSParagraphStyleAttributeName: paragraphStyle};
[attributedString addAttributes:cyZoneDocAttribute range:NSMakeRange(0, [string length])];
return attributedString;
}
13.获取设备的唯一标示
+ (NSString *)getDeviceId
{
NSString * currentDeviceUUIDStr = [SSKeychain passwordForService:@"com.xinghengedu"account:@"uuid"];
if (currentDeviceUUIDStr == nil || [currentDeviceUUIDStr isEqualToString:@""])
{
NSUUID * currentDeviceUUID = [UIDevice currentDevice].identifierForVendor;
currentDeviceUUIDStr = currentDeviceUUID.UUIDString;
currentDeviceUUIDStr = [currentDeviceUUIDStr stringByReplacingOccurrencesOfString:@"-" withString:@""];
currentDeviceUUIDStr = [currentDeviceUUIDStr lowercaseString];
[SSKeychain setPassword: currentDeviceUUIDStr forService:@"com.xinghengedu"account:@"uuid"];
}
return currentDeviceUUIDStr;
}
14.判断一个字符串都是数字
+ (BOOL)isPureInt:(NSString *)string{
NSScanner* scan = [NSScanner scannerWithString:string];
int val;
return [scan scanInt:&val] && [scan isAtEnd];
}
15.使用block时
__weak __typeof(self)weakSelf = self;
__strong __typeof(weakSelf)strongSelf = weakSelf;