#define float VERSION = [[[UIDevice currentDevice] systemVersion] floatValue]
#import "UIUtil.h"
#import "CONSTS.h"
typedef enum{
kTypeImageRectangleV,
kTypeImageRectangleH,
kTypeImageSquare
} ImageType;
@implementation UIUtil
// Remove all the subviews of the given view.
+ (void)removeSubviews: (UIView *)superview{
for( UIView* view in superview.subviews ){
[view removeFromSuperview];
}
}
// Return the formated string by a given date and seperator.
+ (NSString *)getStringWithDate:(NSDate *)date seperator:(NSString *)seperator{
return [self getStringWithDate:date seperator:seperator includeYear:YES];
}
// Return the formated string by a given date and seperator.
+ (NSDate *)getDateWithString:(NSString *)str formate:(NSString *)formate{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:formate];
NSDate *date = [formatter dateFromString:str];
[formatter release];
return date;
}
+ (NSString *)getStringWithData:(NSDate *)date format:(NSString*)format {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:format];
NSString *string = [formatter stringFromDate:date];
[formatter release];
return string;
}
// Return the formated string by a given date and seperator, and specify whether want to include year.
+ (NSString *)getStringWithDate:(NSDate *)date seperator:(NSString *)seperator includeYear:(BOOL)includeYear{
if( seperator==nil ){
seperator = @"-";
}
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
if( includeYear ){
[formatter setDateFormat:[NSString stringWithFormat:@"yyyy%@MM%@dd",seperator,seperator]];
}else{
[formatter setDateFormat:[NSString stringWithFormat:@"MM%@dd",seperator]];
}
NSString *dateStr = [formatter stringFromDate:date];
[formatter release];
return dateStr;
}
// return the date by given the interval day by today. interval can be positive, negtive or zero.
+ (NSDate *)theDateRelativeTodayWithInterval:(NSInteger)interval{
return [NSDate dateWithTimeIntervalSinceNow:(24*60*60*interval)];
}
// return the date by given the interval day by given day. interval can be positive, negtive or zero.
+ (NSDate *)theDateRelativeGivenDayWithInterval:(NSInteger)interval date:(NSDate *)date{
NSTimeInterval givenDateSecInterval = [date timeIntervalSinceDate:[self theDateRelativeTodayWithInterval:0]];
return [NSDate dateWithTimeIntervalSinceNow:(24*60*60*interval+givenDateSecInterval)];
}
+ (NSString *)getWeekdayWithDate:(NSDate *)date{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSString *weekdayStr = nil;
[formatter setDateFormat:@"c"];
NSInteger weekday = [[formatter stringFromDate:date] integerValue];
if( weekday==1 ){
weekdayStr = @"星期日";
}else if( weekday==2 ){
weekdayStr = @"星期一";
}else if( weekday==3 ){
weekdayStr = @"星期二";
}else if( weekday==4 ){
weekdayStr = @"星期三";
}else if( weekday==5 ){
weekdayStr = @"星期四";
}else if( weekday==6 ){
weekdayStr = @"星期五";
}else if( weekday==7 ){
weekdayStr = @"星期六";
}
[formatter release];
return weekdayStr;
}
+ (void)showHint:(UIView *)viewToShowHint
hintCenter:(CGPoint)hintCenter
textContent:(NSString*)text{
[UIUtil showHint:viewToShowHint
hintCenter:hintCenter
textContent:text
timeToDisappear:2];
}
+ (void)showHint:(UIView *)viewToShowHint
hintCenter:(CGPoint)hintCenter
textContent:(NSString*)text
timeToDisappear:(NSInteger)t{
if (viewToShowHint == nil) {
viewToShowHint = [UIApplication sharedApplication].keyWindow;
}
UIView *hintView = [viewToShowHint viewWithTag:kTagHintView];
UIFont *textFont = [UIFont systemFontOfSize:28];
CGFloat maxWidth = 255;
int textLines = [text numberOfLinesWithFont:textFont
withLineWidth:maxWidth];
// CGFloat textHeight = [text heightWithFont:textFont
// withLineWidth:maxWidth];
int hintLblTag = 1;
UILabel *hintLbl;
if( !hintView ){
hintView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 256, 64)] autorelease];
hintView.backgroundColor = [UIColor clearColor];
hintView.tag = kTagHintView;
[viewToShowHint addSubview:hintView];
//alert_frameImg_light
UIImageView *frameImageView = [[UIImageView alloc] initWithFrame:hintView.bounds];
frameImageView.image = [[UIImage imageNamed:@"alert_frameImg_light.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:10];
frameImageView.frame = hintView.bounds;
[hintView addSubview:frameImageView];
[frameImageView release];
hintLbl = [UILabel labelWithFrame:hintView.bounds
text:text
textColor:[UIColor whiteColor]
font:textFont
tag:hintLblTag
hasShadow:NO];
hintLbl.textAlignment = UITextAlignmentCenter;
hintLbl.numberOfLines = textLines;
//[hintLbl sizeToFit];
[hintView addSubview:hintLbl];
}else {
hintLbl = (UILabel*)[hintView viewWithTag:hintLblTag];
hintLbl.text = text;
hintLbl.numberOfLines = textLines;
//[hintLbl sizeToFit];
}
hintLbl.left = round(hintLbl.left);
hintLbl.top = round(hintLbl.top);
hintView.center = hintCenter;
//[UIUtil adjustPositionToPixel:hintLbl];
CATransform3D transform = CATransform3DMakeScale(0.001, 0.001, 1.0);
hintView.layer.transform = transform;
hintView.alpha = 0;
transform = CATransform3DMakeScale(1.0, 1.0, 1.0);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
hintView.layer.transform = transform;
hintView.alpha = 1;
[UIView commitAnimations];
if (t > 0) {
transform = CATransform3DMakeScale(0.001, 0.001, 0.001);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDelay:t];
[UIView setAnimationDuration:.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
hintView.layer.transform = transform;
hintView.alpha = 0;
[UIView commitAnimations];
}
}
+ (NSString*)urlEncode:(NSString*)url stringEncoding:(NSStringEncoding)stringEncoding {
NSArray *escapeChars = @[@";" , @"/" , @"?" , @":" ,
@"@" , @"&" , @"=" , @"+" , @"$" , @"," ,
@"!", @"'", @"(", @")", @"*"];
NSArray *replaceChars = @[@"%3B" , @"%2F" , @"%3F" , @"%3A" ,
@"%40" , @"%26" , @"%3D" , @"%2B" , @"%24" , @"%2C" ,
@"%21", @"%27", @"%28", @"%29", @"%2A"];
int len = [escapeChars count];
NSString *temp = [url stringByAddingPercentEscapesUsingEncoding:stringEncoding];
for(int i = 0; i < len; i++)
{
temp = [temp stringByReplacingOccurrencesOfString:escapeChars[i]
withString:replaceChars[i]
options:NSLiteralSearch
range:NSMakeRange(0, [temp length])];
}
NSString *outString = [NSString stringWithString:temp];
return outString;
}
+ (void)adjustPositionToPixel:(UIView*)view{
view.center = CGPointMake(round(view.center.x), round(view.center.y));
}
+ (void)adjustPositionToPixelByOrigin:(UIView*)view{
view.left = round(view.left);
view.top = round(view.top);
}
+ (BOOL)isHighResolutionDevice {
float version = [[[UIDevice currentDevice] systemVersion] floatValue];
if (version >= 4.0) { //iOS4
UIScreen *mainScreen = [UIScreen mainScreen];
if( mainScreen.scale>1 ){ //iPhone4
}
return TRUE;
}
return FALSE;
}
+ (BOOL) isChineseInputMode
{
BOOL result = FALSE;
CGFloat systemVersion = [[[UIDevice currentDevice] systemVersion] floatValue];
if (systemVersion >= 5.0)
{
NSString *inputMode = [[UITextInputMode currentInputMode] primaryLanguage];
result = inputMode && [[inputMode lowercaseString] isEqualToString:@"zh-hans"];
}
return result;
}
+ (NSString*) imageName:(NSString*) name {
if (![UIUtil isHighResolutionDevice]) {
name = [name stringByAppendingString:@".png"];
}
return name;
}
+ (void)setRoundCornerForView:(UIView*)view
withRadius:(CGFloat)r{
view.layer.cornerRadius = r;
[view setNeedsDisplay];
}
+ (void)setBorderForView:(UIView*)view
withWidth:(CGFloat)width
withColor:(UIColor*)color{
view.layer.borderWidth = width;
view.layer.borderColor = color.CGColor;
[view setNeedsDisplay];
}
+ (CGSize)getFillInImageSize:(CGSize)imgSize frameSize:(CGSize)frameSize{
CGFloat newWidth;
CGFloat newHeight;
BOOL isFitWidth = YES;
if( [[self class] getImageTypeWithSize:imgSize]==kTypeImageRectangleH ){
if( [[self class] getImageTypeWithSize:frameSize]==kTypeImageRectangleH && frameSize.width/frameSize.height>imgSize.width/imgSize.height ){
isFitWidth = NO;
}
}else if( [[self class] getImageTypeWithSize:imgSize]==kTypeImageRectangleV ){
isFitWidth = NO;
if( [[self class] getImageTypeWithSize:frameSize]==kTypeImageRectangleV && frameSize.height/frameSize.width>imgSize.height/imgSize.width ){
isFitWidth = YES;
}
}else{
if( [[self class] getImageTypeWithSize:frameSize]==kTypeImageRectangleH ){
isFitWidth = NO;
}
}
if( isFitWidth ){
newWidth = frameSize.width;
newHeight = imgSize.height/imgSize.width*newWidth;
}else{
newHeight = frameSize.height;
newWidth = imgSize.width/imgSize.height*newHeight;
}
return CGSizeMake(newWidth, newHeight);
}
+ (NSInteger)getImageTypeWithSize:(CGSize)imgSize{
NSInteger type;
if( imgSize.width>imgSize.height ){
type = kTypeImageRectangleH;
}else if( imgSize.width<imgSize.height ){
type = kTypeImageRectangleV;
}else{
type = kTypeImageSquare;
}
return type;
}
+(UIImage*)getImage:(UIImage*)pImage width:(float)width height:(float)heigth
{
CGSize size = CGSizeMake(width, heigth);
UIGraphicsBeginImageContext(size);
[pImage drawInRect:CGRectMake(0,0, size.width, size.height)];
pImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return pImage;
}
+ (void)showHorizontalAnimation:(UIView*)view endX:(CGFloat)endx
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3f];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
view.left = endx;
[UIView commitAnimations];
}
+ (void)showVerticalAnimation:(UIView*)view endY:(CGFloat)endy
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3f];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
view.top = endy;
[UIView commitAnimations];
}
@end
相关推荐
以下是对iOS常用Category分类的详细说明: 1. **NSObject Category**: NSObject是所有Objective-C对象的基类,对其进行分类可以添加通用的方法,比如`descriptionWithLocale:`用于自定义对象的描述,或者`...
本篇文章将详细探讨iOS中常用的类别及其应用。 一、类别基础概念 类别是一种在不改变原有类的结构(无法添加实例变量)的情况下,向类中添加方法的方式。它通过遵循命名规则(如`MyClass+Extension`)来创建一个新...
在iOS开发中,掌握一些常用的公共方法能够极大地提高开发效率,简化代码。以下是一些关键的iOS公共方法的详解: 1. **获取磁盘总空间大小** 这个方法通过`NSFileManager`的`attributesOfFileSystemForPath:error:`...
### iOS常用控件的使用 在iOS开发过程中,有许多基础且重要的控件是开发者们经常用到的。本文将详细介绍几种常用的iOS控件及其使用方法,包括:UIAlertView、UISlider、UIDatePicker 和 UIActionSheet。 #### ...
这个名为"ios常用技术例子集合"的资源包,正是一个全面集成了多种实用技术的示例库,它包含了开发者在实际项目中可能会遇到的各种场景的解决方案。让我们逐一探讨这些技术。 首先,"文字处理"在iOS应用中无处不在,...
在iOS开发中,掌握一些常用的公共方法是提升开发效率的关键。这些方法涵盖了多个领域,包括字符串处理、数组操作、日期格式化、网络请求等。以下将详细介绍这些知识点: 1. **字符串处理**: - `NSString` 的 `...
"swift-ios常用工具集"是一个集合了多种实用工具类的资源包,主要用于处理常见的系统权限请求、数据存储以及安全相关的任务。下面将详细介绍这个工具集中包含的关键知识点。 1. **系统权限判断与请求** - **相册...
标题提到的"iOS常用事件传递方法"主要包括委托(delegate)、通知(Notification)以及Block这三种方式。下面将详细讲解这三种方法,并结合描述中的例子——调用系统打电话功能,来阐述它们的应用。 1. 委托...
本文档主要涉及iOS开发中的常用代码,尤其是与Objective-C字符串操作相关的知识点。 首先,我们来看 `%` 符号在输出格式化字符串中的作用。在C语言及其派生语言(如Objective-C)中,`%`是格式化输出的标志,用于...
在iOS平台上,应用图标是用户与应用程序交互的第一界面,它们代表了应用的视觉识别和品牌形象。...这些43个常用应用图标代表了各品牌在iOS生态系统中的独特形象,是他们与用户建立视觉联系的关键。
"iOS常用宏定义framework" 提供了一系列预定义的宏和常用类别方法,旨在提高开发效率并简化代码。下面我们将深入探讨这个框架中的关键知识点。 1. **宏定义(Macro Definitions)**: - 宏定义是C语言的一种预...
本篇文章将详细介绍iOS中常用的三种加密方法:AES(高级加密标准)、MD5(消息摘要算法5)以及Base64编码。 首先,AES是一种对称加密算法,全称为Advanced Encryption Standard,广泛应用于数据加密。它基于替换和...
这个名为"iOS常用工具类集合"的压缩包显然包含了若干实用的工具类,主要涉及自定义键盘、键盘管理、导航栏返回按钮的重写以及NSString的常见封装。以下是对这些知识点的详细说明: 1. **自定义键盘**: 自定义键盘...
16. **GVUserDefaults**:对NSUserDefaults进行封装的库,提供了更方便的本地存储操作方法。 17. **FCCurrentLocationGeocoder**:提供对当前地理位置进行正向和反向地址编码的功能。 18. **AFSwipeToHide**:在...
标题与描述中的“iOS开发常用代码”涉及到的是iOS应用程序开发中的常见编程实践和技术要点,主要聚焦于使用Objective-C或Swift语言进行UIKit框架下的界面元素定制和优化。以下将详细解析和扩展这部分内容所涵盖的...
【Cisco常用IOS文件】指的是Cisco公司为网络设备提供的操作系统镜像文件,这些文件是网络设备如路由器、交换机运行的基础。在本例中,我们关注的是适用于Cisco 7200系列路由器的IOS(Internetwork Operating System...
本示例集合“ios常用ui的demo”涵盖了多个关键的UI组件和功能,帮助开发者更好地理解和实现这些常见的iOS界面元素。下面将对每个知识点进行详细说明。 1. **程序欢迎界面**:在iOS应用启动时,通常会展示一个欢迎...
ios常用第三方库:ASIHTTPRequest MBProgressHUD SBJson SDWebImage CorePlotHeaders utility工具
这个"ios-平时项目常用分类.zip"文件包含了一个名为DDCategoryDemo的示例,很可能是为了展示如何巧妙地利用类别来增强代码的可复用性和组织性。 分类在iOS开发中的主要用途包括: 1. **扩展已有功能**:当Apple的...
在iOS开发中,类别(Category)是一种强大的工具,它允许我们扩展已有的Objective-C类,添加新的方法或者修改已有方法的行为,而无需继承。这里我们将深入探讨如何利用iOS中的类别工具类来提升项目的开发效率。 1. ...