改变字体大小
label.font = [UIFont systemFontOfSize:20];
1.UIRefreshControl 刷新数据时的显示
swift方法:
DataManager.SharedDataManager.callBack = {
//停止刷刷新动画(有下拉刷新,就必须有停止,不然会一直转)
self.refreshControl?.endRefreshing()
self.tableView.reloadData()
}
//下拉刷新
let refreshControl = UIRefreshControl()
refreshControl.tintColor = UIColor.cyanColor()
refreshControl.attributedTitle = NSAttributedString(string: "正在刷新")
refreshControl.addTarget(self, action: "refreshControlAction:", forControlEvents: UIControlEvents.ValueChanged)
self.refreshControl = refreshControl
//刷新网络数据监听的方法
func refreshControlAction(sender: UIRefreshControl) {
//请求网络数据
DataManager.SharedDataManager.requestData()
}
2.解决有的图片显示时只是一块颜色,而显示不正常的问题
//渲染
UIImage *image = [UIImage imageNamed:@"imageName"];
image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
3.initWithNibName:方法
在视图控制器的 .m中
//(M 层)不管有没有用这个界面,都会先自动执行此方法(相当于先执行初始化方法)(在此方法中写所有的 Modle(数据) 的方法)
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
//根据给定样式来创建图标
// self.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemBookmarks tag:1];
//自定义标题和图片,选中时的图片
self.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"红色" image:[UIImage imageNamed:@"08-chat"] selectedImage:[UIImage imageNamed:@"09-chat2"]];
}
return self;
}
4.单例的写法
第一种
//从线程的方法中衍生出来的
+ (instancetype)shareSingleton{
static Singleton *singleton = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
singleton = [self new];
});
return singleton;
}
第二种
+ (instancetype)shareDataManager{
//声明静态变量,设置为空
static DataManager *manager = nil;
//判断是否为空,空则创建
if (manager == nil) {
manager = [[DataManager alloc] init];
}
//返回对象
return manager;
}
第三种(swift中)
static let SharedDataManager = DataManager()
5.毛玻璃效果
#pragma mark === 背景毛玻璃效果 ===
#warning - Cell中要clearColor的颜色
UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"xiaoxin3"]];
//毛玻璃效果
UIVisualEffectView *visualView = [[UIVisualEffectView alloc]initWithEffect:[UIBlurEffect effectWithStyle:(UIBlurEffectStyleLight)]];
visualView.frame = self.tableView.bounds;
visualView.alpha = 0.8;
[imageView addSubview:visualView];
self.tableView.backgroundView = imageView;
6.将视图截成圆形的
_customView.layer.masksToBounds = YES;
_customView.layer.cornerRadius = self.customView.bounds.size.width/2;
7.使用AFNetworking请求数据
//每请求一次数据,往数据里面添加一次
let requetOperationManager = AFHTTPRequestOperationManager()
requetOperationManager.responseSerializer.acceptableContentTypes = ["text/html"]
let param = [
"count": 10, //每次刷新多杀条数据
"page": pageCount++
]
requetOperationManager.GET(kContentListURL, parameters: param, success: { (opration, data) -> Void in
print(opration.response.URL)
//解析JSON数据
if data == nil {
return
}
//遍历字典
for item in data["items"] as! NSArray {
let content = Content()
let itemDict: NSDictionary = item as! NSDictionary
content.setValuesForKeysWithDictionary(itemDict as! [String : AnyObject])
self.allDataArr.insert(content, atIndex: 0)
}
if self.callBack != nil {
self.callBack() //用于刷新数据
}
}, failure: nil
)
8.使用NSURLSession请求网络数据
使用NSURLConnection请求数据
[NSURLConnection sendAsynchronousRequest:res queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSArray *array = dic[@"cards"];
for (NSDictionary *dic in array) {
HealthKeeping *health = [HealthKeeping new];
[health setValuesForKeysWithDictionary:dic[@"mblog"]];
[self.allDataArray addObject:health];
}
}];
9.语音
//导入包
#import <AVFoundation/AVSpeechSynthesis.h>
//声音集成器
AVSpeechSynthesizer *speechSy = [[AVSpeechSynthesizer alloc] init];
//发声器
AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc] initWithString:@"i am very happy"];
AVSpeechUtterance *utterance2 = [[AVSpeechUtterance alloc] initWithString:@"ha ha ha"];
AVSpeechUtterance *utterance3 = [[AVSpeechUtterance alloc] initWithString:_textFeild.text];
//给合成器添加发生器,让其发音
[speechSy speakUtterance:utterance];
[speechSy speakUtterance:utterance2];
[speechSy speakUtterance:utterance3];
//哪国语言
utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-US"];
//语速
utterance.rate = 0.5;
//音高
utterance.pitchMultiplier = 1.0;
10.webView
UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.frame];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:myurl]]];
self.view = webView;
11.使用第三方SDWebImage解析图片
用imageView调用,直接赋值给imageView
[cell.imgView sd_setImageWithURL:[NSURL URLWithString:news.picUrl] placeholderImage:[UIImage imageNamed:@"image"]];
12.cell的注册
第一种:系统自带的
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:reuseID];
第二种:自定义cell
[self.tableView registerNib:[UINib nibWithNibName:@"HealthCell" bundle:nil] forCellReuseIdentifier:@"cellId"];
cell的实现
注册的cell
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseID forIndexPath:indexPath];//有forIndexPath:indexPath
//不用判断直接用
没注册的cell
static NSString *cellId = @"b";
ShuangCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];//没有forIndexPath:indexPath
if (!cell) { //需要判断
cell = [[ShuangCell alloc ]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
}
设置cell的自适应高度
使用此方法必须要设置cell中的最后一个控件与cell的距离
//设定cell根据内容自适应高度
self.myTableView.rowHeight = UITableViewAutomaticDimension;
//可以不用写
self.myTableView.estimatedRowHeight = 100;
13.UIAlertController
self.alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"收藏成功" preferredStyle:(UIAlertControllerStyleAlert)];
[self presentViewController:_alert animated:YES completion:nil];
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
14.给button添加图片
[button setBackGroundImage:<#(nullable UIImage *)#> forState:UIControlStateNormal];
15.让音乐在后台播放
在plist文件中添加一条字段
然后在代码中添加
16.移除地图上的原有的大头针
//移除原来的大头针
[_mapView removeAnnotations:_mapView.annotations];
17.给界面添加View
[[UIApplication sharedApplication].keyWindow addSubview:_textFeild];
18.将URL中的汉字转换成utf-8格式,拼接到URL字符串中
NSString *typeString = (__bridge NSString *)CFURLCreateStringByAddingPercentEscapes(NULL,(__bridge CFStringRef)@"健康",NULL,(CFStringRef)@"!*'();:@&=+$,/?%#[]",kCFStringEncodingUTF8);
然后将typeString拼接到URL中
19.去除解析出来的数据带的 HTML 标记
- (NSString *)filterHTML:(NSString *)html{
NSScanner * scanner = [NSScanner scannerWithString:html];
NSString * text = nil;
while([scanner isAtEnd]==NO)
{
//找到标签的起始位置
[scanner scanUpToString:@"<" intoString:nil];
//找到标签的结束位置
[scanner scanUpToString:@">" intoString:&text];
//替换字符
html = [html stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@>",text] withString:@""];
}
return html;
}
20.去广告
先到网页,找到开发者平台,依次输入并提交
document.getElementsByClassName(‘index_mask’) -> document.getElementsByClassName(‘index_mask’)[0] -> document.getElementsByClassName(‘index_mask’)[0].style.display = ‘none’
若在网页上显示的对应的广告没了,就可以将最后一句写到下面的程序中
-(void)webViewDidFinishLoad:(UIWebView *)webView{
[webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByClassName('index_mask')[0].style.display = 'none'"];
}
21,刷新一行cell或者一个section,无需reloadData
//一个section刷新
NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:2];
[tableview reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
//一个cell刷新
NSIndexPath *indexPath=[NSIndexPath indexPathForRow:3 inSection:0];
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];