开发随笔-01

准确判断是否有内容

// 字符串
if (string.length) {
}
// 错误写法:
if (string) {
}
// 数组
if (array.count) {
}
// 错误写法:
if (array) {
}

添加视图必须先设置尺寸再设置center
设置size再设置center

获取控件的size和数据方法

[UIImage imageNamed:@"MainTagSubIcon"].size;
[button imageForState:UIControlStateNormal].size;
button.currentImage.size;
[button backgroundImageForState:UIControlStateNormal];
button.currentBackgroundImage;
[button titleForState:UIControlStateNormal];
button.currentTitle;
[button titleForState:UIControlStateNormal];
button.currentTitleColor;

Appearance的使用场合

- 只要后面带有 <UI_APPEARANCE_SELECTOR> 的方法或者属性, 都可以通过appearance对象统一设置
@interface UISwitch : UIControl
@property(nullable, nonatomic, strong) UIColor *onTintColor NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
@end
UISwitch *s = [UISwitch appearance];
s.onTintColor = [UIColor redColor];

frame和bounds的认识

  • frame
    • 以<父控件> '内容' 的左上角为坐标原点, 计算出的 '控件自己' '矩形框'的位置和尺寸
  • bounds
    • 以<控件自己> '内容' 的左上角为坐标原点, 计算出的 '控件自己' '矩形框'的位置和尺寸
  • 概括
frame.size = bounds.size;
frame.origin != bounds.origin;
scrollView.bounds.origin = scrollView.contentOffset;

contentInset的调整

  • 默认情况下, 如果一个控制器处于导航控制器管理中, 并且控制器A的第一个子控件是UIScrollView, 那么就会自动调整这个UIScrollView的contentInset
UIEdgeInsetsMake(64, 0, 0, 0) // 有导航栏
UIEdgeInsetsMake(20, 0, 0, 0) // 没有导航栏
  • 默认情况下, 如果一个控制器处于导航控制器管理中,并且导航控制器又处于UITabBarController管理中, 并且控制器A的第一个子控件是UIScrollView, 那么就会自动调整这个UIScrollView的contentInset
UIEdgeInsetsMake(64, 0, 49, 0)
  • 如何禁止上述的默认问题
控制器A.automaticallyAdjustsScrollViewInsets = NO;

文字内容换行

  • 如何让storyboard/xib中的文字内容换行
    • 快捷键: option + 回车键
    • 在storyboard/xib中 输入\n是无法实现换行的
    • 在代码中输入\n是可以实现换行的
      self.label.text = @"快圣诞节会否按回复\n卡罗积分哈佛\n卡手回复";

UITextField站位文字的设置

// 设置站位文字内容
@property (nullable, nonatomic, copy) NSString *placeholder;
// 设置带有属性的占位位置,  优先级 > placeholder
@property (nullable, nonatomic, copy) NSAttributedString *attributedPlaceholder;

NSAttributedString

  • 带有属性的字符串, 富文本
  • 由2部分组成
    • 文字内容 : NSString *
    • 文字属性 : NSDictionary *
      • 文字颜色 : NSForegroundColorAttributeName
      • 字体大小 : NSFontAttributeName
      • 下划线 : NSUnderlineStyleAttributeName
      • 文字颜色 : NSBackgroundColorAttributeName
NSMutableDictionary*attributes = [NSMutableDictionary dictionary];
attributes[NSForegroundColorAttributeName] = [UIColor whiteColor];
attributes[NSBackgroundColorAttributeName] = [UIColor whiteColor];
attributes[NSUnderlineStyleAttributeName] = @YES;
self.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"123" attributes:attributes];

NSMutableAttributeString

  • 继承自NSAttributeString
  • 常见方法
// 设置range范围的属性, 重复设置包含前一个范围的属性, 最后一次设置才有效 (之前的设置会被覆盖掉)
- (void)setAttributes:(nullableNSDictionary *)attrs range:(NSRange)range;
// 添加range范围的属性, 同一个范围, 可以不断累加属性
- (void)addAttribute:(NSString*)name value:(id)value range:(NSRange)range;
- (void)addAttributes:(NSDictionary *)<NSString*,id> *)attrs range:(NSRange)range;

在storyboard\xib种给UIScrollView子控件添加约束

  • 上下滚动(垂直滚动)
    • 给添加一个UIView类型的子控件A(这将是UIScrollView唯一的一个子控件)
    • 设置A距离UIScrollView上下左右间距都为0
    • 设置A的高度(这个高度就是UIScrollView的内容高度: contentSize.height)
    • 设置A在UIScrollView中左右居中(水平居中)
    • 往A中再添加其他子控件
  • 左右滚动(水平滚动)
    • 给添加一个UIView类型的子控件A(这将是UIScrollView唯一的一个子控件)
    • 设置A距离UIScrollView上下左右间距都为0
    • 设置A的宽度(这个宽度就是UIScrollView的内容宽度: contentSize.width)
    • 设置A在UIScrollView中上下居中(垂直居中)
    • 往A中再添加其他子控件
  • 上下左右滚动(水平垂直滚动)
    • 给添加一个UIView类型的子控件A(这将是UIScrollView唯一的一个子控件)
    • 设置A距离UIScrollView上下左右间距都为0
    • 设置A的宽度(这个宽度就是UIScrollView的内容宽度: contentSize.width)
    • 设置A的高度(这个高度就是UIScrollView的内容高度: contentSize.height)

修改UITextField占位文字的颜色

  • 使用attributedPlaceholder
@property(nullable, nonatomic,copy)  NSAttributedString  *attributedPlaceholder
  • 重写 - (void)drawPlaceholderInRect:(CGRect)rect;
- (void)drawPlaceholderInRect:(CGRect)rect;
  • 修改内部占位文字Label的文字颜色
[self setValue:[UIColor whiteColor] forKeyPath:@"placeholderLabel.textColor"];
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容