UIKit基础知识
UIWindow
class UIWindow : UIView
定义:
The backdrop for your app’s user interface and the object that dispatches events to your views.
You use windows only when you need to do the following:
- Provide a main window to display your app’s content.
- Create additional windows (as needed) to display additional content.
UIWindow 是App的渲染主窗口。在APP启动时就为其加载并设置RootViewController。
App中UIWindow对象只有一个。所有的显示必须加载到UIWindow 上才可以呈现在人的眼前。
创建一个window
// 初始化方法
init(windowScene: UIWindowScene)
例如:
var window: UIWindow?
func scene(_ scene: UIScene,
willConnectTo session: UISceneSession,
options connectionOptions: UIScene.ConnectionOptions) {
//UIWindowScene:A specific type of scene that manages one or more windows for your app.
//@available(iOS 13.0, *) iOS 13.0 可用
guard let windowScene = (scene as? UIWindowScene) else { return }
window = UIWindow(windowScene: windowScene)
}
设置UIWindow大小
window.frame = UIScreen.main.bounds
window.frame = CGRect(x: 0, y: 0,
width: UIScreen.main.bounds.size.width,
height: UIScreen.main.bounds.size.height)
例如:以下设置会将window的启动进行移动
window?.frame = CGRect(x: 100, y: 100,
width: UIScreen.main.bounds.size.width,
height: UIScreen.main.bounds.size.height)
设置根控制器
var rootViewController: UIViewController? { get set }
window?.rootViewController = ViewController();
设置背景
window?.backgroundColor = UIColor.white
让window显示
//Shows the window and makes it the key window.
window?.makeKeyAndVisible()
// 如果只是简单地显示,可以将 isHidden 设置为false
坐标
坐标是数学中的一个基本概念。
在二维的平面空间,相互垂直的两条数轴即可构成一个直角坐标系,其公共原点即为坐标系的原点(origin), 水平的数轴叫做x轴(x-axis)或横轴,垂直的数轴叫做y轴(y-axis)或纵轴。
在iOS中,也有多种坐标体系。这里我们仅讨论UIKit 中的坐标体系,即固定左上角为原点(0,0)的视图坐标系。以原点向右侧为X轴正方向,原点下侧为Y轴正方向。
坐标值由浮点数来表示,内容的布局和定位因此具有更高的精度,还可以支持与分辨率无关的特性。
CGPoint 与 CGSize
iOS采用CGPoint来表示点在坐标系上X、Y位置。
// 创建一个坐标点
let _ = CGPoint(x: 100, y: 100)
//CGPoint 的其它方法属性
// CGPoint.zero 即 CGPoint(x: 0, y: 0)
CGPoint.zero
// 获取坐标点
let point = CGPoint(x: 100, y: 100)
print(point.x ) // 输出 100.0
iOS采用CGSize来表示视图的宽度和高度,即视图的大小。
//初始化一个size
let size = CGSize(width: 100, height: 100)
// 获取width
print(size.width) // 输出:100.0
// 等同 CGSize(width: 0, height: 0)
let zeroSize = CGSize.zero
print(zeroSize.width) // 输出 0.0
而CGRect则是结合了CGPoint和CGSize,用来表示矩形的位置和大小。它的origin表示矩形右上角所在位置(CGPoint),size表示矩形的大小(CGSize)。
CGRect(origin: <CGPoint>, size: <CGSize>)
frame、bounds和center
视图对象通过frame、bounds、和center属性声明来跟踪自己的大小和位置。
frame属性包含一个矩形,即边框矩形,用于指定视图相对于其父视图坐标系统的位置和大小。
bounds属性也包含一个矩形,即边界矩形,负责定义视图相对于本地坐标系统的位置和大小。
虽然边界矩形的原点通常被设置为 (0, 0),但这并不是必须的。
center属性包含边框矩形的中心点。
即:frame描述的是在其父视图中的CGRect,而bounds描述的是在其自身视图中的CGRect。
// frame 定义了 UILable在父视图中的坐标为 (100,100)
let lable = UILabel(frame: CGRect(x: 100, y: 100, width: 100, height: 100))
/**
获取手机屏幕宽度和高度
*/
let deviceW = UIScreen.main.bounds.size.width
let deviceH = UIScreen.main.bounds.size.height
如果还不明白,可以看下图理解:
PS:来源于斯坦福的iOS课程截图
UIColor 与CGColor
UIColor是UIKit中存储颜色信息的一个重要的类,一个UIColor对象包含了颜色和透明度的值。
同时,UIColor包含了一些类方法用于创建一些最常见的颜色,如白色,黑色。
class UIColor : NSObject
extension UIColor {
// 在浅色背景上显示文本的系统颜色。
open class var lightText: UIColor { get }
// 在深色背景上显示文本的系统颜色。
open class var darkText: UIColor { get }
// 分组表的背景的系统颜色。
open class var groupTableViewBackground: UIColor { get }
}
open class UIColor : NSObject, NSSecureCoding, NSCopying {
// 常见的颜色快捷方式
open class var black: UIColor { get } // 0.0 white
open class var darkGray: UIColor { get } // 0.333 white
open class var lightGray: UIColor { get } // 0.667 white
open class var white: UIColor { get } // 1.0 white
open class var gray: UIColor { get } // 0.5 white
open class var red: UIColor { get } // 1.0, 0.0, 0.0 RGB
open class var green: UIColor { get } // 0.0, 1.0, 0.0 RGB
open class var blue: UIColor { get } // 0.0, 0.0, 1.0 RGB
open class var cyan: UIColor { get } // 0.0, 1.0, 1.0 RGB
open class var yellow: UIColor { get } // 1.0, 1.0, 0.0 RGB
open class var magenta: UIColor { get } // 1.0, 0.0, 1.0 RGB
open class var orange: UIColor { get } // 1.0, 0.5, 0.0 RGB
open class var purple: UIColor { get } // 0.5, 0.0, 0.5 RGB
open class var brown: UIColor { get } // 0.6, 0.4, 0.2 RGB
open class var clear: UIColor { get } // 0.0 white, 0.0 alpha
}
创建RGBA颜色:
// red:
/**
The red value of the color object. On applications linked for iOS 10 or later, the color is specified in an extended color space, and the input value is never clamped. On earlier versions of iOS, red values below 0.0 are interpreted as 0.0, and values above 1.0 are interpreted as 1.0.
*/
UIColor(red: <CGFloat>, green: <CGFloat>, blue: <CGFloat>, alpha: <CGFloat>)
即:red 的值在 0.0 - 1.0 之间,和我们正常使用的有些区别,一般使用时,可以简单的封装下:
extension UIColor {
// Method returns a custom color
static func rgba(red: CGFloat, green: CGFloat, blue: CGFloat,alpha:CGFloat) -> UIColor {
return .init(red: blue / 255, green: green / 255, blue: blue / 255, alpha: alpha)
}
}
// 使用
lable.textColor = UIColor.rgba(red: 40, green: 196, blue: 124,alpha: 1.0)
如何使用16进制创建颜色?
使用rgba时,还有有些不方便,如何直接使用16进制颜色呢?同样需要简单扩展一下:
https://stackoverflow.com/questions/1560081/how-can-i-create-a-uicolor-from-a-hex-string
CGColor主要用于CoreGaphics框架之中。
CGColor主要由CGColorSapce和Color Components两个部分组成,同样的颜色组成,如果颜色空间不同的话,解析出来的结果可能会有所不同。
class CGColor
extension CGColor {
@available(iOS 2.0, *)
public /*not inherited*/ init?(colorSpace space: CGColorSpace,
components: UnsafePointer<CGFloat>)
@available(iOS 13.0, *)
public init(genericGrayGamma2_2Gray gray: CGFloat, alpha: CGFloat)
@available(iOS 13.0, *)
public init(srgbRed red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat)
@available(iOS 2.0, *)
public /*not inherited*/ init?(patternSpace space: CGColorSpace,
pattern: CGPattern,
components: UnsafePointer<CGFloat>)
}
UIColor 与CGColor 转换
//to CGColor
UIColor.lightGray.cgColor
UIFont 与 CGFont
定义:
UIFont
provides you with access to the font’s characteristics and also provides the system with access to the font’s glyph information, which is used during layout. You use font objects by passing them to methods that accept them as a parameter.You do not create font objects using the
alloc
andinit
methods. Instead, you use class methods ofUIFont
, such aspreferredFont(forTextStyle:)
, to look up and retrieve the desired font object. These methods check for an existing font object with the specified characteristics and return it if it exists. Otherwise, they create a new font object based on the desired font characteristics.Font objects are immutable and so it is safe to use them from multiple threads in your app.
class UIFont : NSObject
UIFont 提供了访问字体特征的权限,同时也为系统提供了字体字形信息的权限。
要使用字体对象,可以将这些字体特征和信息传递给方法(UIFont、preferredFont)作为参数即可。
创建系统字体
//获取指定尺寸的系统标准字体
open class func systemFont(ofSize fontSize: CGFloat) -> UIFont
//获取指定尺寸的系统粗体
open class func boldSystemFont(ofSize fontSize: CGFloat) -> UIFont
//获取指定尺寸的系统斜体
open class func italicSystemFont(ofSize fontSize: CGFloat) -> UIFont
//获取指定尺寸的系统标准字体, 并设置UIFont.Weight
@available(iOS 8.2, *)
open class func systemFont(ofSize fontSize: CGFloat, weight: UIFont.Weight) -> UIFont
示例代码如下:
let label = UILabel(frame: CGRect(x: 100, y: 100, width: 400, height: 20));
label.text = font.fontName;
//使用系统标准字体,大小为18
label.font = UIFont.systemFont(ofSize: 18);
view.addSubview(label);
UIFont.Weight的定义如下:
extension UIFont.Weight {
@available(iOS 8.2, *)
public static let ultraLight: UIFont.Weight
@available(iOS 8.2, *)
public static let thin: UIFont.Weight
@available(iOS 8.2, *)
public static let light: UIFont.Weight
@available(iOS 8.2, *)
public static let regular: UIFont.Weight
@available(iOS 8.2, *)
public static let medium: UIFont.Weight
@available(iOS 8.2, *)
public static let semibold: UIFont.Weight
@available(iOS 8.2, *)
public static let bold: UIFont.Weight
@available(iOS 8.2, *)
public static let heavy: UIFont.Weight
@available(iOS 8.2, *)
public static let black: UIFont.Weight
}
获取可用的字体名称
// Returns an array of font family names for all installed fonts
open class var familyNames: [String] { get }
// Returns an array of font names for the specified family name
open class func fontNames(forFamilyName familyName: String) -> [String]
例如通过以上方法可以打印系统所有字体和样式:
func getAllSystemFonts() {
// map 接收一个闭包,返回一个数组
UIFont.familyNames.map {
UIFont.fontNames(forFamilyName: $0);
}.forEach { (fonts:[String]) in
fonts.forEach({
print($0);
})
};
}
指定系统字体和大小
//设置字体,同时设置大小
label.font=UIFont(name: "Bobz Type", size: 14)
创建偏好字体
// Returns an instance of the font associated with the text style and scaled appropriately for the user's selected content size category. See UIFontDescriptor.h for the complete list.
// 根据用户设定的字体大小及粗细设置字体
@available(iOS 7.0, *)
open class func preferredFont(forTextStyle style: UIFont.TextStyle) -> UIFont
// Returns an instance of the font associated with the text style and scaled appropriately for the content size category defined in the trait collection.
// 根据系统的用户偏好设置及设备的SizeClass创建字体
@available(iOS 10.0, *)
open class func preferredFont(forTextStyle style: UIFont.TextStyle,
compatibleWith traitCollection: UITraitCollection?) -> UIFont
TextStyle的样式如下:
extension UIFont.TextStyle {
@available(iOS 11.0, *)
public static let largeTitle: UIFont.TextStyle
@available(iOS 9.0, *)
public static let title1: UIFont.TextStyle
@available(iOS 9.0, *)
public static let title2: UIFont.TextStyle
@available(iOS 9.0, *)
public static let title3: UIFont.TextStyle
@available(iOS 7.0, *)
public static let headline: UIFont.TextStyle
@available(iOS 7.0, *)
public static let subheadline: UIFont.TextStyle
@available(iOS 7.0, *)
public static let body: UIFont.TextStyle
@available(iOS 9.0, *)
public static let callout: UIFont.TextStyle
@available(iOS 7.0, *)
public static let footnote: UIFont.TextStyle
@available(iOS 7.0, *)
public static let caption1: UIFont.TextStyle
@available(iOS 7.0, *)
public static let caption2: UIFont.TextStyle
}
示例代码如下:
let label1 = UILabel(frame: CGRect(x: 10, y: 200, width: 400, height: 50));
label1.text = "preferredFont";
// 使用title1样式设置字体
// static let title1: UIFont.TextStyle
label1.font = UIFont.preferredFont(forTextStyle: .title1);
view.addSubview(label1);
使用自定义字体
iOS中可以使用自定义的字体,用法也很简单。
由于用的不多,具体不再详述。
CGFont
CGFont属于Core Graphics 核心库。主要由于绘制文本。
UIView的layer
Layer 属于UIView的属性。比如设置按钮的边框、圆角时都会使用到这个属性。
// 定义
var layer: CALayer { get }
//示例
button.layer.borderWidth = 1
button.layer.borderColor = UIColor.gray.cgColor
button.layer.cornerRadius = 10
UIView 之所以能显示在屏幕上,因为它内部有一个图层:CALayer。
当 UIView 显示在屏幕上的时候,会调用 drawRect 方法进行绘图,会将所有的内容绘制在自己的图层上,绘制完毕后,系统会将图层拷贝到屏幕上,于是 UIView 就显示了出来。
一个 UIView 只有一个 layer 属性,但是 layer 是可以叠加的,多个 layer 叠加在一起就构成了一个组合图像。
一个UIView的layer层可含有附加的layer。
- CALayer可以有阴影,由shadowColor,shadowOpacity,shadowRadius和shadowOffset属性定义。
- CALayer可以有一个边框borderWidth,borderColor。
- CALayer可通过cornerRadius来设置圆角矩形。
- CALayer可以有一个遮罩(mask)。