今天主要学习了Label和Button在ios中的用法,包括界面直接拖动的控件,也包括代码实现的功能。
一,切换字体
//
// ViewController.m
// LabelDemo
//
// Created by 蔡定龙 on 15-4-7.
// Copyright (c) 2015年 李灵杰. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic,strong ) NSArray *array;
@property (nonatomic,assign) NSInteger current;
@property (nonatomic,strong) UIButton *button;
@property (nonatomic,strong) UILabel *label;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self createLabel];
[self createButton];
self.array = [UIFont familyNames];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)createLabel{
NSArray *array = [UIFont familyNames];
for (NSArray *temp in array) {
NSLog(@"%@",temp);
}
//NSArray *array = [[NSArray alloc]initWithArray: _familyNames];
self.label = [[UILabel alloc]initWithFrame:CGRectMake(0, 10, 325, 200)];
_label.text = @" ViewController LabelDe Created by 蔡定龙 on 15-4-7.Copyright (c) 2015年 李灵杰. All rights reserved.";
_label.textAlignment = NSTextAlignmentCenter;
_label.numberOfLines = 0;
_label.lineBreakMode = NSLineBreakByWordWrapping;
_label.backgroundColor = [UIColor lightGrayColor];
_label.font = [UIFont fontWithName:[_array objectAtIndex:0] size:18];
[self.view addSubview:_label];
}
- (void)createButton{
self.button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
_button.frame = CGRectMake(120, 310, 100, 100);
//button.titleLabel.text=@"nihao";
[_button setTitle:@"change" forState:UIControlStateNormal];
_button.titleLabel.textColor = [UIColor blueColor];
_button.titleLabel.font = [UIFont systemFontOfSize:14];
[_button addTarget:self action:@selector(changeFont) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_button];
}
- (void)changeFont{
_current++;
if (_current>_array.count-1) {
_current = 0;
}
NSString *fontName = [_array objectAtIndex:_current];
NSLog(@"%@",fontName);
self.label.font = [UIFont fontWithName:fontName size:20];
NSLog(@"here");
}
@end
本段代码的主体是一个Label控件和一个Button控件。其中,前者实现的是字符串的输出功能;后者实现的是一个按钮,用来切换字体。值得提出的是,在Button控件中如果需要输出字符等,需要借助label实现。
在重新完成过程中,主要遇到了几个问题:1,包括self用法及其出现的位置 2,类方法及对象方法的调用巩固 3,全局变量时声明的注意事项,*表数组。。。 4,输出时结构的调整变化
// ViewController.m
// LabelDemo
//
// Created by 蔡定龙 on 15-4-7.
// Copyright (c) 2015年 李灵杰. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic,strong ) NSArray *array;
@property (nonatomic,assign) NSInteger current;
@property (nonatomic,strong) UIButton *button;
@property (nonatomic,strong) UILabel *label;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self createLabel];
[self createButton];
self.array = [UIFont familyNames];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)createLabel{
NSArray *array = [UIFont familyNames];
for (NSArray *temp in array) {
NSLog(@"%@",temp);
}
//NSArray *array = [[NSArray alloc]initWithArray: _familyNames];
self.label = [[UILabel alloc]initWithFrame:CGRectZero];
//CGRectZero是一个高度和宽度为零、位于(0,0)的矩形常量。需要创建边框但还不确定边框大小或位置时,可以使用此常量
_label.text = @" CGRectZero是一个高度和宽度为零、位于(0,0)的矩形常量。需要创建边框但还不确定边框大小或位置时,可以使用此常量 ViewController LabelDe Created by 蔡定龙 on 15-4-7.Copyright (c) 2015年 李灵杰. All rights reserved.";
_label.textAlignment = NSTextAlignmentCenter;
_label.numberOfLines = 0;
_label.lineBreakMode = NSLineBreakByWordWrapping;
_label.backgroundColor = [UIColor lightGrayColor];
_label.font = [UIFont fontWithName:[_array objectAtIndex:0] size:18];
[self.view addSubview:_label];
}
- (void)createButton{
self.button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
_button.frame = CGRectMake(120, 310, 100, 100);
//button.titleLabel.text=@"nihao";
[_button setTitle:@"change" forState:UIControlStateNormal];
_button.titleLabel.textColor = [UIColor blueColor];
_button.titleLabel.font = [UIFont systemFontOfSize:14];
[_button addTarget:self action:@selector(changeFont) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_button];
}
- (void)changeFont{
_current++;
if (_current>_array.count-1) {
_current = 0;
}
NSString *fontName = [_array objectAtIndex:_current];
NSLog(@"%@",fontName);
self.label.font = [UIFont fontWithName:fontName size:20];
//NSLog(@"here");
//计算真正尺寸的frame
CGSize size = [self sizeWithFontName:fontName];
//重新设置label标签的frame
_label.frame = CGRectMake(0, 20, 320, size.height);
}
- (CGSize)sizeWithFontName :(NSString *) fontName{
//创建一个很大的容器
CGSize bigSize = CGSizeMake(320, 768);
//创建一个字典 包含字体和字
NSDictionary *attrDic = @{NSFontAttributeName:[UIFont fontWithName:fontName size:25]};
CGSize size =[self.label.text boundingRectWithSize:bigSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrDic context:nil].size;
return size;
}
@end
本段代码是在上一段代码改动后实现的,主要的不同点在于在字符段输出的时候由于字号及字体的不同,空间大小占用不同,而本段代码就是解决了这个问题。在这部分中,完成了一个自定义方法sizeWithFontName,实现的功能就是根据字体及字号的变化,计算出容器输出的大小,CGSize.size
在第一次输出label和每次点击button后均要输出字符,因此两个地方均需重新计算容器的高度,CGSize。size。height
特别的,要理解self的用法,实现set,get方法,并灵活应用。