一、自定义搜索栏
1、在.m文件中进行初始化
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.size = CGSizeMake(300, 30);
self.font = [UIFont systemFontOfSize:15];
self.placeholder = @"请输入查询条件";
// 提前在Xcode上设置图片中间拉伸
self.background = [UIImage imageNamed:@"searchbar_textfield_background"];
// 通过init初始化的控件大多都没有尺寸
self.searchIcon = [[UIImageView alloc] init];
self.searchIcon.image = [UIImage imageNamed:@"searchbar_textfield_search_icon"];
// contentMode:default is UIViewContentModeScaleToFill,要设置为UIViewContentModeCenter:使图片居中,防止图片填充整个imageView
self.searchIcon.contentMode = UIViewContentModeCenter;
self.searchIcon.size = CGSizeMake(30, 30);
self.leftView = self.searchIcon;
self.leftViewMode = UITextFieldViewModeAlways;
self.searchIcon.userInteractionEnabled = YES;
UITapGestureRecognizer *searchTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(searchClick)];
[self.searchIcon addGestureRecognizer:searchTap];
self.voiceIcon = [[UIImageView alloc] init];
self.voiceIcon.image = [UIImage imageNamed:@"navigationbar_pop_highlighted"];
// contentMode:default is UIViewContentModeScaleToFill,要设置为UIViewContentModeCenter:使图片居中,防止图片填充整个imageView
self.voiceIcon.contentMode = UIViewContentModeCenter;
self.voiceIcon.size = CGSizeMake(30, 30);
self.rightView = self.voiceIcon;
self.rightViewMode = UITextFieldViewModeAlways;
self.voiceIcon.userInteractionEnabled = YES;
UITapGestureRecognizer *voiceTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(voiceClick)];
[self.voiceIcon addGestureRecognizer:voiceTap];
}
return self;
}
二、block传值
1、在.h设置两个block的方法供调用
#import <UIKit/UIKit.h>
@interface ZTSearchBar : UITextField
@property (nonatomic,retain) UIImageView *searchIcon;
@property (nonatomic,retain) UIImageView *voiceIcon;
//block的使用
@property (copy,nonatomic) void (^moreActionBlock)(ZTSearchBar *search);
@property (copy,nonatomic) void (^voiceActionBlock)(ZTSearchBar *voice);
@end
为什么block使用copy修饰?
block本身是像对象一样可以retain,和release。但是,block在创建的时候,它的内存是分配在栈上的,而不是在堆上。他本身的作于域是属于创建时候的作用域,一旦在创建时候的作用域外面调用block将导致程序崩溃。因为栈区的特点就是创建的对象随时可能被销毁,一旦被销毁后续再次调用空对象就可能会造成程序崩溃,在对block进行copy后,block存放在堆区. 使用retain也可以,但是block的retain行为默认是用copy的行为实现的, 因为block变量默认是声明为栈变量的,为了能够在block的声明域外使用,所以要把block拷贝(copy)到堆,所以说为了block属性声明和实际的操作一致,最好声明为copy。
2、在.m中相应的事件中调用block方法
- (void)searchClick{
NSLog(@"点击了搜索按钮哦");
if (self.moreActionBlock) {
self.moreActionBlock(self);
}
}
- (void)voiceClick{
NSLog(@"点击了声音按钮奥");
if (self.voiceActionBlock) {
self.voiceActionBlock(self);
}
}
3、在项目中点击相应的按钮之后调用block方法
- (void)viewDidLoad {
[super viewDidLoad];
//设置navigationBar的中间为searchBar
ZTSearchBar *ztBar = [[ZTSearchBar alloc]init];
self.navigationItem.titleView = ztBar;
ztBar.moreActionBlock = ^(ZTSearchBar *search) {
NSLog(@"d更多内容返回到主界面的点击了奥");
};
ztBar.voiceActionBlock = ^(ZTSearchBar *voice) {
NSLog(@"声音搜索内容返回到主界面的点击了奥");
};
}
如下图所示
三、通知传值
1、发送通知
NSDictionary *dict = @{@"key":@"value"};
[[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:@"loadSomething" object:nil userInfo:dict]];
2、注册通知:
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(load:) name:@"loadSomething" object:nil];
3、实现监听方法
-(void)load:(NSNotification *)notification
{
NSString *str = notification.userInfo[@"key"];
// do something
}
四、遍历当前页面所有的控件
遍历当前页面的所有按钮控件,然后,对相应的选中和未按钮进行变色和选中处理。
for (UIView *subView in self.scroller.subviews) {
if([subView isKindOfClass:[UIButton class]]){
UIButton *but = (UIButton *)subView;
if ([[NSString stringWithFormat:@"%ld",(long)but.tag-1] isEqualToString:dic[@"streamTag"]]) {
but.selected = YES;
but.backgroundColor = _selColor;
}else{
but.selected = NO;
but.backgroundColor = _norColor;
}
}
}
五、富文本问题
遇到的问题:在项目中遇得到一个莫名奇妙的问题就是富文本的设置在Xcode 9.+版本拖动没问题,但是在升级到Xcode 10.+版本之后,富文本的内容无法滑动,百思不得其解。然后换了一种实现方式,通过计算出TextView的的内容的高度,但是总是计算不精确,出现各种各样的偏差无规律可循,整整折腾了一个下午的时间。造孽啊,然后还是用的晓文同学的方法,做出来的效果最为完美。反思了下,根本原因还是自己对知识点的了解不清楚导致的,下面贴出来以示警戒:
1、正确的处理方式
知识点:https://www.cnblogs.com/YouXianMing/p/3875542.html
// 获取长宽
CGFloat width = self.frame.size.width;
CGFloat height = self.frame.size.height;
//张凯设置文本透明
NSMutableAttributedString *mAttStr = [[NSMutableAttributedString alloc] initWithString:self.textString];
//1、获取段落设置的全部内容
for (NSString *key in [self.paragraphAttributes allKeys]) {
NSValue *value = self.paragraphAttributes[key];
[mAttStr addAttribute:key
value:value
range:NSMakeRange(0, self.textString.length)];
}
/*
2、遍历取出来富文本参数数组内容(ConfigAttributedString对象的数组):
1、addAttribute : 富文本属性
2、value : 富文本值
3、range : 富文本范围
*/
for (int count = 0; count < _attributes.count; count++) {
ConfigAttributedString *oneConfig = _attributes[count];
[mAttStr addAttribute:oneConfig.attribute
value:oneConfig.value
range:oneConfig.range];
}
// 给TextView添加带有内容和布局的容器
self.textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, width, height)];
self.textView.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0];
self.textView.scrollEnabled = YES;
self.textView.editable = NO;
self.textView.selectable = NO;
self.textView.layer.masksToBounds = YES;
self.textView.showsVerticalScrollIndicator = NO;
self.textView.delegate = self;
self.textView.attributedText = mAttStr;
// 添加要显示的view
[self addSubview:self.textView];
2、我的笨方法 ,死活测不准。
CGFloat textHeight = [self hideLabelLayoutHeight:self.textString withTextFontSize:13.0];
NSLog(@"~~~~~~~~~~~~~~~~~~~~~详细故事文本的高度为:%f~~~~~~~~~文本高度:%f",textHeight,_textHeight);
self.textView.contentSize = CGSizeMake(0, textHeight);
/*
@param text 文本
@param limitW 文本宽度
@param font 字体
@param lineSpacing 行高
@param lineHeightMultiple 行间距
@param lineBreakMode 段落样式
*/
- (NSInteger)hideLabelLayoutHeight:(NSString *)content withTextFontSize:(CGFloat)mFontSize
{
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = 26.5; // 行高
paragraphStyle.paragraphSpacing = 5.0f; //段落间距
// paragraphStyle.textFont = [UIFont fontWithName:@"FZQKBYSJW--GB1-0" size:16.f];
paragraphStyle.firstLineHeadIndent = 0.0f;//首行缩进
NSMutableAttributedString *attributes = [[NSMutableAttributedString alloc] initWithString:content];
[attributes addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:13.0] range:NSMakeRange(0, content.length)];
[attributes addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, content.length)];
CGSize attSize = [attributes boundingRectWithSize:CGSizeMake(self.frame.size.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil].size;
if (IS_IPad) {
return attSize.height+80;
}
// else {
//
// return attSize.height-self.textCount*4+50;
//
//
//
//// else if (self.textCount >= 40 && self.textCount < 60){
////
//// return attSize.height-self.textCount*3;
////
//// }else if (self.textCount >= 60 && self.textCount < 75){
////
//// return attSize.height-170;
////
//// }else if (self.textCount >= 75){
////
//// return attSize.height-300;
////
//// }
//
// }
// else{
// if (attSize.height < 2500) {
// return attSize.height+50;
// }else if (attSize.height > 2500 && attSize.height<3000) {
// return attSize.height+100;
// }else if (attSize.height > 3000 && attSize.height<3300) {
// return attSize.height+150;
// } else if (attSize.height > 3300 && attSize.height<3800){
// return attSize.height+90;
// }else{
// return attSize.height;
//
// }
// }
return attSize.height;
}
六、音频播放在面板上的问题
今天测试播放音频的时候突然发现,在控制面板上音频是暂停的状态,找来找去发现是音频没有及时加载出来的原因导致的。吓屎我了!
七、JsonModel的赋值
被这个问题坑了好几个小时,自以为简单不需要记录的自大害了我不止一次,走点心吧!
self.freeModel = [[AudioBookPkgModel alloc] init];//就是这里没有初始化导致数据总是出不来
self.freeModel.name = model.name;
self.freeModel.pkgImg = model.pkgImg;
self.freeModel.bookSubtitle = model.bookSubtitle;
self.freeModel.numOfBooks = model.numOfBooks;
self.freeModel.despLong = model.despLong;
self.freeModel.despShort = model.despShort;
self.freeModel.subsReqd = model.subsReqd;
self.freeModel.pkgLevel = model.pkgLevel;
NSMutableArray<Optional,AudioBookModel> *bookModelArr = [[NSMutableArray<Optional,AudioBookModel> alloc]init];
for (int i=0; i<self.playingBooks.count; i++) {
AudioBookModel *bookModel = self.playingBooks[i];
//主要目的:将数组取出来,然后放到新的数据中。
if (![bookModel.isFree isEqualToNumber:@0]) {//这里是免费的数据,然后将数据重新拼接起来
[bookModelArr addObject:bookModel];
}
self.freeModel.audioBooks = bookModelArr;
}