title: 有用的UI组件 date: 2015-09-22 22:36:00 categories:
- 实用技术
tags: - 实用
- UI
Xcode 快捷键 倒计时闪烁问题 可视化编程中----cell的自适应高度 iOS 关闭图片渲染 setValue 和setObject 的区别 IOS如何让实现国际化的相关配置 UITextView的点击链接 collecton的长按编辑的代码实现 NSAttributedString.h 中属性的定义,key 使用openURL实现程序间带参数跳转详解
1.子类父类class/superclass判断
- 子类调用子类自己的方法,判断如下
- 子类调用父类的方法,结果如下
2.倒计时闪烁问题
- UIButton+countDown.h,点击获取验证码后进行倒计时,效果挺好,但会闪烁,
- 解决办法:将UIButton的类型由system改为custom就OK。
3.Xcode 快捷键
- 快捷键
1、command 1/2/3/4/5 左侧的不同栏目 2、commad + e 搜索当前词 在本文件中搜索 3、command +g 结合上面,选中搜索,然后下一个 4、 Command + [ 左缩进 Command + ] 右缩进 5、 control +6 搜索本文件的方法显示出方法列表 6、command option + j 跳到目录搜索 7、command option + [ 当前行上下移动 8、command option + Left/Right 折叠、展开当前代码段 9、command control + Up/Down .h 和.m 之间的切换 10、command control + Left/Right浏览历史的前进后退 11、command shift o 文件、方法全局搜索 12、command shift +f 全局搜索 13、 command shift +J 左侧栏显示当前所在文件 复制代码
4.Xcode描述文件问题
- No matching provisioning profile found: Your build settings specify a provis
- 解决办法~/Library/MobileDevice/Provisioning Profiles 全部删除,之后重新安装描述文件即可
5.可视化编程中----cell的自适应高度
- 可视化编程中----cell的自适应高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { Joke *joke = self.dataArray[indexPath.row]; NSString *content = joke.joke; self.customCell.contentLabel.text = content; self.customCell.bounds = CGRectMake(0, 0, self.view.frame.size.width, self.customCell.frame.size.height); [self.customCell updateConstraintsIfNeeded]; [self.customCell layoutIfNeeded]; CGFloat height = [self.customCell systemLayoutSizeFittingSize: UILayoutFittingCompressedSize].height + 1; return height; } 复制代码
6.iOS 关闭图片渲染
-
在为Button 设置背景图片的时候, 会发现显示的效果和UI给的图片不一样, 往往是把图片显示成为蓝色, 这是因为在新版的iOS中, 会自动对图片渲染. 我们只要把图片渲染关掉就OK了
- (UIImage *)imageWithRenderingMode:(UIImageRenderingMode)renderingMode 这个方法就是用来设置图片的渲染模式的 UIImageRenderingModeAlwaysOriginal这个枚举值是声明这张图片要按照原来的样子显示,不需要渲染成其他颜色 UIImage *playImage = [UIImage imageNamed:@"luzhi"]; playImage = [playImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; [_playBtn setImage:playImage forState:UIControlStateNormal]; 复制代码
7.setValue 和setObject 的区别
- setObject:forkey:中value是不能够为nil的,不然会报错。
- setValue:forKey:中value能够为nil,但是当value为nil的时候,会自动调用removeObject:forKey方法
- setValue:forKey:中key的参数只能够是NSString类型,而setObject:forKey:的可以是任何类型
8.IOS如何让实现国际化的相关配置
- iOS中国际化需要做相关的配置:
- 选中应用程序对应的project,然后添加所需要国际化的语言。
- 新建Localizable.strings文件,作为多语言对应的词典,存储多种语言,点击右侧Localization,勾选国际化对应的语言。
- 添加一个字段,设置你想要国际化的字段
- 在English中,添加:SUBMIT_BTN_TITLE = Go;
- 在Chinese中,添加:SUBMIT_BTN_TITLE = 开始;
9.UITextView的点击链接
-
UITextView的链接创建一个NSAttributedString然后增加给它增加一个NSLinkAttributeName 属性,见以下:
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"This is an example by @marcelofabri_"]; [attributedString addAttribute:NSLinkAttributeName value:@"username://marcelofabri_" range:[[attributedString string] rangeOfString:@"@marcelofabri_"]]; NSDictionary *linkAttributes = @{NSForegroundColorAttributeName: [UIColor greenColor], NSUnderlineColorAttributeName: [UIColor lightGrayColor], NSUnderlineStyleAttributeName: @(NSUnderlinePatternSolid) }; textView.linkTextAttributes = linkAttributes; // customizes the appearance of links textView.attributedText = attributedString; textView.delegate = self; 复制代码
-
这样就可以让链接在文本中显示。然而,你也可以控制当链接被点击的时候会发生什么,实现这个可以使用UITextViewDelegate协议的新的shouldInteractWithURL方法,就像这样:
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange { if ([[URL scheme] isEqualToString:@"username"]) { NSString *username = [URL host]; // do something with this username return NO; } return YES; // let the system open this URL } 复制代码
10.table下面滑不下去,是table的高度超出了屏幕宽度
- 高度超过了屏幕宽度,高度设置的不对
[weakSelf.currentVC.view setFrame:CGRectMake(0, 100, ScreenW, ScreenH-64)];
self.edgesForExtendedLayout = UIRectEdgeNone; //也是个方法,是上方多处一块64时
复制代码
11.气泡图片中间拉伸
-
把一张小的图片做背景保持四周不变,拉伸中间,则用如下属性。UIEdgeInsets是拉伸的区域,一般取最中间的一个点
UIImage *meBgNor=[UIImage imageNamed:@"chat_send_nor"]; UIEdgeInsets edge1=UIEdgeInsetsMake(28, 32, 28, 32); meBgNor=[meBgNor resizableImageWithCapInsets:edge1 resizingMode:UIImageResizingModeStretch]; [self.textView setBackgroundImage:meBgNor forState:UIControlStateNormal]; 复制代码
-
背景气泡图
UIImage *normal; if (message.from == UUMessageFromMe) { normal = [UIImage imageNamed:@"chatto_bg_normal"]; normal = [normal resizableImageWithCapInsets:UIEdgeInsetsMake(35, 10, 10, 22)]; } else{ normal = [UIImage imageNamed:@"chatfrom_bg_normal"]; normal = [normal resizableImageWithCapInsets:UIEdgeInsetsMake(35, 22, 10, 10)]; } [self.btnContent setBackgroundImage:normal forState:UIControlStateNormal]; [self.btnContent setBackgroundImage:normal forState:UIControlStateHighlighted]; 复制代码
12.collecton的长按编辑的代码实现
-
(1) 对collectionView的处理
- (void)handlelongGesture:(UILongPressGestureRecognizer *)longGesture { //判断手势状态 switch (longGesture.state) { case UIGestureRecognizerStateBegan: { //判断手势落点位置是否在路径上 NSIndexPath *indexPath = [self.theCollectionView indexPathForItemAtPoint:[longGesture locationInView:self.theCollectionView]]; if (indexPath == nil) { break; } //在路径上则开始移动该路径上的cell [self.theCollectionView beginInteractiveMovementForItemAtIndexPath:indexPath]; } break; case UIGestureRecognizerStateChanged: //移动过程当中随时更新cell位置 [self.theCollectionView updateInteractiveMovementTargetPosition: [longGesture locationInView:self.theCollectionView]]; break; case UIGestureRecognizerStateEnded: //移动结束后关闭cell移动 [self.theCollectionView endInteractiveMovement]; break; default: [self.theCollectionView cancelInteractiveMovement]; break; } } 复制代码
-
(2) 代理方法能否编辑返回yes,
- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath { return YES; } 复制代码
-
(3) 对数据源数据的处理:
- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath { //取出源item数据 id objc1 = [_titleMuarray objectAtIndex:sourceIndexPath.row]; id objc2 = [_imageNameMuarray objectAtIndex:sourceIndexPath.row]; //从资源数组中移除该数据 [_titleMuarray removeObject:objc1]; [_imageNameMuarray removeObject:objc2]; //将数据插入到资源数组中的目标位置上 [_titleMuarray insertObject:objc1 atIndex:destinationIndexPath.row]; [_imageNameMuarray insertObject:objc2 atIndex:destinationIndexPath.row]; WorkStore *workStoreInfomation1 = [WorkStore MR_findFirst]; workStoreInfomation1.sortOrder = _titleMuarray; workStoreInfomation1.imageName = _imageNameMuarray; [[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait]; } 复制代码
13.项目中使用多个storyBoard之间的跳转
-
使用代码进行2个storyboard之间的跳转: 另外创建的storyboard的名字自己定义,之后在将要跳转的controller中取出storyboard,然后再取出其中需要的controller,进行跳转
UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"Storyboard2" bundle:nil]; UIViewController *test2obj=[secondStroyBoard instantiateViewControllerWithIdentifier:@"test2"]; [self.navigationController pushViewController:test2obj animated:NO]; 复制代码
-
如何从多个storyboard中取出控制器实例。不用管控制在哪个storyboard文件里,只要 控制器设置identifier为类名就OK。
14.NSAttributedString.h 中属性的定义,key
-
如何使用NSString在 drawInRect中居中
-
drewRect属性, withAttributes:参数是属性字典,该字典的key在NSAttributedString.h中获得,
NSMutableParagraphStyle style = [[NSMutableParagraphStyle alloc] init]; [style setAlignment:UITextAlignmentCenter]; style.lineBreakMode =NSLineBreakByCharWrapping; NSDictionary attr = @{ NSParagraphStyleAttributeName:style, NSFontAttributeName: [UIFont systemFontOfSize:10], NSForegroundColorAttributeName:[UIColor colorWithHexString:@"333333"] }; 复制代码
-
NSAttributedString.h 中属性的定义,key
字符属性 字符属性可以应用于 attributed string 的文本中。 NSString *const NSFontAttributeName;(字体) NSString *const NSParagraphStyleAttributeName;(段落) NSString *const NSForegroundColorAttributeName;(字体颜色) NSString *const NSBackgroundColorAttributeName;(字体背景色) NSString *const NSLigatureAttributeName;(连字符) NSString *const NSKernAttributeName;(字间距) NSString *const NSStrikethroughStyleAttributeName;(删除线) NSString *const NSUnderlineStyleAttributeName;(下划线) NSString *const NSStrokeColorAttributeName;(边线颜色) NSString *const NSStrokeWidthAttributeName;(边线宽度) NSString *const NSShadowAttributeName;(阴影)(横竖排版) NSString *const NSVerticalGlyphFormAttributeName; 复制代码
-
详细解释
常量 1> NSFontAttributeName(字体) 该属性所对应的值是一个 UIFont 对象。该属性用于改变一段文本的字体。如果不指定该属性,则默认为12-point Helvetica(Neue)。 2> NSParagraphStyleAttributeName(段落) 该属性所对应的值是一个 NSParagraphStyle 对象。该属性在一段文本上应用多个属性。如果不指定该属性,则默认为 NSParagraphStyle 的defaultParagraphStyle 方法返回的默认段落属性。 3> NSForegroundColorAttributeName(字体颜色) 该属性所对应的值是一个 UIColor 对象。该属性用于指定一段文本的字体颜色。如果不指定该属性,则默认为黑色。 4> NSBackgroundColorAttributeName(字体背景色) 该属性所对应的值是一个 UIColor 对象。该属性用于指定一段文本的背景颜色。如果不指定该属性,则默认无背景色。 5> NSLigatureAttributeName(连字符) 该属性所对应的值是一个 NSNumber 对象(整数)。连体字符是指某些连在一起的字符,它们采用单个的图元符号。0 表示没有连体字符。1 表示使用默认的连体字符。2表示使用所有连体符号。默认值为 1(注意,iOS 不支持值为 2)。 6> NSKernAttributeName(字间距) 该属性所对应的值是一个 NSNumber 对象(整数)。字母紧排指定了用于调整字距的像素点数。字母紧排的效果依赖于字体。值为 0 表示不使用字母紧排。默认值为0。 7> NSStrikethroughStyleAttributeName(删除线) 该属性所对应的值是一个 NSNumber 对象(整数)。该值指定是否在文字上加上删除线,该值参考“Underline Style Attributes”。默认值是NSUnderlineStyleNone。 8> NSUnderlineStyleAttributeName(下划线) 该属性所对应的值是一个 NSNumber 对象(整数)。该值指定是否在文字上加上下划线,该值参考“Underline Style Attributes”。默认值是NSUnderlineStyleNone。 9> NSStrokeColorAttributeName(边线颜色) 该属性所对应的值是一个 UIColor 对象。如果该属性不指定(默认),则等同于 NSForegroundColorAttributeName。否则,指定为删除线或下划线颜色。更多细节见“Drawing attributedstrings that are both filled and stroked”。 10> NSStrokeWidthAttributeName(边线宽度) 该属性所对应的值是一个 NSNumber 对象(小数)。该值改变描边宽度(相对于字体size 的百分比)。默认为 0,即不改变。正数只改变描边宽度。负数同时改变文字的描边和填充宽度。例如,对于常见的空心字,这个值通常为3.0。 11> NSShadowAttributeName(阴影) 该属性所对应的值是一个 NSShadow 对象。默认为 nil。 12> NSVerticalGlyphFormAttributeName(横竖排版) 该属性所对应的值是一个 NSNumber 对象(整数)。0 表示横排文本。1 表示竖排文本。在 iOS 中,总是使用横排文本,0 以外的值都未定义。 复制代码
15.清除所有的NSUserDefault
-
清除所有的NSUserDefault
- (void)clearAllUserDefaultsData{ NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier]; [[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain]; } 复制代码
16.使用openURL实现程序间带参数跳转详解
-
(1) target -> info -> url scheme 添加URL
-
(2) 正常跳转
NSURL *url = [NSURL URLWithString:@"B://com.YouXianMing"]; if ([[UIApplication sharedApplication] canOpenURL:url]){ NSLog(@"跳转并打开"); [[UIApplication sharedApplication] openURL:url]; }else{ NSLog(@"打开失败"); } 复制代码
-
(3) 带参数跳转
// 其他应用的 URL Schemes --> B // 其他应用的 Identifier --> com.xinguan // values? --> 要传递的参数,方便解析 NSURL *url = [NSURL URLWithString:\ @"B://com.xinguan/values?username=WT&password=123456&callback=invoking"]; if ([[UIApplication sharedApplication] canOpenURL:url]){ NSLog(@"跳转并打开"); [[UIApplication sharedApplication] openURL:url]; }else{ NSLog(@"打开失败"); } 复制代码
-
(4) 接收到参数解析
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{ if ([[url scheme] isEqualToString:@"B"]){ if ([[url host] isEqualToString:@"com.xinguan"]){ /* query用法 The query string from the URL. If the receiver does not conform to RFC 1808, returns nil. For example, in the URL http://www.example.com/index.php?key1=value1&key2=value2, the query string is key1=value1&key2=value2. */ NSString *query = [url query]; // 分割& NSArray *array = [query componentsSeparatedByString:@"&"]; // 显示数据 NSLog(@"%@", array); } return YES; } return NO; } 复制代码
17.在view上的window问题
-
在view上的window问题:
- 当在一个XIB上添加一个window,拖线出来,默认是显示的,就是不写任何代码,也是在当前窗口上覆盖,影响交互,
- 先设置hidden,后在
makeKeyAndVisible
-
当取消key window的时候,注意hidden调