Uislider + uilable: Set the font size

Source: Internet
Author: User

Today, the text font settings are similar to those on the iPhone settings page. The specific effects are as follows:

Click cell on the first page to go To the font size details page. On the font size details page, drag the uislider slider to change the text size above.

Another point is that the font size set by the user needs to be saved in a plist configuration file, so that the current entry can read the user's previous configuration. This involves reading and writing files in iPhone development. Because of the fruit sandbox mechanism, different programs cannot access files from each other. Each program has its own folder to store its own data, this is of course out of security considerations. In my opinion, compared with Android, fruit products are indeed much more secure and secure.

First, create a view-based project and drag a navigation controller into the storyboard, as shown below:

Remember to set the navigation controller as the initial Page. Otherwise, black screen may occur when the page jumps later. Set the uitableview style to group. I personally think plain is not very pleasing to the eye, of course, you may have different opinions.

Associate cell in uitableview with viewcontroller, drag a uilable into viewcontroller for text display, a uislider for dragging, and a uilable for displaying the font size, A uilablei displays "font size" as a prompt.

The. h file on the first page:

#import <UIKit/UIKit.h>#import  "ViewController.h"@interface settingRootViewController : UITableViewController<UITableViewDataSource,UITableViewDelegate>{    }@property(nonatomic,retain)IBOutlet UITableView *settingTableView;@end


Note that you must set two delegate for uitableview.

The. M file on the first page:

# 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) returns :( 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 progress: 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 = @ "font"; cell. accessorytype = uitableviewcellaccessorydisclosureindicator; cell. selectionstyle = uitableviewcellselectionstyleblue; return cell;} @ end

Cell settings can be completed in storyboard, but we recommend that you use the code for implementation. After all, the code is fundamental. When you customize the cell, you will know that programmers who rely on visual interface programming will be stupid sooner or later.

Next is the. h file of the second page:

# Import <uikit/uikit. h> @ interface viewcontroller: uiviewcontroller {nsdictionary * userdefault; bool plistexist; // check whether nsinteger showfontvalue exists in the configuration file;} @ property (nonatomic, retain) iboutlet uilabel * fontlabel; @ property (nonatomic, retain) iboutlet uislider * fontslider; @ property (nonatomic, retain) iboutlet uilabel * fontshow; @ end

The. M file on the second page:

# 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 = minfon T; [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: @ "events"] stringbyappendingpathcomponent: @ "userdefault. plist "]; If ([[nsfilemanager defaultmanager] fileexistsatpath: plistpath]) {plistexist = true; userdefault = [[nsmutabledictionary alloc] initwithcontentsoffile: plistpath];} else // create a file {nslog (@ "CREATE"); userdefault = [[nsmutabledictionary alloc] initwithcapacity: 12]; [userdefault writetofile: PL Istpath atomically: Yes];} If (plistexist) {nsnumber * font = [userdefault valueforkey: @ "font"]; _ fontslider. value = [font intvalue];} else {_ fontslider. value = 17; // set the default value}-(void) fontchanged :( uislider *) sender {float fontvalue = sender. value; _ fontlabel. font = [uifont fontwithname: @ "Helvetica" Size: fontvalue]; // set the font and size. showfontvalue = sender. value; _ fontshow. TEXT = [nsstring stringwithformat: @ "% d", showfontva Lue]; [self savetoplistfile];}-(void) savetoplistfile {nsstring * localpath = [[nsbundle mainbundle] pathforresource: @ "userdefault" oftype: @ "plist"]; nsstring * plistpath = [[nshomedirectory () stringbyappendingpathcomponent: @ "events"] stringbyappendingpathcomponent: @ "userdefault. plist "]; If (! [[Nsfilemanager defaultmanager] defaults: 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

Notes for implementation:

1. The value of uislider. value is of the float type. Therefore, when the displayed text is large, it must be converted to the int type before being converted to the nsstring type and assigned to _ fontshow. Text.

2. setvalue: forkey: the value in the function can only be one object. Therefore, when passing an nsinteger value of the text size to the function, you need to convert it to nsnumber. Conversion Method:

[NSNumber numberWithInteger:showFontValue]

3. When reading the plist configuration file, you need to convert the read nsnumber object to the nsinteger value type and then assign the value to uislider:

NSNumber *font=[userDefault valueForKey:@"font"];_fontSlider.value=[font intValue];

4.int and nsstring conversion:

NSString *string=[NSString stringWithFormat:@"%d",showFontValue];
int showFontValue=[string intValue];

5. difference between nsinteger and INT: nsinteger is always recommended in Apple's official documents. In Apple's API implementation, nsinteger is an encapsulation that identifies the number of digits in the current operating system, the maximum type automatically returned. Therefore: With nsinteger, the 32-bit system nsinteger is an int, that is, 32-bit. However, in a 64-bit system, nsinteger is 64-bit.

6. The cell. imageview image in the cell blocks the cell rounded corner, as shown below:



The ideal situation is as follows:



The solution is in. M:

 cell.imageView.layer.cornerRadius=9.0; cell.imageView.layer.masksToBounds=YES;

Add# Import
<Quartzcore/quartzcore. h>. For more information, see http://atastypixel.com/blog/easy-rounded-corners-on-uitableviewcell-image-view /.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.