Skip to content

Commit

Permalink
Upgrade project to Xcode 9.3, Swift4.1
Browse files Browse the repository at this point in the history
  • Loading branch information
jkpang committed Apr 10, 2018
1 parent 19d5c67 commit ee9cb98
Show file tree
Hide file tree
Showing 16 changed files with 192 additions and 53 deletions.
12 changes: 12 additions & 0 deletions PPBadgeView/objc/PPBadgeLabel.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,20 @@

#import <UIKit/UIKit.h>


typedef NS_ENUM(NSUInteger, PPBadgeViewFlexMode) {
PPBadgeViewFlexModeHead, // 左伸缩 Head Flex : <==●
PPBadgeViewFlexModeTail, // 右伸缩 Tail Flex : ●==>
PPBadgeViewFlexModeMiddle // 左右伸缩 Middle Flex : <=●=>
};

@interface PPBadgeLabel : UILabel

+ (instancetype)defaultBadgeLabel;

/** Badge伸缩的方向, Default is PPBadgeViewFlexModeTail */
@property (nonatomic, assign) PPBadgeViewFlexMode flexMode;

/** 记录Badge的偏移量 Record the offset of Badge */
@property (nonatomic, assign) CGPoint offset;
@end
2 changes: 1 addition & 1 deletion PPBadgeView/objc/PPBadgeLabel.m
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ - (void)setupUI
self.textAlignment = NSTextAlignmentCenter;
self.layer.cornerRadius = self.p_height * 0.5;
self.layer.masksToBounds = YES;

self.backgroundColor = [UIColor colorWithRed:1.00 green:0.17 blue:0.15 alpha:1.00];
self.flexMode = PPBadgeViewFlexModeTail;
}

- (void)setText:(NSString *)text
Expand Down
16 changes: 16 additions & 0 deletions PPBadgeView/objc/UIBarButtonItem+PPBadgeView.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
#import <UIKit/UIKit.h>

@class PPBadgeLabel;

typedef NS_ENUM(NSUInteger, PPBadgeViewFlexMode);

@interface UIBarButtonItem (PPBadgeView)

/**
Expand Down Expand Up @@ -57,6 +60,19 @@
*/
- (void)pp_setBadgeHeight:(CGFloat)height;

/**
设置Badge伸缩的方向
Setting the direction of Badge expansion
PPBadgeViewFlexModeHead, // 左伸缩 Head Flex : <==●
PPBadgeViewFlexModeTail, // 右伸缩 Tail Flex : ●==>
PPBadgeViewFlexModeMiddle // 左右伸缩 Middle Flex : <=●=>
@param flexMode : Default is PPBadgeViewFlexModeTail
*/
- (void)pp_setBadgeFlexMode:(PPBadgeViewFlexMode)flexMode;

/**
设置Badge的偏移量, Badge中心点默认为其父视图的右上角
Expand Down
8 changes: 7 additions & 1 deletion PPBadgeView/objc/UIBarButtonItem+PPBadgeView.m
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ - (void)pp_setBadgeHeight:(CGFloat)height
[[self bottomView] pp_setBadgeHeight:height];
}

- (void)pp_setBadgeFlexMode:(PPBadgeViewFlexMode)flexMode
{
[[self bottomView] pp_setBadgeFlexMode:flexMode];
}

- (void)pp_moveBadgeWithX:(CGFloat)x Y:(CGFloat)y
{
[[self bottomView] pp_moveBadgeWithX:x Y:y];
Expand Down Expand Up @@ -87,7 +92,8 @@ - (UIView *)bottomView
{
// 通过Xcode视图调试工具找到UIBarButtonItem的Badge所在父视图为:UIImageView
UIView *navigationButton = [self valueForKey:@"_view"];
NSString *controlName = (kSystemVersion < 11 ? @"UIImageView" : @"UIButton" );
double systemVersion = [UIDevice currentDevice].systemVersion.doubleValue;
NSString *controlName = (systemVersion < 11 ? @"UIImageView" : @"UIButton" );
for (UIView *subView in navigationButton.subviews) {
if ([subView isKindOfClass:NSClassFromString(controlName)]) {
subView.layer.masksToBounds = NO;
Expand Down
16 changes: 16 additions & 0 deletions PPBadgeView/objc/UITabBarItem+PPBadgeView.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
#import <UIKit/UIKit.h>

@class PPBadgeLabel;

typedef NS_ENUM(NSUInteger, PPBadgeViewFlexMode);

@interface UITabBarItem (PPBadgeView)

/**
Expand Down Expand Up @@ -57,6 +60,19 @@
*/
- (void)pp_setBadgeHeight:(CGFloat)height;

