UISlider+UILable 实现对字体大小的设置
来源:互联网 发布:js按钮取消点击事件 编辑:程序博客网 时间:2024/06/05 13:30
今天主要是实现了类似于iPhone设置页面的文字字体设置,具体效果如下:
点击第一个页面的cell会跳转到字体大小详情设置页面。在字体大小详情设置页面,拖动UISlider滑动条可以改变上方文本的大小。
还有一点就是,需要将用户设置的字体大小保存在一个plist配置文件中,这样当下一次进入时就可以先读取用户之前的配置。这涉及到iPhone开发中对文件的读写操作,由于水果的沙盒机制,不同程序之间是不能互相访问文件的,每个程序都有自己的文件夹用于保存自己的数据,这当然是出于安全的考虑。个人觉得,相对于Android,水果的产品的确是要安全的多,多很多。
首先,建立一个基于视图的工程,storyBoard中拖入一个Navigation Controller,如下:
记得将Navigation Controller设置为初始页,不然后面在页面跳转时可能会出现黑屏的的情况,将UITableView的style设为Group,个人觉得plain看着不大顺眼,当然你可能有不同的看法。
将UITableView中的cell和ViewController相关联,并在ViewController中拖入一个UILable作为文本显示,一个UISlider用以拖动,一个UILable用以显示字体大小,一个UILablei显示“字体大小”作为提示。
第一个页面的.h文件:
#import <UIKit/UIKit.h>#import "ViewController.h"@interface settingRootViewController : UITableViewController<UITableViewDataSource,UITableViewDelegate>{ }@property(nonatomic,retain)IBOutlet UITableView *settingTableView;@end
注意要设置UITableView的两个delegate
第一个页面的.m文件:
#import "settingRootViewController.h"#import <QuartzCore/QuartzCore.h>@interface settingRootViewController ()@end@implementation settingRootViewController@synthesize settingTableView=_settingTableView;- (void)viewDidLoad{ [super viewDidLoad]; _settingTableView.delegate=self; _settingTableView.dataSource=self; }- (void)viewDidUnload{ [super viewDidUnload]; // Release any retained subviews of the main view. // e.g. self.myOutlet = nil;}- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{ return (interfaceOrientation == UIInterfaceOrientationPortrait);}#pragma mark - Table view data source- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ // Return the number of sections. return 1;}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ // Return the number of rows in the section. return 1;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell==nil) { cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; }// cell.layer.cornerRadius=9.0;// cell.layer.masksToBounds=YES; cell.imageView.layer.cornerRadius=9.0; cell.imageView.layer.masksToBounds=YES; UIImage *fontImage=[UIImage imageNamed:@"fontSetting.png"]; cell.imageView.image=fontImage; cell.textLabel.text=@"字体"; cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator; cell.selectionStyle=UITableViewCellSelectionStyleBlue; return cell;} @end
其中cell的设置都可以在storyBoard中完成,但还是建议大家用代码实现。因为毕竟代码是根本,等到你自己自定义cell的时候就知道了,依赖于可视化界面编程的程序员迟早要傻掉.
然后是第二个页面的.h文件:
#import <UIKit/UIKit.h>@interface ViewController : UIViewController{ NSDictionary *userDefault; BOOL plistExist;//配置文件是否存在 NSInteger showFontValue;}@property(nonatomic,retain)IBOutlet UILabel *fontLabel;@property(nonatomic,retain)IBOutlet UISlider *fontSlider;@property(nonatomic,retain) IBOutlet UILabel *fontShow;@end
第二个页面的.m文件:#import "ViewController.h"#define MAXFont 35#define MINFont 15@interface ViewController ()@end@implementation ViewController@synthesize fontLabel=_fontLabel;@synthesize fontSlider=_fontSlider;@synthesize fontShow=_fontShow;- (void)viewDidLoad{ [super viewDidLoad]; [self.view addSubview:_fontLabel]; [self.view addSubview:_fontSlider]; _fontSlider.maximumValue=MAXFont; _fontSlider.minimumValue=MINFont; [self readThePlist]; showFontValue= _fontSlider.value; _fontShow.text=[NSString stringWithFormat:@"%d",showFontValue]; _fontLabel.font=[UIFont fontWithName:@"Helvetica" size:showFontValue]; // [_fontSlider setBackgroundColor:[UIColor blueColor]]; [_fontSlider addTarget:self action:@selector(fontChanged:) forControlEvents:UIControlEventValueChanged];}-(void)readThePlist{ NSString *plistPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"userDefault.plist"]; if ([[NSFileManager defaultManager] fileExistsAtPath:plistPath]) { plistExist=true; userDefault=[[NSMutableDictionary alloc]initWithContentsOfFile:plistPath]; } else //创建文件 { NSLog(@"create"); userDefault=[[NSMutableDictionary alloc] initWithCapacity:12]; [userDefault writeToFile:plistPath atomically:YES]; } if (plistExist) { NSNumber *font=[userDefault valueForKey:@"font"]; _fontSlider.value=[font intValue]; }else { _fontSlider.value=17;//设置默认值 }}-(void)fontChanged:(UISlider *)sender{ float fontValue=sender.value; _fontLabel.font=[UIFont fontWithName:@"Helvetica" size:fontValue];//设置字体和大小 showFontValue=sender.value; _fontShow.text=[NSString stringWithFormat:@"%d",showFontValue]; [self saveToPlistFile];}-(void)saveToPlistFile{ NSString *localPath=[[NSBundle mainBundle] pathForResource:@"userDefault" ofType:@"plist"]; NSString *plistPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:@"userDefault.plist"]; if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) { [[NSFileManager defaultManager] copyItemAtPath:localPath toPath:plistPath error:nil]; } [userDefault setValue:[NSNumber numberWithInteger:showFontValue] forKey:@"font"]; NSLog(@"-----plist-----%@,count=%i",userDefault,[userDefault count]); [userDefault writeToFile:plistPath atomically:YES]; }- (void)viewDidUnload{ [super viewDidUnload]; // Release any retained subviews of the main view.}- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{ return (interfaceOrientation == UIInterfaceOrientationPortrait);}@end
实现时几点注意的地方:1.UISlider.value的值是float型,因此在显示文本大小时,须先转化为int型,再转化为NSString型赋值给_fontShow.text。
2.setValue:forKey:函数中的value只能是一个对象,所以在将文本大小这么一个NSInteger类型的数值传给函数时,需要将其转化为NSNumber。转化方式为:
[NSNumber numberWithInteger:showFontValue]
3.同样在读取plist配置文件时,需要将读出的NSNumber对象转化为NSInteger数值类型再赋值给UISlider:NSNumber *font=[userDefault valueForKey:@"font"];_fontSlider.value=[font intValue];
4.int和NSString的相互转化:
NSString *string=[NSString stringWithFormat:@"%d",showFontValue];
int showFontValue=[string intValue];
5.NSInteger和int的区别:苹果的官方文档中总是推荐用NSInteger,在苹果的api实现中,NSInteger是一个封装,它会识别当前操作系统的位数,自动返回最大的类型。所以:用NSInteger,32位系统NSInteger是一个int,即32位,但当时64位系统时,NSInteger便是64位的。6.在cell中cell.imageView的图片会挡住cell的圆角,如下:
理想的情况应该是这样:
解决的办法就是第一个.m中的:
cell.imageView.layer.cornerRadius=9.0; cell.imageView.layer.masksToBounds=YES;
当然要先添加 #import <QuartzCore/QuartzCore.h>。这一点可参考一个帅气老外的文章:http://atastypixel.com/blog/easy-rounded-corners-on-uitableviewcell-image-view/。
- UISlider+UILable 实现对字体大小的设置
- 设置UIlable字体的行距 字体大小 颜色
- UILable 的 属性设置
- UILable标签的设置
- UIlable 适应字体大小
- uislider 设置线条的宽度
- 设置gvim的字体大小
- 设置控件的字体大小
- HTML字体大小的设置
- 设置gvim的字体大小
- 应用字体大小的设置
- 如何设置UILable的各种属性
- UILable和UIButton的常用设置
- IOS之UILable删除线的设置
- 投机取巧,实现UILable的点击效果
- 将UITextField的内容设置到UILable上,求两个UITextField的和到UILable上
- CSS3的rem设置字体大小
- CSS3的REM设置字体大小
- 一份精辟的俄罗斯方块源码(335行)
- 再次呼唤大家一起来开源
- sizeof与strlen总结
- HDU 3068 ( 最长回文 )
- 跨计算机专业的10年路程--献给想跨专业的人士
- UISlider+UILable 实现对字体大小的设置
- php csv操作
- 无为而治
- 汇编复习笔记
- UITabBarController 之 设置选中和未选中的图片 隐藏tabbar
- 人吃人
- 深入理解js面向对象中的prototype
- 关于Android的Run as 不出现Android Application的问题
- SPOJ 1043