-
StatusBarHeight x会返回44 普通返回20
-
self.navigationItem.rightBarButtonItem
-
在任意位置(X,Y)展示只有文字的HUD
-
颜色转换为背景 图片
-
四舍五入保留小数位
-
遍历可变数组的同时删除数组元素
-
两种方法删除NSUserDefaults所有记录
-
iOS跳转到App Store下载应用评分
-
更改iOS状态栏的颜色
-
退回上一界面的时候 判断当前控制器是push过来的 还是preset过来的,采取不同方式
-
数字放数组中 快速求总和 最大值 最小值 和 平均值
-
修改UITextField中Placeholder的文字颜色
-
阿拉伯数字转中文格式
-
通过监听textfield的变化 实现 字数限制
StatusBarHeight x会返回44 普通返回20
#define StatusBarHeight (IOS_VERSION >= 8.0 ? ([[UIApplication sharedApplication] statusBarFrame].size.height) : (IS_LANDSCAPE ? ([[UIApplication sharedApplication] statusBarFrame].size.width) : ([[UIApplication sharedApplication] statusBarFrame].size.height)))
// 操作系统版本号
#define IOS_VERSION ([[[UIDevice currentDevice] systemVersion] floatValue])
// 用户界面横屏了才会返回YES
#define IS_LANDSCAPE UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])
self.navigationItem.rightBarButtonItem
UIBarButtonItem *rightBarBtn = [[UIBarButtonItem alloc] initWithTitle:@"打印" style:UIBarButtonItemStylePlain target:self action:@selector(clickPrintBtn:)];
self.navigationItem.rightBarButtonItem = rightBarBtn;
在任意位置(X,Y)展示只有文字的HUD
+ (void) showCustomMsg:(NSString *)msg X:(CGFloat )x Y:(CGFloat)y
{
// 显示到主窗口中
MBProgressHUD *hud =[MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES];
// 显示模式,改成customView,即显示自定义图片(mode设置,必须写在customView赋值之前)
hud.mode = MBProgressHUDModeCustomView;
NSInteger delay = 1.5;
// 设置要显示 的自定义的图片
hud.labelText = msg;
hud.xOffset = x;
hud.yOffset = y;
hud.opacity = 0.9;
hud.margin = 7;
hud.cornerRadius = 3;
hud.labelFont = [UIFont systemFontOfSize:15];
// 标志:必须为YES,才可以隐藏, 隐藏的时候从父控件中移除
hud.removeFromSuperViewOnHide = YES;
// 3秒后自动隐藏
[hud hide:YES afterDelay:delay];
}
颜色转换为背景 图片
// 颜色转换为背景图片
+ (UIImage *)imageWithColor:(UIColor *)color {
CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
四舍五入保留小数位
/**
四舍五入保留小数位
@param price 价格
@param position 保留几位小数
@return 保留position位小数后的字符串
*/
+ (NSString *)notRounding:(float)price afterPoint:(NSInteger)position{
NSDecimalNumberHandler* roundingBehavior = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundBankers scale:position raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:NO];
NSDecimalNumber *ouncesDecimal;
NSDecimalNumber *roundedOunces;
ouncesDecimal = [[NSDecimalNumber alloc] initWithString:[NSString stringWithFormat:@"%f",price]];
roundedOunces = [ouncesDecimal decimalNumberByRoundingAccordingToBehavior:roundingBehavior];
NSString *roundNOPoStr = [NSString stringWithFormat:@"%@",roundedOunces];
NSString* lastStr;
if (![roundNOPoStr containsString:@"."]) {
NSString *addStr = @".";
for (int i = 0; i < position; i ++) {
addStr = [addStr stringByAppendingFormat:@"0"];
}
lastStr = [NSString stringWithFormat:@"%@%@",roundNOPoStr,addStr];
}else{
NSDecimalNumberHandler* roundingBehavior = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundBankers scale:position raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:NO];
NSDecimalNumber *ouncesDecimal;
NSDecimalNumber *roundedOunces;
ouncesDecimal = [[NSDecimalNumber alloc] initWithString:[NSString stringWithFormat:@"%f",price]];
roundedOunces = [ouncesDecimal decimalNumberByRoundingAccordingToBehavior:roundingBehavior];
NSString *roundHasPoStr = [NSString stringWithFormat:@"%@",roundedOunces];
NSArray *array1 = [roundHasPoStr componentsSeparatedByString:@"."];
NSString *tempStr = array1[1];
NSInteger count = tempStr.length ;
if (count <= position) {
NSString *addStr = @"";
for (int i = 0; i < position - tempStr.length; i ++) {
addStr = [addStr stringByAppendingFormat:@"0"];
}
lastStr = [NSString stringWithFormat:@"%@%@",roundHasPoStr,addStr];
}else{
NSArray *array2 = [ [NSString stringWithFormat:@"%f",price] componentsSeparatedByString:@"."];
NSString *tempStr = array2[1];
NSString *tailStr = [tempStr substringToIndex:position+1];
NSString *totalStr = [NSString stringWithFormat:@"%@.%@",array2[0],tailStr];
NSDecimalNumberHandler* roundingBehavior = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundBankers scale:position raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:NO];
NSDecimalNumber *ouncesDecimal;
NSDecimalNumber *roundedOunces;
ouncesDecimal = [[NSDecimalNumber alloc] initWithString:totalStr];
roundedOunces = [ouncesDecimal decimalNumberByRoundingAccordingToBehavior:roundingBehavior];
lastStr = [NSString stringWithFormat:@"%@",roundedOunces];
}
}
if (position <= 0) {
lastStr = roundNOPoStr;
}
return lastStr;
}
遍历可变数组的同时删除数组元素
NSMutableArray *copyArray = [NSMutableArray arrayWithArray:array];
NSString *str1 = @“zhangsan”;
for (AddressPerson *perModel in copyArray) {
if ([[perModel name] isEqualToString:str1]) {
/*
copyArray 和 array 里头的元素内存地址的一样的,
所以遍历copyArray 直接删 array 中的 这个Model 是可以成功删掉的
*/
[array removeObject:perModel];
}
}
两种方法删除NSUserDefaults所有记录
//方法一
NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier];
[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];
//方法二
- (void)resetDefaults
{
NSUserDefaults * defs = [NSUserDefaults standardUserDefaults];
NSDictionary * dict = [defs dictionaryRepresentation];
for (id key in dict)
{
[defs removeObjectForKey:key];
}
[defs synchronize];
}
iOS跳转到App Store下载应用评分
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"itms-apps://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=APPID"]];
更改iOS状态栏的颜色
- (void)setStatusBarBackgroundColor:(UIColor *)color
{
UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
if ([statusBar respondsToSelector:@selector(setBackgroundColor:)])
{
statusBar.backgroundColor = color;
}
}
退回上一界面的时候 判断当前控制器是push过来的 还是preset过来的,采取不同方式
NSArray *viewcontrollers=self.navigationController.viewControllers;
if (viewcontrollers.count > 1)
{
if ([viewcontrollers objectAtIndex:viewcontrollers.count - 1] == self)
{
//push方式
[self.navigationController popViewControllerAnimated:YES];
}
}
else
{
//present方式
[self dismissViewControllerAnimated:YES completion:nil];
}
数字放数组中 快速求总和 最大值 最小值 和 平均值
NSArray *array = [NSArray arrayWithObjects:@"2.0", @"2.3", @"3.0", @"4.0", @"10", nil];
CGFloat sum = [[array valueForKeyPath:@"@sum.floatValue"] floatValue];
CGFloat avg = [[array valueForKeyPath:@"@avg.floatValue"] floatValue];
CGFloat max =[[array valueForKeyPath:@"@max.floatValue"] floatValue];
CGFloat min =[[array valueForKeyPath:@"@min.floatValue"] floatValue];
NSLog(@"%f\n%f\n%f\n%f",sum,avg,max,min);
修改UITextField中Placeholder的文字颜色
[textField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];
阿拉伯数字转中文格式
+(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;
}
通过监听textfield的变化 实现 字数限制
#define MAX_STARWORDS_LENGTH 60
//在viewDidLoad中注册<UITextFieldTextDidChangeNotification>通知
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textFiledEditChanged:)
name:@"UITextFieldTextDidChangeNotification" object:myTextField];
//实现监听方法
#pragma mark - Notification Method
-(void)textFieldEditChanged:(NSNotification *)obj
{
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];
}
}
}
}
网友评论