/**
设置Badge伸缩的方向
Setting the direction of Badge expansion
PPBadgeViewFlexModeHead, // 左伸缩 Head Flex : <==●
PPBadgeViewFlexModeTail, // 右伸缩 Tail Flex : ●==>
PPBadgeViewFlexModeMiddle // 左右伸缩 Middle Flex : <=●=>
@param flexMode : Default is PPBadgeViewFlexModeTail
*/
- (void)pp_setBadgeFlexMode:(PPBadgeViewFlexMode)flexMode;

/**
设置Badge的偏移量, Badge中心点默认为其父视图的右上角
Expand Down
5 changes: 5 additions & 0 deletions PPBadgeView/objc/UITabBarItem+PPBadgeView.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ - (void)pp_setBadgeHeight:(CGFloat)height
[[self bottomView] pp_setBadgeHeight:height];
}

- (void)pp_setBadgeFlexMode:(PPBadgeViewFlexMode)flexMode
{
[[self bottomView] pp_setBadgeFlexMode:flexMode];
}

- (void)pp_moveBadgeWithX:(CGFloat)x Y:(CGFloat)y
{
[[self bottomView] pp_moveBadgeWithX:x Y:y];
Expand Down
21 changes: 17 additions & 4 deletions PPBadgeView/objc/UIView+PPBadgeView.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@
*********************************************************************************
*/

#ifndef kSystemVersion
#define kSystemVersion [UIDevice currentDevice].systemVersion.doubleValue
#endif

#import <UIKit/UIKit.h>

@class PPBadgeLabel;

typedef NS_ENUM(NSUInteger, PPBadgeViewFlexMode);

@interface UIView (PPBadgeView)

/**
Expand Down Expand Up @@ -71,6 +70,20 @@
*/
- (void)pp_moveBadgeWithX:(CGFloat)x Y:(CGFloat)y;


/**
设置Badge伸缩的方向
Setting the direction of Badge expansion
PPBadgeViewFlexModeHead, 左伸缩 Head Flex : <==●
PPBadgeViewFlexModeTail, 右伸缩 Tail Flex : ●==>
PPBadgeViewFlexModeMiddle 左右伸缩 Middle Flex : <=●=>
@param flexMode : Default is PPBadgeViewFlexModeTail
*/
- (void)pp_setBadgeFlexMode:(PPBadgeViewFlexMode)flexMode;

/**
设置Bage的属性
Expand Down
28 changes: 20 additions & 8 deletions PPBadgeView/objc/UIView+PPBadgeView.m
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,27 @@ - (void)pp_moveBadgeWithX:(CGFloat)x Y:(CGFloat)y
{
[self lazyLoadBadgeLabel];

/**
self.badgeLabel.center = CGPointMake(self.p_width+x, y);
如果通过badge的center来调整其在父视图的位置, 在给badge赋值不同长度的内容时
会导致badge会以中心点向两边调整其自身宽度,如果badge过长会遮挡部分父视图, 所以
正确的方式是以badge的x坐标为起点,其宽度向x轴正方向增加/x轴负方向减少
*/
self.badgeLabel.p_x = (self.p_width - self.badgeLabel.p_height*0.5)/*badge的x坐标*/ + x;
self.badgeLabel.offset = CGPointMake(x, y);

self.badgeLabel.p_y = -self.badgeLabel.p_height*0.5/*badge的y坐标*/ + y;

switch (self.badgeLabel.flexMode) {
case PPBadgeViewFlexModeHead: // 1. 左伸缩 <==●
self.badgeLabel.p_right = self.badgeLabel.superview.p_width + self.badgeLabel.p_height*0.5 + x;
break;
case PPBadgeViewFlexModeTail: // 2. 右伸缩 ●==>
self.badgeLabel.p_x = (self.p_width - self.badgeLabel.p_height*0.5)/*badge的x坐标*/ + x;
break;
case PPBadgeViewFlexModeMiddle: // 3. 左右伸缩 <=●=>
self.badgeLabel.center = CGPointMake(self.p_width+x, y);
break;
}
}

- (void)pp_setBadgeFlexMode:(PPBadgeViewFlexMode)flexMode
{
self.badgeLabel.flexMode = flexMode;
[self pp_moveBadgeWithX:self.badgeLabel.offset.x Y:self.badgeLabel.offset.y];
}

- (void)pp_setBadgeLabelAttributes:(void (^)(PPBadgeLabel *))badgeLabel
Expand Down
32 changes: 22 additions & 10 deletions PPBadgeView/swift/PPBadgeLabel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@

import UIKit

