diff --git a/Riot/Modules/BadgeLabel/BadgeLabel.h b/Riot/Modules/BadgeLabel/BadgeLabel.h deleted file mode 100644 index ccebf2f67..000000000 --- a/Riot/Modules/BadgeLabel/BadgeLabel.h +++ /dev/null @@ -1,31 +0,0 @@ -// -// Copyright 2021 New Vector Ltd -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// - -#import - -NS_ASSUME_NONNULL_BEGIN - -IB_DESIGNABLE -@interface BadgeLabel : UILabel - -@property IBInspectable (nonatomic, weak) UIColor *badgeColor; -@property IBInspectable (nonatomic) CGFloat borderWidth; -@property IBInspectable (nonatomic, weak) UIColor *borderColor; -@property IBInspectable (nonatomic) CGSize padding; - -@end - -NS_ASSUME_NONNULL_END diff --git a/Riot/Modules/BadgeLabel/BadgeLabel.m b/Riot/Modules/BadgeLabel/BadgeLabel.m deleted file mode 100644 index bb4932a76..000000000 --- a/Riot/Modules/BadgeLabel/BadgeLabel.m +++ /dev/null @@ -1,96 +0,0 @@ -// -// Copyright 2021 New Vector Ltd -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// - -#import "BadgeLabel.h" - -@implementation BadgeLabel - -- (instancetype)initWithCoder:(NSCoder *)coder -{ - self = [super initWithCoder:coder]; - if (self) { - [self setupView]; - } - return self; -} - -- (instancetype)initWithFrame:(CGRect)frame -{ - self = [super initWithFrame:frame]; - if (self) { - [self setupView]; - } - return self; -} - -- (void)layoutSubviews -{ - [super layoutSubviews]; - self.layer.cornerRadius = self.bounds.size.height / 2; -} - -- (CGSize)intrinsicContentSize -{ - CGSize intrinsicSize = [super intrinsicContentSize]; - intrinsicSize.height = MAX(intrinsicSize.height + self.padding.height, intrinsicSize.height) + self.borderWidth / 2; - intrinsicSize.width = MAX(intrinsicSize.width + self.padding.width, intrinsicSize.height); - return intrinsicSize; -} - -- (void)drawRect:(CGRect)rect -{ - CGContextRef context = UIGraphicsGetCurrentContext(); - CGContextSaveGState(context); - - CGRect backgroundRect = CGRectInset(self.bounds, self.borderWidth / 2, self.borderWidth / 2); - CGFloat cornerRadius = backgroundRect.size.height / 2; - UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:backgroundRect cornerRadius:cornerRadius]; - CGContextAddPath(context, [path CGPath]); - CGContextSetLineWidth(context, self.borderWidth); - CGContextSetStrokeColorWithColor(context, [self.borderColor CGColor]); - CGContextSetFillColorWithColor(context, [self.badgeColor CGColor]); - - if (self.borderWidth > 0) - { - CGContextDrawPath(context, kCGPathFillStroke); - } - else - { - CGContextDrawPath(context, kCGPathFill); - } - - CGContextRestoreGState(context); - - [super drawRect:rect]; -} - -- (void)prepareForInterfaceBuilder -{ - [super prepareForInterfaceBuilder]; - [self setupView]; -} - -- (void)setupView -{ - self.badgeColor = UIColor.redColor; - self.borderWidth = 0; - self.borderColor = UIColor.whiteColor; - self.padding = CGSizeMake(10, 2); - self.textAlignment = NSTextAlignmentCenter; - self.textColor = UIColor.whiteColor; -} - -@end diff --git a/Riot/Modules/BadgeLabel/BadgeLabel.swift b/Riot/Modules/BadgeLabel/BadgeLabel.swift new file mode 100644 index 000000000..f52409871 --- /dev/null +++ b/Riot/Modules/BadgeLabel/BadgeLabel.swift @@ -0,0 +1,112 @@ +// +// Copyright 2021 New Vector Ltd +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +import UIKit + +@IBDesignable +@objcMembers +class BadgeLabel: UILabel { + + // MARK: - Public properties + + @IBInspectable var badgeColor: UIColor = .red { + didSet { + setNeedsDisplay() + } + } + + @IBInspectable var borderWidth: CGFloat = 0 { + didSet { + invalidateIntrinsicContentSize() + } + } + + @IBInspectable var borderColor: UIColor = .white { + didSet { + invalidateIntrinsicContentSize() + } + } + + @IBInspectable var padding: CGSize = CGSize(width: 10, height: 2) { + didSet { + invalidateIntrinsicContentSize() + } + } + + // MARK: - Lifecycle + + required init?(coder: NSCoder) { + super.init(coder: coder) + setupView() + } + + override init(frame: CGRect) { + super.init(frame: frame) + setupView() + } + + override func layoutSubviews() { + super.layoutSubviews() + + self.layer.cornerRadius = self.bounds.size.height / 2 + } + + override var intrinsicContentSize: CGSize { + var intrinsicSize = super.intrinsicContentSize + intrinsicSize.height = max(intrinsicSize.height + padding.height, intrinsicSize.height) + borderWidth / 2 + intrinsicSize.width = max(intrinsicSize.width + padding.width, intrinsicSize.height) + return intrinsicSize + } + + override func draw(_ rect: CGRect) { + if let context = UIGraphicsGetCurrentContext() { + context.saveGState() + + let rect = self.bounds.insetBy(dx: borderWidth / 2, dy: borderWidth / 2) + let cornerRadius = rect.height / 2 + let path = UIBezierPath(roundedRect: rect, cornerRadius: cornerRadius) + + context.addPath(path.cgPath) + context.setLineWidth(borderWidth) + context.setStrokeColor(borderColor.cgColor) + context.setFillColor(badgeColor.cgColor) + + if borderWidth > 0 { + context.drawPath(using: .fillStroke) + } else { + context.drawPath(using: .fill) + } + + context.restoreGState() + } + + super.draw(rect) + } + + // MARK: - Interface Builder + + override func prepareForInterfaceBuilder() { + super.prepareForInterfaceBuilder() + setupView() + } + + // MARK: - Private methods + + private func setupView() { + self.textAlignment = .center + self.textColor = .white + } +} diff --git a/Riot/Modules/Common/Recents/Views/RecentTableViewCell.h b/Riot/Modules/Common/Recents/Views/RecentTableViewCell.h index 04475a1e1..d2e776b92 100644 --- a/Riot/Modules/Common/Recents/Views/RecentTableViewCell.h +++ b/Riot/Modules/Common/Recents/Views/RecentTableViewCell.h @@ -15,7 +15,6 @@ */ #import -#import "BadgeLabel.h" /** `RecentTableViewCell` instances display a room in the context of the recents list. diff --git a/Riot/Modules/Home/Views/RoomCollectionViewCell.h b/Riot/Modules/Home/Views/RoomCollectionViewCell.h index c415ba579..fd5c93afc 100644 --- a/Riot/Modules/Home/Views/RoomCollectionViewCell.h +++ b/Riot/Modules/Home/Views/RoomCollectionViewCell.h @@ -15,7 +15,8 @@ */ #import -#import "BadgeLabel.h" + +@class BadgeLabel; /** 'RoomCollectionViewCell' class is used to display a room in a collection view. diff --git a/Riot/Modules/Home/Views/RoomCollectionViewCell.xib b/Riot/Modules/Home/Views/RoomCollectionViewCell.xib index 55bf49818..837d0de4e 100644 --- a/Riot/Modules/Home/Views/RoomCollectionViewCell.xib +++ b/Riot/Modules/Home/Views/RoomCollectionViewCell.xib @@ -32,7 +32,7 @@ -