UI基础第二天(自学)

本文详细介绍了如何使用Objective-C创建自定义字体和颜色的UI标签,包括系统默认字体、小字体、标签用字体、按钮用字体、粗体字和斜体字的设置,以及如何通过RGB值、HSB值或图片创建自定义颜色。此外,还展示了如何在视图中添加这些定制标签。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、绘制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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值