// 1.设置UILabel行间距
UILabel* Lab =[[UILabel alloc]init];
NSMutableAttributedString* attributeStr = [[NSMutableAttributedString alloc]initWithString:Lab.text];
NSMutableParagraphStyle* parStyle = [[NSMutableParagraphStyle alloc]init];
[parStyle setLineSpacing:20];
[attributeStr addAttribute:NSParagraphStyleAttributeName value:parStyle range:NSMakeRange(0, Lab.text.length)];
Lab.attributedText = attributeStr;
//2UILabel显示不同颜色字体
UILabel* Lab =[[UILabel alloc]init];
NSMutableAttributedString* attributeStr = [[NSMutableAttributedString alloc]initWithString:Lab.text];
[attributeStr addAttribute:NSForegroundColorAttributeName value:[UIColor yellowColor] range:NSMakeRange(0, 5)];
[attributeStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(5, 8)];
[attributeStr addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(8, Lab.text.length)];
Lab.attributedText = attributeStr;
//3.防止离屏渲染为UIImage添加圆角
- (UIImage*)circleImage{
UIGraphicsBeginImageContextWithOptions(self.size, NO, 1);//NO代表透明
CGContextRef ctx = UIGraphicsGetCurrentContext();//获得上下文
CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);//添加一个圆
CGContextAddEllipseInRect(ctx, rect);//方形变圆形
CGContextClip(ctx);//裁剪
[self drawInRect:rect];//将图片画上去
UIImage* iamge = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return iamge;
}
//判断一个字符串是否为数字
NSCharacterSet* notDigits = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
NSString* str = @"123";
if ([str rangeOfCharacterFromSet:notDigits].location ==NSNotFound) {
//是数字
}else{
//不是数字
}
//将一个view保存为pdf格式
- (void)createPDFFromUIView:(UIView *)aView saveToDocumentsWithFileName:(NSString *)fileName{
NSMutableData* pdfData = [NSMutableData data];
UIGraphicsBeginPDFContextToData(pdfData, aView.bounds, nil);
UIGraphicsBeginPDFPage();
CGContextRef pdfContext = UIGraphicsGetCurrentContext();
[aView.layer renderInContext:pdfContext];
UIGraphicsEndPDFContext();
NSString* path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject];
NSString* pdfPath = [path stringByAppendingPathComponent:fileName];
[pdfData writeToFile:pdfPath atomically:YES];
}
//获取当前导航控制器下前一个控制器
NSInteger mgINdex= [self.navigationController.viewControllers indexOfObject:self];
if (mgINdex != 0 && mgINdex !=NSNotFound) {
[self.navigationController.viewControllers objectAtIndex:mgINdex-1];//前一个控制器
}else{
//前一个控制器为nil
}
//在UIImage上绘制文字并生成新的image
- (UIImage*)Image{
UIFont* font = [UIFont boldSystemFontOfSize:12];
UIGraphicsBeginImageContext(self.size);//self---image
[self drawInRect:CGRectMake(0, 0, self.size.width, self.size.height)];
CGPoint point;
// CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
CGRect rect = CGRectMake(point.x, point.y, self.size.width, self.size.height);
[[UIColor yellowColor]set];
NSString* str = @"123";
[str drawInRect:CGRectIntegral(rect) withFont:font];
UIImage* newImage =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
//判断某一行的cell是否已经显示
CGRect cellRect = [tableView rectForRowAtIndexPath:indexpath];
BOOL comVisible = CGRectContainsRect(tableView.bounds,cellRect);
//让导航控制器pop会指定的控制器
NSArray* allVC = self.navigationController.viewControllers;
for (UIViewController* aVC in allVC) {
if ([aVC isKindOfClass:[ViewController class]]) {//ViewController-指定的控制器
[self.navigationController popToViewController:aVC animated:YES];
}
}
//设置UIImage的透明度
/*方法一*/
UIImageView* iamgev = [[UIImageView alloc]init];
iamgev.alpha = 0.5;
/*方法2--添加UIImage的分类*/
- (UIImage *)imageByApplyingAlpha:(CGFloat)alpha{
UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0f);//NO代表透明
CGContextRef ctx = UIGraphicsGetCurrentContext();//获得上下文
CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
CGContextScaleCTM(ctx, 1, -1);
CGContextTranslateCTM(ctx, 0, -rect.size.height);
CGContextSetBlendMode(ctx, kCGBlendModeMultiply);
CGContextSetAlpha(ctx, alpha);
CGContextDrawImage(ctx, rect, self.CGImage);
UIImage* newiamge = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newiamge;
}
UIWebView添加单击手势不响应---
因为UIWebView本身有一个单击手势,所以添加会造成手势冲突,从而不响应,在手势代理UIGestureRecognizerDelegate中实现下面的方法即可- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { return YES; }
地图上两个点之间的实际距离//
需要导入#importCLLocation *locA = [[CLLocation alloc] initWithLatitude:34 longitude:113]; CLLocation *locB = [[CLLocation alloc] initWithLatitude:31.05 longitude:121.76];// CLLocationDistance求出的单位为米 CLLocationDistance distance = [locA distanceFromLocation:locB];
在应用中打开设置的某个界面
// 打开设置->通用[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=General"]];// 以下是设置其他界面prefs:root=General&path=Aboutprefs:root=General&path=ACCESSIBILITYprefs:root=AIRPLANE_MODEprefs:root=General&path=AUTOLOCKprefs:root=General&path=USAGE/CELLULAR_USAGEprefs:root=Brightnessprefs:root=Bluetoothprefs:root=General&path=DATE_AND_TIMEprefs:root=FACETIMEprefs:root=Generalprefs:root=General&path=Keyboardprefs:root=CASTLEprefs:root=CASTLE&path=STORAGE_AND_BACKUPprefs:root=General&path=INTERNATIONALprefs:root=LOCATION_SERVICESprefs:root=ACCOUNT_SETTINGSprefs:root=MUSICprefs:root=MUSIC&path=EQprefs:root=MUSIC&path=VolumeLimitprefs:root=General&path=Networkprefs:root=NIKE_PLUS_IPODprefs:root=NOTESprefs:root=NOTIFICATIONS_IDprefs:root=Phoneprefs:root=Photosprefs:root=General&path=ManagedConfigurationListprefs:root=General&path=Resetprefs:root=Sounds&path=Ringtoneprefs:root=Safariprefs:root=General&path=Assistantprefs:root=Soundsprefs:root=General&path=SOFTWARE_UPDATE_LINKprefs:root=STOREprefs:root=TWITTERprefs:root=FACEBOOKprefs:root=General&path=USAGE prefs:root=VIDEOprefs:root=General&path=Network/VPNprefs:root=Wallpaperprefs:root=WIFIprefs:root=INTERNET_TETHERINGprefs:root=Phone&path=Blockedprefs:root=DO_NOT_DISTURB
在UITextView中显示html文本
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(20, 30, 100, 199)]; textView.backgroundColor = [UIColor redColor]; [self.view addSubview:textView]; NSString *htmlString = @"
Header
Subheader
Sometext
![](http://blogs.babble.com/famecrawler/files/2010/11/mickey_mouse-1097.jpg)"; NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData: [htmlString dataUsingEncoding:NSUnicodeStringEncoding] options: @{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes: nil error: nil]; textView.attributedText = attributedString;
监听scrollView是否滚动到了顶部/底部
-(void)scrollViewDidScroll: (UIScrollView*)scrollView{ float scrollViewHeight = scrollView.frame.size.height; float scrollContentSizeHeight = scrollView.contentSize.height; float scrollOffset = scrollView.contentOffset.y; if (scrollOffset == 0) { // 滚动到了顶部 } else if (scrollOffset + scrollViewHeight == scrollContentSizeHeight) { // 滚动到了底部 }}
如何把一个CGPoint存入数组里
第一种方法:
CGPointitemSprite1position =CGPointMake(100,200);
NSMutableArray* array = [[NSMutableArrayalloc] initWithObjects:NSStringFromCGPoint(itemSprite1position),nil];// 从数组中取值的过程是这样的:CGPointpoint =CGPointFromString([array objectAtIndex:0]);NSLog(@"point is %@.",NSStringFromCGPoint(point));
第二种方法:
CGPointitemSprite1position =CGPointMake(100,200);
NSValue*originValue = [NSValuevalueWithCGPoint:itemSprite1position];
NSMutableArray* array = [[NSMutableArrayalloc] initWithObjects:originValue,nil];// 从数组中取值的过程是这样的:NSValue*currentValue = [array objectAtIndex:0];CGPointpoint = [currentValueCGPointValue];NSLog(@"point is %@.",NSStringFromCGPoint(point));
修改textField的placeholder的字体颜色、大小
self.textField.placeholder = @"username is in here!";
[self.textFieldsetValue:[UIColor redColor]forKeyPath:@"_placeholderLabel.textColor"];[self.textFieldsetValue:[UIFont boldSystemFontOfSize:16]forKeyPath:@"_placeholderLabel.font"];
在ios7上使用size classes后上面下面黑色
使用了size classes后,在ios7的模拟器上出现了上面和下面部分的黑色
可以在General->App Icons and Launch Images->Launch Images Source中设置Images.xcassets来解决。
iOS 开发,工程中混合使用 ARC 和非ARC
Xcode 项目中我们可以使用 ARC 和非 ARC 的混合模式。
如果你的项目使用的非 ARC 模式,则为 ARC 模式的代码文件加入 -fobjc-arc 标签。
如果你的项目使用的是 ARC 模式,则为非 ARC 模式的代码文件加入 -fno-objc-arc 标签。
添加标签的方法:
打开:你的target -> Build Phases -> Compile Sources.
双击对应的 *.m 文件
在弹出窗口中输入上面提到的标签 -fobjc-arc / -fno-objc-arc
点击 done 保存
Xcode iOS加载图片只能用PNG
虽然在Xcode可以看到jpg的图片,但是在加载的时候会失败。
错误为 Could not load the "ReversalImage1" image referenced from a nib in the bun
必须使用PNG的图片。
如果需要使用JPG 需要添加后缀
[UIImage imageNamed:@"myImage.jpg"];
多用GCD,少用performSelector系列方法
多用GCD,少用performSelector系列方法:理由1.performSelector系列方法在内存管理方面容易有缺憾,因为它无法确定将要执行的选择子具体是什么,因而ARC编译器也就无法插入适当的内存管理方法。2.performSelector系列方法所能处理的选择子太过局限了,选择子的返回值类型以及发送给方法的参数个数都受到限制。3.如果想把任务放在另一个线程上执行,最好不要使用performSelector系列方法,而是把任务封装到块里,然后调用打中枢派发机制的相关方法去实现。