您的位置:首页 > 移动开发 > IOS开发

iOS开发笔记(2)

2015-12-15 18:52 573 查看

iOS开发笔记(2)

关于Masonry包

Masonry就是一个自动布局框架,用起来很容易上手,也很方便。

官方给的源码例子:

UIEdgeInsets padding = UIEdgeInsetsMake(10, 10, 10, 10);
[view1 mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(superview.mas_top).with.offset(padding.top); //with is an optional semantic filler
make.left.equalTo(superview.mas_left).with.offset(padding.left);
make.bottom.equalTo(superview.mas_bottom).with.offset(-padding.bottom);
make.right.equalTo(superview.mas_right).with.offset(-padding.right);
}];


or even shorter:

[view1 mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(superview).with.insets(padding);
}];


大于等于、小于等于:

make.width.greaterThanOrEqualTo(@200);
make.width.lessThanOrEqualTo(@400)


除此之外,我还发现了Masonry一个特别好用的方法:倍数

make.width.equalTo(self.view.mas_width).multipliedBy(0.8);


但是在用的过程中,我还是发现了一些问题:

使用了Masonry的相对布局后,好像无法获取控件的高度了

RadioButton也无法使用Masonry相对布局

使用Masonry相对布局之后,Label有的时候有些功能无效,比如lineBreakMode和numberOfLines。具体什么情况可以用什么情况无效还没有测试,只是有的时候会遇到这种情况。

Label换行

label.lineBreakMode = UILineBreakModeWordWrap;
label.numberOfLines = 0;


点击空白处收回键盘

在init View的时候加上这几句代码即可:

//点击空白处收回键盘
self.userInteractionEnabled = YES;
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(fingerTapped:)];
[self.view addGestureRecognizer:singleTap];


NSNumber

NSNumber类型的要用@()括起来,比如@(3)

UIScrollView

UIScrollView与其他view不同的是,要在最后设置一下contentSize:

self.scrollView.contentSize = CGSizeMake(width, height);


NSString

连接:

label.text = [model.grade stringByAppendingFormat:@"%@%@字",@"  ",model.letter_count];


去掉字符串中的换行回车:

NSString *str = [model.content stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]]


whitespaceCharacterSet 去除空格

newlineCharacterSet 去除回车

whitespaceAndNewlineCharacterSet 去除空格和回车

图片名称大小写问题

图片名称如果是大写,代码中使用小写的时候,在模拟机上跑可以,但是真机上就不行了!

判断字符串是否为空

判断字符串是否为空的时候,我以前习惯用

[str isEqualToString@”“]

但实际上有更好的办法:判断字符串的长度:

!str.length

Label attributedText

- (NSMutableAttributedString *)contentAttributedString:(NSString *)text
{
NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithString:text];
UIFont *font = [UIFont fontWithName:@"Heiti SC" size:16];
NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
style.lineBreakMode = NSLineBreakByWordWrapping;
style.lineSpacing = 4;//行间距
style.paragraphSpacing = 0.5 * font.lineHeight;//段间距
style.hyphenationFactor = 1.0;
style.alignment = NSTextAlignmentLeft;
[string addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0,string.length)];
[string addAttribute:NSFontAttributeName value:kSYSTEMFONT(16) range:NSMakeRange(0,string.length)];
return string;
}


然后调用:

label.attributedText = [self contentAttributedString:contentLabel.text];


其他需要注意的地方

self.currentHeight 最好使用CGFloat类型,虽然NSIngteger也可以

为button加tag的时候,tag的命名要规范,不要用数字,不然加pingback别人看不懂

.h文件最好不要import头文件,因为会增加编译时间。如果需要的话,可以用
@class
类,然后再.m文件里import头文件

不要再使用
!=0
这种东西了!!!

UItableView的delegate方法里,如果数组大小为0:

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.array.count;
}


那么

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
}


就不执行了。

所以没必要在上面那个方法中单独去判断self.array.count是否为0.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: