一、绘制NSString
- 创建UIView的子类,重写drawRect:方法,在drawRect:方法中调用用于绘制字符串的方法
#import "SSString.h"
@implementation SSString
-(void)drawRect:(CGRect)rect{
//设置系统默认字体的大小
UIFont *sytemFont = [UIFont systemFontOfSize:[UIFont systemFontSize]];
//不能自动换行;drawAtPoint:表示绘制字符串的左上角;withFont:表示字体
//[self.String drawAtPoint:CGPointMake(0, 0) withFont:sytemFont];
//自动换行;drawInRect:表示制定绘制范围;
//[self.String drawInRect:rect withFont:sytemFont];
//换行与省略;lineBreakMode:表示制定省略与换行的模式
//[self.String drawInRect:rect withFont:sytemFont lineBreakMode:UILineBreakModeTailTruncation];
//横向位置的控制;alignment:表示制定靠左或居中或靠右
//[self.String drawInRect:rect withFont:sytemFont lineBreakMode:UILineBreakModeTailTruncation alignment:UITextAlignmentCenter];
//字符的缩小与位置控制;forWidth:表示绘制界面的宽度;fontSize:表示文字缩小的值;baselineAdjustment:表示缩小纵向的位置
//[self.String drawAtPoint:CGPointMake(0, 0) forWidth:375 withFont:sytemFont fontSize:10 lineBreakMode:UILineBreakModeTailTruncation baselineAdjustment:UIBaselineAdjustmentAlignBaselines];
//字符串的自动缩小;minFontSize:表示允许缩小的最小值;actualFontSize:表示输出指针型参数,取得实际绘制字符串的字体尺寸,如果不需要此参数可以写NULL
CGFloat actualFontSize;
[self.String drawAtPoint:CGPointMake(0, 0) forWidth:375 withFont:sytemFont minFontSize:5 actualFontSize:&actualFontSize lineBreakMode:UILineBreakModeWordWrap baselineAdjustment:UIBaselineAdjustmentAlignBaselines];
NSLog(@"%F",actualFontSize);
}
@end
- 在viewDidLoad方法中调用该类创建对象,设置属性并添加到父视图
#import "ViewController.h"
#import "SSString.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
SSString *str = [[SSString alloc]init];
str.frame = CGRectMake(0, 200, 375, 50);
str.backgroundColor = [UIColor greenColor];
str.String = @"回忆里阳光温暖,光线照射在阿北的脸上,总觉得是温润如玉的,却不知是时光在他脸上宣泄,慢慢淌过,便是岁月流逝。";
[self.view addSubview:str];
}
@end
二、UIFont
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UILabel *lable1 = [self buildLabelWithFrame:CGRectMake(0, 40, 375, 50)];
UILabel *lable2 = [self buildLabelWithFrame:CGRectMake(0, 120, 375, 50)];
UILabel *lable3 = [self buildLabelWithFrame:CGRectMake(0, 200, 375, 50)];
UILabel *lable4 = [self buildLabelWithFrame:CGRectMake(0, 280, 375, 50)];
UILabel *lable5 = [self buildLabelWithFrame:CGRectMake(0, 360, 375, 50)];
UILabel *lable6 = [self buildLabelWithFrame:CGRectMake(0, 440, 375, 50)];
UILabel *lable7 = [self buildLabelWithFrame:CGRectMake(0, 520, 375, 50)];
//系统默认字体
lable1.font = [UIFont systemFontOfSize:[UIFont systemFontSize]];
//小字体尺寸
lable2.font = [UIFont systemFontOfSize:[UIFont smallSystemFontSize]];
//标签用字体
lable3.font = [UIFont systemFontOfSize:[UIFont labelFontSize]];
//按钮用字体
lable4.font = [UIFont systemFontOfSize:[UIFont buttonFontSize]];
//粗体字
lable5.font = [UIFont boldSystemFontOfSize:30];
//斜体字
lable6.font = [UIFont italicSystemFontOfSize:30];
//使用“Courer-Bold”字体
lable7.font = [UIFont fontWithName:@"Courer-Bold" size:30];
}
//创建标签
-(UILabel*)buildLabelWithFrame:(CGRect)frame{
UILabel *lable = [[UILabel alloc]init];
lable.frame = frame;
lable.backgroundColor = [UIColor greenColor];
lable.textAlignment = NSTextAlignmentCenter;
lable.text = @"不同的字体";
[self.view addSubview:lable];
return lable;
}
@end
三、UIColor
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UILabel *lable1 = [self buildLabelWithFrame:CGRectMake(0, 40, 375, 50)];
UILabel *lable2 = [self buildLabelWithFrame:CGRectMake(0, 120, 375, 50)];
UILabel *lable3 = [self buildLabelWithFrame:CGRectMake(0, 200, 375, 50)];
UILabel *lable4 = [self buildLabelWithFrame:CGRectMake(0, 280, 375, 50)];
UILabel *lable5 = [self buildLabelWithFrame:CGRectMake(0, 360, 375, 50)];
//使用系统预设颜色
lable1.backgroundColor = [UIColor greenColor];
//使用系统字体所用颜色
lable2.backgroundColor = [UIColor lightGrayColor];
//使用自定义颜色,指定RGB的值
lable3.backgroundColor = [UIColor colorWithRed:0.9 green:0 blue:0.5 alpha:1];
//使用自定义颜色,指定HSB的值
lable4.backgroundColor = [UIColor colorWithHue:1 saturation:1 brightness:1 alpha:1];
//根据指定图片创建类似的颜色
lable5.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"photo"]];
}
//创建标签
-(UILabel*)buildLabelWithFrame:(CGRect)frame{
UILabel *lable = [[UILabel alloc]init];
lable.frame = frame;
lable.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:lable];
return lable;
}
@end