let kSystemVersion = (UIDevice.current.systemVersion as NSString).doubleValue
public enum PPBadgeViewFlexMode {
case head // 左伸缩 Head Flex : <==●
case tail // 右伸缩 Tail Flex : ●==>
case middle // 左右伸缩 Middle Flex : <=●=>
}



open class PPBadgeLabel: UILabel {

Expand All @@ -37,16 +43,13 @@ open class PPBadgeLabel: UILabel {
fatalError("init(coder:) has not been implemented")
}

private func setupUI() {
self.textColor = UIColor.white
self.font = UIFont.systemFont(ofSize: 13)
self.textAlignment = NSTextAlignment.center
self.layer.cornerRadius = self.p_height * 0.5
self.layer.masksToBounds = true
self.backgroundColor = UIColor.init(red: 1.00, green: 0.17, blue: 0.15, alpha: 1.0)
}
/// 记录Badge的偏移量 Record the offset of Badge
public var offset: CGPoint = CGPoint(x: 0, y: 0)

// 重写UILabel的text属性方法
/// Badge伸缩的方向, Default is PPBadgeViewFlexModeTail
public var flexMode: PPBadgeViewFlexMode = .tail

/// 重写UILabel的text属性方法
override open var text: String? {
didSet {
// 根据内容长度调整Label宽
Expand All @@ -59,6 +62,15 @@ open class PPBadgeLabel: UILabel {
}
}

private func setupUI() {
self.textColor = UIColor.white
self.font = UIFont.systemFont(ofSize: 13)
self.textAlignment = NSTextAlignment.center
self.layer.cornerRadius = self.p_height * 0.5
self.layer.masksToBounds = true
self.backgroundColor = UIColor.init(red: 1.00, green: 0.17, blue: 0.15, alpha: 1.0)
}

private func width(string: String?, font: UIFont, height: CGFloat) -> CGFloat {
if string?.isEmpty == true { return 0.0 }
var attributes : [NSAttributedStringKey: AnyObject] = [.font : font]
Expand Down
19 changes: 16 additions & 3 deletions PPBadgeView/swift/UIBarButtonItem+PPBadgeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@ public extension PP where Base: UIBarButtonItem {
_bottomView.pp.moveBadge(x: x, y: y)
}

/// 设置Badge伸缩的方向
///
/// Setting the direction of Badge expansion
///
/// PPBadgeViewFlexModeHead, 左伸缩 Head Flex : <==●
/// PPBadgeViewFlexModeTail, 右伸缩 Tail Flex : ●==>
/// PPBadgeViewFlexModeMiddle 左右伸缩 Middle Flex : <=●=>
/// - Parameter flexMode : Default is PPBadgeViewFlexModeTail
public func setBadge(flexMode: PPBadgeViewFlexMode = .tail) {
_bottomView.pp.setBadge(flexMode: flexMode)
}

/// 设置Badge的高度,因为Badge宽度是动态可变的,通过改变Badge高度,其宽度也按比例变化,方便布局
///
/// (注意: 此方法需要将Badge添加到控件上后再调用!!!)
Expand All @@ -74,8 +86,8 @@ public extension PP where Base: UIBarButtonItem {
/// (Note: this method needs to add Badge to the controls and then use it !!!)
///
/// - Parameter points: 高度大小
public func setBadgeHeight(_ points: CGFloat) {
_bottomView.pp.setBadgeHeight(points)
public func setBadge(height: CGFloat) {
_bottomView.pp.setBadge(height: height)
}

/// 设置Bage的属性
Expand Down Expand Up @@ -122,7 +134,8 @@ public extension PP where Base: UIBarButtonItem {
/// 通过Xcode视图调试工具找到UIBarButtonItem的Badge所在父视图为:UIImageView
private var _bottomView: UIView {
let navigationButton = (self.base.value(forKey: "_view") as? UIView) ?? UIView()
let controlName = (kSystemVersion < 11.0 ? "UIImageView" : "UIButton" )
let systemVersion = (UIDevice.current.systemVersion as NSString).doubleValue
let controlName = (systemVersion < 11.0 ? "UIImageView" : "UIButton" )
for subView in navigationButton.subviews {
if subView.isKind(of: NSClassFromString(controlName)!) {
subView.layer.masksToBounds = false
Expand Down
18 changes: 15 additions & 3 deletions PPBadgeView/swift/UITabBarItem+PPBadgeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,18 @@ public extension PP where Base: UITabBarItem {
_bottomView.pp.moveBadge(x: x, y: y)
}

/// 设置Badge伸缩的方向
///
/// Setting the direction of Badge expansion
///
/// PPBadgeViewFlexModeHead, 左伸缩 Head Flex : <==●
/// PPBadgeViewFlexModeTail, 右伸缩 Tail Flex : ●==>
/// PPBadgeViewFlexModeMiddle 左右伸缩 Middle Flex : <=●=>
/// - Parameter flexMode : Default is PPBadgeViewFlexModeTail
public func setBadge(flexMode: PPBadgeViewFlexMode = .tail) {
_bottomView.pp.setBadge(flexMode: flexMode)
}

/// 设置Badge的高度,因为Badge宽度是动态可变的,通过改变Badge高度,其宽度也按比例变化,方便布局
///
/// (注意: 此方法需要将Badge添加到控件上后再调用!!!)
Expand All @@ -75,9 +87,9 @@ public extension PP where Base: UITabBarItem {
///
/// (Note: this method needs to add Badge to the controls and then use it !!!)
///
/// - Parameter points: 高度大小
public func setBadgeHeight(_ points: CGFloat) {
_bottomView.pp.setBadgeHeight(points)
/// - Parameter height: 高度大小
public func setBadge(height: CGFloat) {
_bottomView.pp.setBadge(height: height)
}

/// 设置Bage的属性
Expand Down
40 changes: 28 additions & 12 deletions PPBadgeView/swift/UIView+PPBadgeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public extension PP where Base: UIView {
/// - Parameter color: 颜色
public func addDot(color: UIColor?) {
addBadge(text: "")
setBadgeHeight(8.0)
setBadge(height: 8.0)
if let color = color {
self.base.badgeLabel.backgroundColor = color
}
Expand All @@ -82,15 +82,31 @@ public extension PP where Base: UIView {
/// - x: X轴偏移量 (x<0: 左移, x>0: 右移) axis offset (x <0: left, x> 0: right)
/// - y: Y轴偏移量 (y<0: 上移, y>0: 下移) axis offset (Y <0: up, y> 0: down)
public func moveBadge(x: CGFloat, y: CGFloat) {
/**
self.badgeLabel.center = CGPointMake(self.p_width+x, y)

如果通过badge的center来调整其在父视图的位置, 在给badge赋值不同长度的内容时
会导致badge会以中心点向两边调整其自身宽度,如果badge过长会遮挡部分父视图, 所以
正确的方式是以badge的x坐标为起点,其宽度向x轴正方向增加/x轴负方向减少
*/
self.base.badgeLabel.p_x = (self.base.p_width - self.base.badgeLabel.p_height*0.5)/*badge的x坐标*/ + x

self.base.badgeLabel.offset = CGPoint(x: x, y: y)
self.base.badgeLabel.p_y = -self.base.badgeLabel.p_height*0.5/*badge的y坐标*/ + y

switch self.base.badgeLabel.flexMode {
case .head:
self.base.badgeLabel.p_right = self.base.badgeLabel.superview!.p_width + self.base.badgeLabel.p_height*0.5 + x
case .tail:
self.base.badgeLabel.p_x = (self.base.p_width - self.base.badgeLabel.p_height*0.5)/*badge的x坐标*/ + x
case .middle:
self.base.badgeLabel.center = CGPoint(x: self.base.p_width+x, y: y)
}
}

/// 设置Badge伸缩的方向
///
/// Setting the direction of Badge expansion
///
/// PPBadgeViewFlexModeHead, 左伸缩 Head Flex : <==●
/// PPBadgeViewFlexModeTail, 右伸缩 Tail Flex : ●==>
/// PPBadgeViewFlexModeMiddle 左右伸缩 Middle Flex : <=●=>
/// - Parameter flexMode : Default is PPBadgeViewFlexModeTail
public func setBadge(flexMode: PPBadgeViewFlexMode = .tail) {
self.base.badgeLabel.flexMode = flexMode
self.moveBadge(x: self.base.badgeLabel.offset.x, y: self.base.badgeLabel.offset.y)
}

/// 设置Badge的高度,因为Badge宽度是动态可变的,通过改变Badge高度,其宽度也按比例变化,方便布局
Expand All @@ -101,9 +117,9 @@ public extension PP where Base: UIView {
///
/// (Note: this method needs to add Badge to the controls and then use it !!!)
///
/// - Parameter points: 高度大小
public func setBadgeHeight(_ points: CGFloat) {
let scale = points / self.base.badgeLabel.p_height
/// - Parameter height: 高度大小
public func setBadge(height: CGFloat) {
let scale = height / self.base.badgeLabel.p_height
self.base.badgeLabel.transform = self.base.badgeLabel.transform.scaledBy(x: scale, y: scale)
}

Expand Down
Loading

0 comments on commit ee9cb98

Please sign in to comment.