Skip to content

Instantly share code, notes, and snippets.

@wangzz
Last active August 29, 2015 14:01
Show Gist options
  • Save wangzz/de7c9d52bb4d7b29107b to your computer and use it in GitHub Desktop.
Save wangzz/de7c9d52bb4d7b29107b to your computer and use it in GitHub Desktop.
记录有用的代码片段
记录有用的代码片段
it is a test file.
@wangzz
Copy link
Author

wangzz commented Aug 11, 2014

  • iOS5及以上版本layout放在:
viewWillLayoutSubviews:

viewDidLayoutSubviews: 
  • iOS4系统layout放在:
layoutSubviews

@wangzz
Copy link
Author

wangzz commented Aug 11, 2014

计算文本区域:

+ (CGSize)sizeOfString:(NSString *)string font:(UIFont *)font size:(CGSize)size
{
    CGSize stringSize = CGSizeZero;
    if (SYSTEM_VERSION_LESS_THAN(@"7.0")) {
        stringSize = [string sizeWithFont:font
                        constrainedToSize:size
                            lineBreakMode:UILineBreakModeWordWrap];
    } else {
        stringSize = [string boundingRectWithSize:size
                                          options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
                                       attributes:font.fontDescriptor.fontAttributes
                                          context:nil].size;
    }

    return stringSize;
}

@wangzz
Copy link
Author

wangzz commented Oct 22, 2014

更改UIToolBar背景色

if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) {
        toolBar.translucent = NO;
        toolBar.barTintColor = [UIColor whiteColor];
 }

@wangzz
Copy link
Author

wangzz commented Oct 22, 2014

四舍五入保留两位小数

float a = 3.456; //保留到小数点后两位
float b =(int)((a * 100) + 0.5) / 100.0;

//output b = 3.46;

@wangzz
Copy link
Author

wangzz commented Oct 23, 2014

不同view继承体系之间区域转换

例如一个视图控制器的view中有一个UITableView,UITableView的某个cell中有个UITextField,想要得到UITextField在view中的位置:

CGRect rect = [self.view convertRect:textField.frame fromView:textField.superview];
//或者
CGRect rect = [textField.superview convertRect:textField.frame toView:self.view];

不同view继承体系之间点转换

  • 把一个点从一个坐标系转换到接收者的坐标系
- (CGPoint)convertPoint:(CGPoint)point fromView:(UIView *)view

参数
point
一个视图中坐标系上的点
view
一个视图包含了点和他自身坐标系。如果是图是nil,那么这个方法将尝试转换基于窗口的坐标系。否则视图和那个接收者必须属于同一个UIWindow对象。

  • 转换一个点从接收者坐标系到给定的视图坐标系
- (CGPoint)convertPoint:(CGPoint)point toView:(UIView *)view

参数
point
一个在调用者坐标系中的点
view
一个包含了需要被转换的点的视图。如果视图是nil,那么这个方法将会转换成基于窗口的坐标。否则视图和接收者都要属于同一个UIWindow对象。
返回值

@wangzz
Copy link
Author

wangzz commented Nov 25, 2014

Block 使用方式

How Do I Declare A Block in Objective-C?

  • As a local variable:
returnType (^blockName)(parameterTypes) = ^returnType(parameters) {...};
  • As a property:
@property (nonatomic, copy) returnType (^blockName)(parameterTypes);
  • As a method parameter:
- (void)someMethodThatTakesABlock:(returnType (^)(parameterTypes))blockName;
  • As an argument to a method call:
[someObject someMethodThatTakesABlock:^returnType (parameters) {...}];
  • As a typedef:
typedef returnType (^TypeName)(parameterTypes);
TypeName blockName = ^returnType(parameters) {...};

URL:
http://goshdarnblocksyntax.com/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment