5. Create controls and typographical controls for ios code, and 5. typeset controls for ios
1. UIView
The control added to the View is x, and y is calculated from the upper left corner of the View.
2. UIButton
UIButton contains UIlable and UIImageView
SetTitle must be used to set text. titleLabel. text cannot be changed directly because the text status is unknown.
TitleLabel. font when setting the text font
3. NSArray lazy loading
@ Property (nonatomic, strong) NSArray * apps; @ implementation MJViewController-(void) viewDidLoad {[super viewDidLoad]; // Add application information // 0. total number of columns (a maximum of three columns in a row) int totalColumns = 3; // 1. application size CGFloat appW = 85; CGFloat appH = 90; // 2. gap = (Controller view width-3 * Application width)/4 CGFloat marg.pdf = (self. view. frame. size. width-totalColumns * appW)/(totalColumns + 1); CGFloat marginY = 15; // 3. create a box (index 0 ~ 11) for (int index = 0; index <self. apps. count; index ++) {// 3. 1. create a 1-frame UIView * appView = [[UIView alloc] init]; // set the background color // appView. backgroundColor = [UIColor redColor]; // 3. 2. calculate the box position // calculate the row number and column number int row = index/totalColumns; int col = index % totalColumns; // calculate x and y CGFloat appX = marginX + col * (appW + marginX); CGFloat appY = 30 + row * (appH + marginY); // set frame appView. frame = CGRectMake (appX, appY, appW, appH); // 3. 3. add a box to view [self. view addSubview: appView]; // 3. 4. add the application information corresponding to the internal widget // 3.4.0.index position NSDictionary * appInfo = self. apps [index]; // 3.4.1. add image UIImageView * iconView = [[UIImageView alloc] init]; // set the position CGFloat iconW = 45; CGFloat iconH = 45; CGFloat iconX = (appW-iconW) * 0.5; CGFloat iconY = 0; iconView. frame = CGRectMake (iconX, iconY, iconW, iconH); // set the iconView of the image. image = [UIImage imageNamed: appInfo [@ "icon"]; [appView addSubview: iconView]; // 3.4.2. add the name UILabel * nameLabel = [[UILabel alloc] init]; // set the location CGFloat nameW = appW; CGFloat nameH = 20; CGFloat nameX = 0; CGFloat nameY = iconY + iconH; nameLabel. frame = CGRectMake (nameX, nameY, nameW, nameH); // set the text nameLabel. text = appInfo [@ "name"]; // set the font nameLabel. font = [UIFont systemFontOfSize: 13]; // you can specify whether the text is centered or alignment. textAlignment = NSTextAlignmentCenter; [appView addSubview: nameLabel]; // 3.4.3. add the Download button UIButton * downloadBtn = [[UIButton alloc] init]; // set the location CGFloat downloadX = 12; CGFloat downloadY = nameY + nameH; CGFloat downloadW = appW-2 * downloadX; CGFloat downloadH = 20; downloadBtn. frame = CGRectMake (downloadX, downloadY, downloadW, downloadH); // set the Default background UIImage * normalImage = [UIImage imageNamed: @ "buttongreen"]; [downloadBtn restart: normalImage forState: UIControlStateNormal]; // set the highlighted background UIImage * highImage = [UIImage imageNamed: @ "buttongreen_highlighted"]; [downloadBtn setBackgroundImage: highImage forState: UIControlStateHighlighted]; // set the text of the button [downloadBtn setTitle: @ "Download" forState: UIControlStateNormal]; // It is not recommended to directly obtain the label setting text in the button. // downloadBtn. titleLabel. text = @ "5435345345"; // set the font of the button text to downloadBtn. titleLabel. font = [UIFont systemFontOfSize: 13]; [appView addSubview: downloadBtn] ;}}- (NSArray *) apps {if (_ apps = nil) {// initialization // 1. obtain the full path NSString * path = [[NSBundle mainBundle] pathForResource: @ "app. plist "ofType: nil]; // 2. load array _ apps = [NSArray arrayWithContentsOfFile: path];} return _ apps ;}