IOS课程笔记[6] 基础控件

UILabel 控件

frame 设置尺寸

复制代码
    [lb setFrame:CGRectMake(5, 0, 400, 40)];

Font 字体设置

复制代码
[lb setFont:[UIFont systemFontOfSize:20.f]]    
[lb setFont:[UIFont boldSystemFontOfSize:25.f]];
[lb setFont:[UIFont italicSystemFontOfSize:23.f]];

背景色

复制代码
[lb setBackgroundColor:[UIColor grayColor]];

shadowColor 阴影设置

设置对齐

复制代码
//NSTextAlignmentCenter 
//NSTextAlignmentLeft
//NSTextAlignmentRight
[lb setTextAlignment:NSTextAlignmentLeft];

设置行数

复制代码
//0为自动

[lb setNumberOfLines:0]

显示模式

复制代码
lb.lineBreakMode=NSLineBreakByTruncatingHead;

UIIMageView控件

资源打包

  • 查看调试包路径

    // debug模式找到资源位置

    (lldb) po NSHomeDirectory()

    /Users/bruce/Library/Developer/CoreSimulator/Devices/78EC5C14-10EA-4BC0-8020-F7668666694D/data/Containers/Data/Application/CE640BE1-3B2A-4AD6-A251-DFE4EB8AC0AA

    //Containers下的Bundle为沙箱位置

    /Users/bruce/Library/Developer/CoreSimulator/Devices/78EC5C14-10EA-4BC0-8020-F7668666694D/data/Containers/Bundle/Application/1A2D4A85-1DBB-4134-A958-E9DD5AE1E5D9/BundleMetadata.plist

  • Xcode7以下需要导入文件至Supporting Files中

  • Xcode7以上资源存放在项目配置的Rescources中,如下图

图片添加

复制代码
    //创建图片方式

    //方式一 通过资源名称

    UIImage *img2=[UIImage   imageNamed:@"img1"];

    //方式二 通过文件

    NSString *path =[[NSBundle mainBundle] pathForResource:@"top/banner" ofType:@"png"];

    UIImage *img=[UIImage imageWithContentsOfFile:path];

图片模式设置

contentMode的几个属性值:

复制代码
//可能会被裁剪

     UIViewContentModeScaleToFill, // 完全缩放

     UIViewContentModeScaleAspectFit, //按比例缩放

     UIViewContentModeScaleAspectFill, //按比例填充

        //重新绘制

     UIViewContentModeRedraw,

        //图片偏移

     UIViewContentModeCenter,

     UIViewContentModeTop,

     UIViewContentModeBottom,

     UIViewContentModeLeft,

     UIViewContentModeRight,

     UIViewContentModeTopLeft,

     UIViewContentModeTopRight,

     UIViewContentModeBottomLeft,

     UIViewContentModeBottomRight,

设置尺寸的方式

复制代码
    //创建带尺寸的Image 方式

    //方式一 直接创建

    [banner setFrame:CGRectMake(10, 500, 300, 200)];

    //方式一二 根据图片尺寸创建

    [banner setFrame:CGRectMake(10, 500, img.size.width, img.size.height)];

    //方式三  根据尺寸创建

    UIImageView *banner1=[[UIImageView alloc] initWithFrame:CGRectMake(10, 500, img.size.width, img.size.height)];

    //方式四 根据图片创建

    UIImageView *banner2=[[UIImageView alloc] initWithImage:img];

    banner2.center =CGPointMake(30, 20);

样例

复制代码
-(void) loadBanner{

    //创建图片View

    UIImageView *banner=[[UIImageView alloc] init];

    //设置背景色

    [banner setBackgroundColor:[UIColor yellowColor]];

    //设置位置大小 

    [banner setFrame:CGRectMake(10, 500, 300, 200)];

    //设置图片

    UIImage *img=[UIImage   imageNamed:@"img1"];

    [banner setImage:img];

    //设置剪裁

    banner.clipsToBounds=true;

   //设置 显示模型

    banner.contentMode=UIViewContentModeScaleToFill;

    //加毛玻璃

    UIToolbar *tool= [[UIToolbar alloc] init];

    // Bounds固定的尺寸,frame可能会随其他设置变化

    tool.frame =banner.bounds;

    tool.barStyle = UIBarStyleDefault;

    tool.alpha =0.6;

    [banner addSubview:tool];

   //添加控件

    [self.view addSubview: banner];

}
相关推荐
2501_915909062 小时前
iOS 应用反调试技详解术 检测调试器的原理与防护实践
android·ios·小程序·https·uni-app·iphone·webview
砚凝霜2 小时前
软考网络工程师|第 4 章 移动通信、CDMA 码分多址 完整备考笔记
网络·笔记
游戏开发爱好者83 小时前
TraceEagle 代理抓包详解,无感代理、去证书绑定与 HTTP/3 解密能力解析
网络协议·计算机网络·网络安全·ios·adb·https·udp
chnyi6_ya4 小时前
论文阅读笔记 | The Script is All You Need: 对话到电影级视频生成的 Agentic 框架
论文阅读·笔记
aiopencode4 小时前
SwiftUI Introspect生产环境完全指南:为什么它是安全可靠的选择
后端·ios
听雨入夜4 小时前
CMake构建学习笔记-libxml库的构建
笔记·学习
iiiiii114 小时前
【论文阅读笔记】Reparameterization Proximal Policy Optimization (RPO):将PPO的稳定性引入可微分强化学习
论文阅读·人工智能·笔记·深度学习·机器学习·机器人·具身智能
for_ever_love__5 小时前
iOS: 网络管理单例
学习·ios·objective-c
鱼子星_5 小时前
【C++】深入剖析list:list及其双向迭代器实现
开发语言·数据结构·c++·笔记·stl·list
00后程序员张5 小时前
iOS加固技术路线全面解析:Bitcode模式、源码模式与汇编模式对比及爱加密优势
android·汇编·ios·小程序·uni-app·cocoa·iphone