八.设置textFiled的光标为白色,隐藏文字的变化
描述
当textFiled选中时,隐藏文字改变亮度,光标由蓝色变为白色
“隐藏文字”解决方案
- 1.通过该属性
@property(nullable, nonatomic,copy) NSAttributedString *attributedPlaceholder
@property(nonatomic,copy) NSAttributedString *attributedPlaceholder;
// 文字属性
NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
attrs[NSForegroundColorAttributeName] = [UIColor grayColor];
// NSAttributedString : 带有属性的文字(富文本技术)
NSAttributedString *placeholder = [[NSAttributedString alloc] initWithString:@"手机号" attributes:attrs];
self.phoneField.attributedPlaceholder = placeholder;
NSMutableAttributedString *placehoder = [[NSMutableAttributedString alloc] initWithString:@"手机号"];
[placehoder setAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]} range:NSMakeRange(0, 1)];
[placehoder setAttributes:@{
NSForegroundColorAttributeName : [UIColor yellowColor],
NSFontAttributeName : [UIFont systemFontOfSize:30]
} range:NSMakeRange(1, 1)];
[placehoder setAttributes:@{NSForegroundColorAttributeName : [UIColor redColor]} range:NSMakeRange(2, 1)];
self.phoneField.attributedPlaceholder = placehoder;
- 2.通过该方法
- (void)drawPlaceholderInRect:(CGRect)rect;
- (void)drawPlaceholderInRect:(CGRect)rect
{
[self.placeholder drawInRect:CGRectMake(0, 10, rect.size.width, 25) withAttributes:@{
NSForegroundColorAttributeName : [UIColor grayColor],
NSFontAttributeName : self.font
}];
}
3.利用运行时技术,查找该控件的隐藏成员变量,方法
- 导入
#import <objc/runtime.h>
框架
+ (void)getIvars { unsigned int count = 0; // 拷贝出所有的成员变量列表 Ivar *ivars = class_copyIvarList([UITextField class], &count); for (int i = 0; i<count; i++) { // 取出成员变量 // Ivar ivar = *(ivars + i); Ivar ivar = ivars[i]; // 打印成员变量名字 XMGLog(@"%s %s", ivar_getName(ivar), ivar_getTypeEncoding(ivar)); } // 释放 free(ivars); }
- 找到
_placeholderLabel
属性,设置:
// UILabel *placeholderLabel = [self valueForKeyPath:@"_placeholderLabel"]; // placeholderLabel.textColor = [UIColor redColor];
- 导入
扩展方法
打印控件property属性
+ (void)getProperties { unsigned int count = 0; objc_property_t *properties = class_copyPropertyList([UITextField class], &count); for (int i = 0; i<count; i++) { // 取出属性 objc_property_t property = properties[i]; // 打印属性名字,类型 XMGLog(@"%s <----> %s", property_getName(property), property_getAttributes(property)); } free(properties); }
“光标颜色”解决方案
- 通过该属性
@property(null_resettable, nonatomic,strong) UIColor *tintColor
九。在textFiled内部监听光标的移动
解决方案
- 重写
becomeFirstResponder
,resignFirstResponder
.
/**
* 当前文本框聚焦时就会调用
*/
- (BOOL)becomeFirstResponder
{
// 修改占位文字颜色
[self setValue:self.textColor forKeyPath:LZGPlacerholderColorKeyPath];
return [super becomeFirstResponder];
}
/**
* 当前文本框失去焦点时就会调用
*/
- (BOOL)resignFirstResponder
{
// 修改占位文字颜色
[self setValue:[UIColor grayColor] forKeyPath:LZGPlacerholderColorKeyPath];
return [super resignFirstResponder];
}
注意:
版本号处理
+ (void)show
{
NSString *key = @"CFBundleShortVersionString";
// 获得当前软件的版本号
NSString *currentVersion = [NSBundle mainBundle].infoDictionary[key];
// 获得沙盒中存储的版本号
NSString *sanboxVersion = [[NSUserDefaults standardUserDefaults] stringForKey:key];
if (![currentVersion isEqualToString:sanboxVersion]) {
UIWindow *window = [UIApplication sharedApplication].keyWindow;
XMGPushGuideView *guideView = [XMGPushGuideView guideView];
guideView.frame = window.bounds;
[window addSubview:guideView];
// 存储版本号
[[NSUserDefaults standardUserDefaults] setObject:currentVersion forKey:key];
[[NSUserDefaults standardUserDefaults] synchronize];
}
}
十。滑动指示器的简单实现
- (void)setTitliView{
// 整体视图
UIView *titleView = [[UIView alloc]init];
self.titlesView = titleView;
titleView.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.6];
titleView.width = self.view.width;
titleView.height = 35;
titleView.y = 64;
[self.view addSubview:titleView];
// 指示器
UIView *redView = [[UIView alloc]init];
redView.backgroundColor = [UIColor redColor];
redView.height = 2;
redView.y = titleView.height - redView.height;
[titleView addSubview:redView];
self.buttonView = redView;
// 内部控件
NSArray *titles = @[@"全部",@"视频",@"声音",@"图片",@"段子"];
NSInteger count = 5;
CGFloat width = titleView.width / count;
CGFloat height = titleView.height;
for (NSInteger i = 0; i<count; i ++) {
UIButton *button = [[UIButton alloc]init];
button.height = height;
button.width = width;
button.x = i * width;
[button setTitle:titles[i] forState:UIControlStateNormal];
[button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor redColor] forState:UIControlStateDisabled];
button.titleLabel.font = [UIFont systemFontOfSize:14];
[button addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchUpInside];
[titleView addSubview:button];
/**
* 默认点击了第一个按钮
*/
if (i == 0) {
button.enabled = NO;
self.btn = button;
// 让按钮内部的label根据文字内容来计算尺寸
[button.titleLabel sizeToFit];
self.buttonView.width = button.titleLabel.width;
// self.indicatorView.width = [titles[i] sizeWithAttributes:@{NSFontAttributeName:button.titleLabel.font}].width;
self.buttonView.centerX = button.centerX;
}
}
}
- (void)titleClick:(UIButton *)btn{
/**
* 修改按钮状态
*/
self.btn.enabled = YES;
btn.enabled = NO;
self.btn = btn;
[UIView animateWithDuration:0.25 animations:^{
self.buttonView.width = btn.titleLabel.width;
self.buttonView.centerX = btn.centerX;
}];
}
十一.设置滚动条的可滚动范围
vc.tableView.scrollIndicatorInsets = vc.tableView.contentInset;
十二.计算时间的差值,确定时间之间的差值
方案一
- (void)testDate:(NSString *)create_time
{
// 当前时间
NSDate *now = [NSDate date];
// 发帖时间
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
// 设置日期格式(y:年,M:月,d:日,H:时,m:分,s:秒)
fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
NSDate *create = [fmt dateFromString:create_time];
NSTimeInterval delta = [now timeIntervalSinceDate:create];
}
方案二
// 日期格式化类
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
// 设置日期格式(y:年,M:月,d:日,H:时,m:分,s:秒)
fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
// 当前时间
NSDate *now = [NSDate date];
// 发帖时间
NSDate *create = [fmt dateFromString:create_time];
// 日历
NSCalendar *calendar = [NSCalendar currentCalendar];
// 比较时间
NSCalendarUnit unit = NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
NSDateComponents *cmps = [calendar components:unit fromDate:create toDate:now options:0];
XMGLog(@"%@ %@", create, now);
XMGLog(@"%zd %zd %zd %zd %zd %zd", cmps.year, cmps.month, cmps.day, cmps.hour, cmps.minute, cmps.second);
具体比较方法(需要用到一个封装的方法)
- (NSString *)create_time
{
// 日期格式化类
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
// 设置日期格式(y:年,M:月,d:日,H:时,m:分,s:秒)
fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
// 帖子的创建时间
NSDate *create = [fmt dateFromString:_create_time];
if (create.isThisYear) { // 今年
if (create.isToday) { // 今天
NSDateComponents *cmps = [[NSDate date] deltaFrom:create];
if (cmps.hour >= 1) { // 时间差距 >= 1小时
return [NSString stringWithFormat:@"%zd小时前", cmps.hour];
} else if (cmps.minute >= 1) { // 1小时 > 时间差距 >= 1分钟
return [NSString stringWithFormat:@"%zd分钟前", cmps.minute];
} else { // 1分钟 > 时间差距
return @"刚刚";
}
} else if (create.isYesterday) { // 昨天
fmt.dateFormat = @"昨天 HH:mm:ss";
return [fmt stringFromDate:create];
} else { // 其他
fmt.dateFormat = @"MM-dd HH:mm:ss";
return [fmt stringFromDate:create];
}
} else { // 非今年
return _create_time;
}
}
封装的方法
#import <Foundation/Foundation.h>
@interface NSDate (XMGExtension)
/**
* 比较from和self的时间差值
*/
- (NSDateComponents *)deltaFrom:(NSDate *)from;
/**
* 是否为今年
*/
- (BOOL)isThisYear;
/**
* 是否为今天
*/
- (BOOL)isToday;
/**
* 是否为昨天
*/
- (BOOL)isYesterday;
@end
--------------------------------------------------------------
#import "NSDate+XMGExtension.h"
@implementation NSDate (XMGExtension)
- (NSDateComponents *)deltaFrom:(NSDate *)from
{
// 日历
NSCalendar *calendar = [NSCalendar currentCalendar];
// 比较时间
NSCalendarUnit unit = NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
return [calendar components:unit fromDate:from toDate:self options:0];
}
- (BOOL)isThisYear
{
// 日历
NSCalendar *calendar = [NSCalendar currentCalendar];
NSInteger nowYear = [calendar component:NSCalendarUnitYear fromDate:[NSDate date]];
NSInteger selfYear = [calendar component:NSCalendarUnitYear fromDate:self];
return nowYear == selfYear;
}
//- (BOOL)isToday
//{
// // 日历
// NSCalendar *calendar = [NSCalendar currentCalendar];
//
// NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
//
// NSDateComponents *nowCmps = [calendar components:unit fromDate:[NSDate date]];
// NSDateComponents *selfCmps = [calendar components:unit fromDate:self];
//
// return nowCmps.year == selfCmps.year
// && nowCmps.month == selfCmps.month
// && nowCmps.day == selfCmps.day;
//}
- (BOOL)isToday
{
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
fmt.dateFormat = @"yyyy-MM-dd";
NSString *nowString = [fmt stringFromDate:[NSDate date]];
NSString *selfString = [fmt stringFromDate:self];
return [nowString isEqualToString:selfString];
}
- (BOOL)isYesterday
{
// 2014-12-31 23:59:59 -> 2014-12-31
// 2015-01-01 00:00:01 -> 2015-01-01
// 日期格式化类
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
fmt.dateFormat = @"yyyy-MM-dd";
NSDate *nowDate = [fmt dateFromString:[fmt stringFromDate:[NSDate date]]];
NSDate *selfDate = [fmt dateFromString:[fmt stringFromDate:self]];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *cmps = [calendar components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:selfDate toDate:nowDate options:0];
return cmps.year == 0
&& cmps.month == 0
&& cmps.day == 1;
}
@end