美文网首页iOS点点滴滴
iOS开发笔记-20170309

iOS开发笔记-20170309

作者: 一年后的你 | 来源:发表于2017-03-09 22:10 被阅读7次

计算字符串 size

在 iOS7 中下面计算字符串 size的方法都标注为 deprecated

- (CGSize)sizeWithFont:(UIFont *)font
     constrainedToSize:(CGSize)size
         lineBreakMode:(NSLineBreakMode)lineBreakMode 

- (CGSize)sizeWithFont:(UIFont *)font;

iOS7以后提供了新的计算方法

CGRect textRect = [text boundingRectWithSize:size
                                 options:NSStringDrawingUsesLineFragmentOrigin
                              attributes:@{NSFontAttributeName:FONT}
                                 context:nil];

CGSize size = textRect.size;

参考链接

当在.h文件用readonly修饰的时候,调用属性的setter方法会报错

@property (readonly, nonatomic, copy) NSString* eventDomain;
// 编译器会报错
 [self setEventDomain:@"someString"]; 

class extension中重新声明,告诉比编译器你需要setter方法:

@interface YourClass ()

@property (nonatomic, copy) NSString* eventDomain;

@end

参考链接

微博中的‘@用户名’和‘#话题#’这个正则表达式怎么写

匹配 @用户名

首先分析下微博中从哪里开始到哪里结束才是一个完整的用户名,按照常规的表现形式,一般是以@开头,以:结尾,中间为用户的名称。 匹配表达式就可写为: @[^::]+ 这是简单的写法,但是有些是在微博之后再@的,还有就是连续@的情况,还有些是以逗号等结束的,因此完善一下 修改为: @[^,,::\s@]+ 但是这些匹配都是从形式上进行了一个大概的归类,但是为了更为严谨,就要彻底分析其用户名的具体格式,例如新浪微博中的用户名格式为是“4-30个字符,支持英文、数字、"_"或减号” ,也就是说,支持中文、字母、数字、下划线及减号,并且是4到30个字符(这里暂且认为汉字为一个字符)那么在写匹配的表达式的时候就可以这么来写:
@[\u4e00-\u9fa5a-zA-Z0-9_-]{4,30}

匹配 #话题#

这个相对相对就简单了很多,前后都是#,以#号开始并以#结束 匹配表达式写为: #[^#]+#

参考链接

YYKit 中匹配 @用户名话题

+ (NSRegularExpression *)regexAt {
    static NSRegularExpression *regex;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        // 微博的 At 只允许 英文数字下划线连字符,和 unicode 4E00~9FA5 范围内的中文字符,这里保持和微博一致。。
        // 目前中文字符范围比这个大
        regex = [NSRegularExpression regularExpressionWithPattern:@"@[-_a-zA-Z0-9\u4E00-\u9FA5]+" options:kNilOptions error:NULL];
    });
    return regex;
}

+ (NSRegularExpression *)regexTopic {
    static NSRegularExpression *regex;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        regex = [NSRegularExpression regularExpressionWithPattern:@"#[^@#]+?#" options:kNilOptions error:NULL];
    });
    return regex;
}

参考链接

iOS7中 controllerview上移64问题

 // 在 controller 中调用
 self.edgesForExtendedLayout = UIRectEdgeNone;

iOS7 以上修改 navigation bar背景色

改变某一个 navigation controller(iOS7以上有效)

// tips : 在 viewDidLoad 方法中条用
self.navigationController.navigationBar.barTintColor = [UIColor customColor];
self.navigationController.navigationBar.translucent = NO;

更改整个 app 中的 navigation bar(iOS7以上有效)

// 在 appdelegate 中调用
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    [[UINavigationBar appearance] setBarTintColor:[UIColor whiteColor]];
    [[UINavigationBar appearance] setTranslucent:NO];
    return YES;
}

参考链接:How to change Navigation Bar color in iOS 7?

iOS获取当前版本号

//当前版本号,对应bundle,一般为数字:123
[[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString*)kCFBundleVersionKey];
//版本号,对应version,一般为字符串:1.0.2
[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];

自定义 UIBarButtonItem


// 导航栏左边的按钮
self.leftButton = [PPQButton ppq_buttonWithType:PPQButtonNavBarBackImageWhite];
[self.leftButton addTarget:self action:@selector(leftBtnClicked) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.leftButton];

// 导航栏 title view
self.titleView = [[UILabel alloc] init];
self.titleView.text = self.title;
self.titleView.font = [UIFont boldSystemFontOfSize:16];
[self.titleView sizeToFit];
self.navigationItem.titleView = self.titleView;

// 导航栏右侧
self.rightButton = [PPQButton ppq_buttonWithType:PPQButtonNavBarRightImageWhite];
[self.rightButton addTarget:self action:@selector(rightButtonClicked) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.rightButton];
    

隐藏掉 底部的黑线

[self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];

讲导航栏背景透明

[self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];

窝草,怎么导航栏没变透明??黑人问号


self.navigationController.navigationBar.translucent = YES;// 让导航栏具有穿透效果
[self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];// 这样导航栏就变透明了

相关文章

网友评论

    本文标题:iOS开发笔记-20170309

    本文链接:https://www.haomeiwen.com/subject/clbggttx.html