Use Uifont to set fonts and bulk create controls in IOS development _ios

Source: Internet
Author: User

In iOS, use [Uifont Familynames] This method to obtain 72 kinds of system fonts.

Use the [Uifont fontwithname:@ Zapfino size:18] method to set the font and font size for text in space.

You can use a For loop to bulk define a control and set properties.

The following programs get the system 72 fonts and store them in an array, either by getting each font in a for loop and adding it to a mutable array, or by assigning 72 fonts directly to an array.

Note: In the case of fewer page controls, choose to create each control manually, using a looping batch to create the control when the number of controls is large and regular. The size of the control can be automatically fitted to the device by acquiring the resolution of the hardware device. The specific way is:

Screen size
CGRect rect = [[UIScreen mainscreen] bounds];
  Cgsize size = rect.size;
  CGFloat width = size.width;
  CGFloat height = size.height;
  NSLog (@ "Print%f,%f", width,height);

CGFloat Scale_screen = [UIScreen mainscreen].scale;

Program content:

#import "ViewController.h" @interface Viewcontroller () @end @implementation Viewcontroller-(void) Viewdidload {[
Super Viewdidload];
Defines a variable array that holds all fonts nsmutablearray *fontarray = [Nsmutablearray arraywithcapacity:10];
    Traversal UI font for (ID x in [uifont Familynames]) {NSLog (@ "%@", X);
  [Fontarray addobject:x];
  ///directly store the fonts in the array nsarray *fontarrauy2 = [Uifont familynames];
NSLog (@ "%@", fontarrauy2);
  Creates a label that displays the string Uilabel *mylab1 = [[Uilabel alloc]initwithframe:cgrectmake (100, 100, 200, 50)];
  Mylab1.font = [Uifont systemfontofsize:20];
  Mylab1.font = [Uifont fontwithname:@ "Zapfino" size:18];
  Mylab1.font = [Uifont fontwithname:[fontarray objectatindex:10] size:18];
  Mylab1.text = @ "HelloWorld";
  Creates a new variable array that holds the label nsmutablearray *labarr = [Nsmutablearray arraywithcapacity:100] that is created using the For loop in bulk; for (int x=0; x<24; x + +) {for (int y=0; y<3; y++) {//loop to create 72 labels, each Label Transverse spacing 135-130=5, longitudinal spacing 30-28=2, Uilabel *lab = [Uilabel alloc]initwithframe:cgrectmake (Y*135+7, x*30+20, 130, 28)];
      Lab.backgroundcolor = [Uicolor colorwithred:0.820 green:0.971 blue:1.000 alpha:1.000];
Lab.text = @ "HelloWorld";
    Add the created label to the variable array [Labarr Addobject:lab];
    }///Use a For loop to set various font formats for the 72-label font for (int i=0; i<72; i++) {Uilabel *lab = [Labarr objectatindex:i];
    NSString *fontstring = [Fontarray objectatindex:i];
    Lab.font = [Uifont fontwithname:fontstring size:18];
  [Self.view Addsubview:[labarr objectatindex:i]];
  }-(void) didreceivememorywarning {[Super didreceivememorywarning];
Dispose of any of the can is recreated.
 } @end

The above is the entire content of this article, I hope to help you learn.

Related Article

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: 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.