oc 基本控件

label

//
//  ViewController.m
//  OcDemoTest
//
//  Created by Mac on 2023/7/14.
//

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *label;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
- (IBAction)clickRedButton:(id)sender {
    // 改变文字的颜色
    self.label.textColor = [UIColor redColor];
    // 改变内容
    self.label.text = @"我是一段红色的文字";
    // 背景颜色
    self.label.backgroundColor = [UIColor blueColor];
    // 文字居中
    self.label.textAlignment = NSTextAlignmentCenter;
    // 改变文字的大小
    self.label.font = [UIFont systemFontOfSize:30.f];
}
- (IBAction)clickGreenButton:(id)sender {
    self.label.textColor = [UIColor greenColor];
    self.label.text = @"我是一段绿色的文字";
    self.label.backgroundColor = [UIColor redColor];
    self.label.textAlignment = NSTextAlignmentCenter;
    self.label.font = [UIFont systemFontOfSize:34.f];
}

- (IBAction)clickBlueButton:(id)sender {
    self.label.textColor = [UIColor blueColor];
    self.label.text = @"我是一段蓝色的文字";
    self.label.backgroundColor = [UIColor yellowColor];
    self.label.textAlignment = NSTextAlignmentCenter;
    self.label.font = [UIFont systemFontOfSize:40.f];
}


@end

类扩展

@interface 类名

属性 成员变量

方法声明 

@end

@implementation 类名

方法实现

@end

uiview

常见方法

-(void)addSubview:(UIView *)view  添加一个子控件

-(void) removeFromSuperview;  将自己从父控件中移除

//
//  ViewController.m
//  OcDemoTest
//
//  Created by Mac on 2023/7/14.
//

#import "ViewController.h"



@interface ViewController ()
// label
// UI成员变量可以用weak来修饰。
@property (nonatomic,weak) UILabel *label;


@end

@implementation ViewController

//
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // 创建UILabel对象
    UILabel *label = [[UILabel alloc] init];
    // 设置frame
    label.frame = CGRectMake(40, 40, 100, 60);
    label.backgroundColor = [UIColor yellowColor];
    // 添加到控制器
    [self.view addSubview:label];
    self.label = label;
   
    
}
- (IBAction)bounds:(id)sender {
    // 改变尺寸
    self.label.bounds = CGRectMake(200, 200, 100, 60);
}
- (IBAction)center:(id)sender {
    // 改变位置
    //self.label.center = CGPointMake(100, 100);
    
    self.label.center = CGPointMake(self.view.frame.size.width * 0.5, self.view.frame.size.height * 0.5);
    
}
- (IBAction)changeFrame:(id)sender {
    //方式一
    //self.label.frame = CGRectMake(200, 100, 100, 60);
    CGRect frame = self.label.frame;
    
    frame.origin.x -=100; // 改变x值
    frame.origin.y += 100; // 改变y值
    self.label.frame = frame;
}


@end

//
//  ViewController.m
//  OcDemoTest
//
//  Created by Mac on 2023/7/14.
//

#import "ViewController.h"



@interface ViewController ()
// label

@property (weak, nonatomic) IBOutlet UITextField *num1;

@property (weak, nonatomic) IBOutlet UITextField *num2;

@property (weak, nonatomic) IBOutlet UILabel *resultLabel;

@end

@implementation ViewController

//
- (void)viewDidLoad {
    [super viewDidLoad];
   
   
    
}

- (IBAction)sum:(id)sender {
    // 1.拿到两个字符串
    NSString *num1str = self.num1.text;
    NSString *num2str = self.num2.text;
    
    // 判断
    if(num1str.length == 0){
//        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"标题 输入有误" message:@"请输入第二个数" preferredStyle:UIAlertControllerStyleAlert];
//        // 添加按钮
//        // 创建并添加按钮
//        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//            NSLog(@"ok action");
//        }];
//        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
//            NSLog(@"cancel action");
//        }];
//        [alertController addAction:okAction];
//        [alertController addAction:cancelAction];
//
//        // 显示
//        [self presentViewController:alertController animated:YES completion:nil];
        
        [self showInfo:@"请输入第一个数"];
        
        return;
    }
    
    if(num2str.length == 0){
        // 调用方法
        [self showInfo:@"请输入第二个数"];
        
        return;
    }
    
    // 2.把字符串转成数值
    NSInteger sum1 = [num1str integerValue];
    NSInteger sum2 = [num2str integerValue];
    
    // 相加
    NSInteger result = sum1 + sum2;
    
    // 显示结果
    self.resultLabel.text = [NSString stringWithFormat:@"%zd",result];
    
}

// 封装方法
//  有参数
- (void)showInfo: (NSString *)info{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"标题 输入有误" message:info preferredStyle:UIAlertControllerStyleAlert];
    // 添加按钮
    // 创建并添加按钮
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"ok action");
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"cancel action");
    }];
    [alertController addAction:okAction];
    [alertController addAction:cancelAction];
    
    // 显示
    [self presentViewController:alertController animated:YES completion:nil];
    
    
}


@end

label

//
//  ViewController.m
//  OcDemoTest
//
//  Created by Mac on 2023/7/14.
//

#import "ViewController.h"



@interface ViewController ()
// label



@end

@implementation ViewController

//
- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 1.1 创建UILabel对象
    UILabel *label = [[UILabel alloc]init];
    // 设置frame
    label.frame = CGRectMake(100, 100, 100, 75);
    // 设置背景颜色
    label.backgroundColor = [UIColor redColor];
    label.text = @"文本标签文本标签文本标签文本标签文本标签文本标签文本标签文本标签";
    //居中
    label.textAlignment = NSTextAlignmentCenter;
    // 设置文字大小
    label.font = [UIFont systemFontOfSize:20.f];
    // 加粗
    label.font = [UIFont boldSystemFontOfSize:2.5f];
    // 斜体
    label.font = [UIFont italicSystemFontOfSize:20.f];
    
    // 设置文字的颜色
    label.textColor = [UIColor purpleColor];
    
    // 设置阴影
    label.shadowColor = [UIColor blackColor];
    label.shadowOffset = CGSizeMake(-2, 1);
    
    // 设置行数 0自动换行
    label.numberOfLines = 0;
    
    // 显示模式
    label.lineBreakMode = NSLineBreakByWordWrapping;
    //添加view
    [self.view addSubview:label];
   
    
}



@end

UIImageView

//
//  ViewController.m
//  OcDemoTest
//
//  Created by Mac on 2023/7/14.
//

#import "ViewController.h"



@interface ViewController ()
// label



@end

@implementation ViewController

//
- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 创建UIImageView对象
    UIImageView *imageView = [[UIImageView alloc] init];
    // 设置frame
    imageView.frame = CGRectMake(100, 100, 200, 200);
    
//    UIImageView *imageView2 = [[UIImageView alloc] init];
//    UIImage *image = [UIImage imageNamed:@"img"];
//    imageView2.frame = CGRectMake(100, 10, image.size.width, image.size.height);
//    imageView2.image = image;
    /***
        
     */
    // 设置背景
    imageView.backgroundColor = [UIColor greenColor];
    // 设置图片
    imageView.image = [UIImage imageNamed:@"img"];
    
    // 设置图片的内容模式
   // imageView.contentMode = UIViewContentModeTop;
   // imageView.contentMode = UIViewContentModeScaleToFill;
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    // 添加view中
    [self.view addSubview:imageView];
    
    //裁剪多余部分
    imageView.clipsToBounds = YES;
    
}



@end

图片文件夹放进项目中

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值