CGRect, CGSize, CGPoint C 语法 —— 披着羊皮的狼 我相信下面这种写法,很多 Swift 开发者都会中枪,让我看见你们的双手! 所以,让我们来看看正确的 Swift 姿势吧: let rect = CGRect(x: 0, y: 0, width: 100, height: 100) let size = CGSize(width let rect = CGRect.zero let size = CGSize.zero let point = CGPoint.zero 这样写代码也更为清晰,Xcode 会将 . 中某一个变量的值,并且你也可以直接对 frame 包含的 origin 与 size 结构体重新赋值: let view = UIView(frame: .zero) view.frame.size = CGSize
param string 字符串 /// @param lineSpacing 行数 /// @param fontSize 字符串字号 /// @param rectSize 给一个固定的宽或者高 + (CGSize lineSpacing:(CGFloat)lineSpacing fontSize:(CGFloat)fontSize rectSize:(CGSize )rectSize { UIFont *font = [UIFont systemFontOfSize: fontSize]; CGSize size = [self @param string 字符串 /// @param lineSpacing 行数 /// @param font 字符串字号 /// @param rectSize 给一个固定的宽或者高 + (CGSize @param string 字符串 /// @param lineSpacing 行数 /// @param font 字符串字号 /// @param rectSize 给一个固定的宽或者高 + (CGSize
cellid"]; ; [self.view addSubview:collect]; } //设置每个item的大小,双数的为50*50 单数的为100*100 -(CGSize 设置列与列之间的间距最小距离 @property (nonatomic) CGFloat minimumInteritemSpacing; 设置每个item的大小 @property (nonatomic) CGSize itemSize; 设置每个Item的估计大小,一般不需要设置 @property (nonatomic) CGSize estimatedItemSize NS_AVAILABLE_IOS(8_0) headerReferenceSize; 设置尾视图尺寸大小 @property (nonatomic) CGSize footerReferenceSize; 设置分区的EdgeInset @property )collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section; 动态设置某个分区头视图大小 - (CGSize
一、几个常用结构体 struct CGPoint { CGFloat x; CGFloat y; }; 定义一个点,设置x坐标和y坐标 struct CGSize { CGFloat width; 定义一个尺寸,设置宽度和高度 struct CGVector { CGFloat dx; CGFloat dy; }; 定义一个二维矢量 struct CGRect { CGPoint origin; CGSize size; }; 定义一个矩形 二、几个系统定义的量 const CGPoint CGPointZero 零点,与CGPointMake(0, 0)等效 const CGSize CGSizeZero size1, CGSize size2); 判断两个尺寸是否相等 bool CGRectEqualToRect(CGRect rect1, CGRect rect2); 判断两个矩形是否相等 CGRect size); 返回一个表示尺寸的字典 bool CGSizeMakeWithDictionaryRepresentation(CFDictionaryRef dict, CGSize *size)
没有注册这个 设置区头高度 -(CGSize)collectionView:(UICollectionView )collectionView layout:(UICollectionViewLayout section { /** 上左下右*/ UIEdgeInsets top = {5*NEWX,5*NEWX,5*NEWX,5*NEWX}; return top; } 设置顶部的大小 -(CGSize UICollectionViewLayout *)**collectionViewLayout referenceSizeForHeaderInSection**:(NSInteger)section{ CGSize size={5*NEWX,5*NEWX}; return size; } 设置元素大小 -(CGSize)collectionView:(UICollectionView *)collectionView size = CGSizeMake(0, 30); return size; } 设置footer高度 - (CGSize)collectionView:(UICollectionView *
@property (nonatomic) CGSize itemSize; @property (nonatomic) CGSize estimatedItemSize // 滚动方向,默认是水平 // 垂直滚动时section间宽度为该尺寸的高,而水平滚动时为宽度起作用, @property (nonatomic) CGSize headerReferenceSize; @property (nonatomic 代理方法 // 设定指定Cell的尺寸 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout property (nonatomic) CGRect frame // 中心点 @property (nonatomic) CGPoint center // 大小 @property (nonatomic) CGSize 之后,-(CGSize) collectionViewContentSize将被调用, 以确定collection应该占据的尺寸。注意这里的尺寸不是指可视部分的尺寸,而应该是所有内容所占的尺寸。
https://bujige.net/blog/iOS-Foundation-other.html 本文对Foundation框架中一些数字类(NSNumber)、常用结构体类(CGPoint、CGSize 结构体类(CGPoint、CGSize、CGRect、CGRange和NSValue) 1. NSSize和CGSize CGSize和NSSize是同义的 CGSize代表的是二维平面中的某个物体的尺寸(宽度和高度) CGSize有2个成员 CGFloat width:表示该矩形尺寸的宽 CGFloat height:表示该矩形尺寸的高 typedef CGSize NSSize; // CGSize的定义 struct CGSize { CGFloat width; CGFloat height ; }; typedef struct CGSize CGSize; 可以使用CGSizeMake和NSMakeSize函数创建CGSize CGSize size = CGSizeMake(20.0
collectionViewContentSize,将contentSize取出来修改为我们自己创建的newSize就可以了代码如下 override var collectionViewContentSize: CGSize { let size: CGSize = super.collectionViewContentSize let collectionViewWidth: CGFloat = self.collectionView frame.size.width let nbOfScreen: Int = Int(ceil(size.width / collectionViewWidth)) let newSize: CGSize = CGSize(width: collectionViewWidth * CGFloat(nbOfScreen), height: size.height) return newSize }
setMutableTitleWithString:(NSString *)text textFont:(UIFont *)textFont forState:(UIControlState)controlState { CGSize self.frame = self_Rect; } /** * 字符串获取属性 * @param text * @param font * * @return size */ - (CGSize font{ CGRect screen = [UIScreen mainScreen].bounds; CGFloat maxWidth = screen.size.width; CGSize maxSize = CGSizeMake(maxWidth, CGFLOAT_MAX); CGSize textSize = CGSizeZero; // iOS7以后使用boundingRectWithSize
此外,还有NSNumber、NSValue、NSData等封装类型,有NSDictionary、NSArray、NSSet等集合数据类型,有CGRect/NSRect、CGPoint/NSPoint、CGSize NSValue主要用来封装自定义的数据结构,可以是系统框架提供的CGRect/CGPoint/CGSize等数据结构,也可以是自己定义的struct。 1.2.6 尺寸、范围相关的类型 Object-C中有CGRect/NSRect、CGPoint/NSPoint、CGSize/NSSize等尺寸相关的,其实CG开头的和NS开头的都是一个东西,都是struct { CGFloat width; CGFloat height; }; typedef struct CG_BOXABLE CGSize CGSize; //NSSize就是CGSize typedef CGSize NSSize; /* Rectangles. */ struct CGRect { CGPoint origin; CGSize size; }; typedef
UIImageView 内 设置 contentMode 为 center 居中便可有 padding 效果 func imageWithImage(image: UIImage, scaledToSize: CGSize , scaledToSize: CGSize(width: 18, height: 18)) xxxUIImageView.contentMode = .center 2.3 对 UIImageView padding") return } let originSize = self.frame.size var scaledToSize: CGSize image.size.height { let radio = image.size.height / image.size.width scaledToSize = CGSize } else { let radio = image.size.width / image.size.height scaledToSize = CGSize
/// @return 返回自适应的最合适尺寸 CGSize calcTextSize(CGSize fitsSize, id text, NSInteger numberOfLines, UIFont *font, NSTextAlignment textAlignment, NSLineBreakMode lineBreakMode, CGFloat minimumScaleFactor, CGSize calcTextSizeV2(CGSize fitsSize, id text, NSInteger numberOfLines, UIFont *font) { return calcTextSize testStringArray.count]]; } if (text.length == 0) continue; CGSize label.attributedText = attributedText; start = CACurrentMediaTime(); CGSize
layoutAttributesForSupplementaryViewOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath; //返回内容高度 -(CGSize widthForItemAtIndexPath:(NSIndexPath *)indexPath itemHeight:(CGFloat)itemHeight; /** 竖向瀑布流 item等高不等宽 列数、行数无用 */ - (CGSize WSLWaterFlowLayout *)waterFlowLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath; /** 头视图Size */ -(CGSize WSLWaterFlowLayout *)waterFlowLayout sizeForHeaderViewInSection:(NSInteger)section; /** 脚视图Size */ -(CGSize
这时候就要用到改进后的截图方案: /** 针对有用过OpenGL渲染过的视图截图 @return 截取的图片 */ - (UIImage *)openglSnapshotImage { CGSize UIWebView的category,也算是对UIWebView 的截图,顺便也贴出来吧 - (UIImage *)imageForWebView { // 1.获取WebView的宽高 CGSize boundsWidth = boundsSize.width; CGFloat boundsHeight = boundsSize.height; // 2.获取contentSize CGSize [self.scrollView setContentOffset:offset]; CGFloat scale = [UIScreen mainScreen].scale; CGSize
为了符合GeometryEffect协议,你需要实现以下方法: func effectValue(size: CGSize) -> ProjectionTransform 假设你的方法叫SkewEffect (width: x, height: y))) } func offset(_ offset: CGSize) -> some View { return modifier (_OffsetEffect(offset: offset)) } } struct _OffsetEffect: GeometryEffect { var offset: CGSize var animatableData: CGSize.AnimatableData { get { CGSize.AnimatableData(offset.width , offset.height) } set { offset = CGSize(width: newValue.first, height: newValue.second) }
文字的字体属性 * @param context nil * * @return 返回的是 CGRect 类型,frame */ - (CGRect)boundingRectWithSize:(CGSize ; CGSize textMaxWH = CGSizeMake(textW, ?) ; CGSize textMaxWH = CGSizeMake(textW, ?)
imageFromText(_ bgColor:UIColor,str:String,imageWidth:CGFloat)->UIImage{ let size = CGSize imageWidth); } ///指定大小缩放 static func imageZoomBySize(_ sourceImage:UIImage,newSize:CGSize return sourceImage; }else{ return imageZoomBySize(sourceImage, newSize: CGSize return sourceImage; }else{ return imageZoomBySize(sourceImage, newSize: CGSize targetWidth = width / maxRatio; return imageZoomBySize(sourceImage, newSize: CGSize
layoutAttributesForSupplementaryViewOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath; //返回内容高度 -(CGSize widthForItemAtIndexPath:(NSIndexPath *)indexPath itemHeight:(CGFloat)itemHeight; /** 竖向瀑布流 item等高不等宽 列数、行数无用 */ - (CGSize WSLWaterFlowLayout *)waterFlowLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath; /** 头视图Size */ -(CGSize WSLWaterFlowLayout *)waterFlowLayout sizeForHeaderViewInSection:(NSInteger)section; /** 脚视图Size */ -(CGSize
URLWithString:imageURL]]; UIImage* image = [[UIImage alloc] initWithData:imageData]; // resize image CGSize newSize = CGSizeMake(100, 100); UIGraphicsBeginImageContext( newSize );// a CGSize that has the size
iOS 首先左上角为坐标原点(0,0) CGPoint创建坐标点也就是位置 CGSize表示视图宽度和高度 CGRect结合了CGPoint和CGSize origin表示左上角所在的CGPoint