Skip to content

Commit

Permalink
Support text font change
Browse files Browse the repository at this point in the history
  • Loading branch information
bawn committed Aug 26, 2020
1 parent 94da10c commit 582732f
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 28 deletions.
67 changes: 52 additions & 15 deletions Trident/MenuItemView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,28 @@

import UIKit

internal class MenuItemView: UILabel {
internal class MenuItemView: UIView {
private let textLabel: UILabel = {
let label = UILabel()
label.textAlignment = .center
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
private let placeholdLabel: UILabel = {
let label = UILabel()
label.isHidden = true
label.textAlignment = .center
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
private var normalColor = UIColor.white
private var selectedColor = UIColor.lightGray
private var normalColors = UIColor.white.rgb
private var selectedColors = UIColor.white.rgb
private var selectedColors = UIColor.lightGray.rgb
private var normalFont = UIFont.systemFont(ofSize: 15, weight: .regular)
private var selectedFont = UIFont.systemFont(ofSize: 15, weight: .medium)
var isSelected = false {

internal var isSelected = false {
didSet {
configAttributedText(isSelected ? 1 : 0)
}
Expand All @@ -55,11 +71,28 @@ internal class MenuItemView: UILabel {
self.normalFont = normalFont
self.selectedFont = selectedFont

self.normalColor = normalColor
self.selectedColor = selectedColor

normalColors = normalColor.rgb
selectedColors = selectedColor.rgb

addSubview(placeholdLabel)
NSLayoutConstraint.activate([
placeholdLabel.topAnchor.constraint(equalTo: topAnchor),
placeholdLabel.bottomAnchor.constraint(equalTo: bottomAnchor),
placeholdLabel.leadingAnchor.constraint(equalTo: leadingAnchor),
placeholdLabel.trailingAnchor.constraint(equalTo: trailingAnchor),
])
addSubview(textLabel)
NSLayoutConstraint.activate([
textLabel.centerXAnchor.constraint(equalTo: centerXAnchor),
textLabel.centerYAnchor.constraint(equalTo: centerYAnchor),
])

let attributes: [NSAttributedString.Key: Any] = [.font: normalFont, .foregroundColor: normalColor]
attributedText = NSAttributedString(string: text, attributes: attributes)
textLabel.attributedText = NSAttributedString(string: text, attributes: attributes)
placeholdLabel.attributedText = NSAttributedString(string: text, attributes: attributes)
}

internal func configAttributedText(_ rate: CGFloat) {
Expand All @@ -70,31 +103,35 @@ internal class MenuItemView: UILabel {


let strokeWidth = floor(CGFloat(selectedFont.weightValue - normalFont.weightValue) * 8.0) * rate
guard let text = attributedText?.string else {
guard let text = textLabel.attributedText?.string
, normalFont.pointSize != 0.0 else {
return
}

let color = UIColor(red: r, green: g, blue: b, alpha: a)
let attributes: [NSAttributedString.Key: Any] = [
.font: font,
.font: normalFont,
.foregroundColor: color,
.strokeWidth: -abs(strokeWidth),
.strokeColor: color
]

attributedText = NSAttributedString(string: text, attributes: attributes)
textLabel.attributedText = NSAttributedString(string: text, attributes: attributes)
placeholdLabel.attributedText = NSAttributedString(string: text, attributes: attributes)

let scaleRatio = (selectedFont.pointSize / normalFont.pointSize) - 1.0
let scaleValue = 1.0 + scaleRatio * rate
textLabel.transform = CGAffineTransform(scaleX: scaleValue, y: scaleValue)
}

// internal func showNormalStyle() {
// configAttributedText(0)
// }
//
// internal func showSelectedStyle() {
// configAttributedText(1)
// }

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}

func updateText(_ text: String) {
let attributes: [NSAttributedString.Key: Any] = [.font: normalFont, .foregroundColor: normalColor]
textLabel.attributedText = NSAttributedString(string: text, attributes: attributes)
placeholdLabel.attributedText = NSAttributedString(string: text, attributes: attributes)
}
}

24 changes: 12 additions & 12 deletions Trident/MenuView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -162,18 +162,18 @@ public class TridentMenuView: UIView {
return
}
titles.forEach { (item) in
let label = MenuItemView(item,
normalTextFont,
selectedTextFont,
normalTextColor,
selectedTextColor)
label.isUserInteractionEnabled = true
label.translatesAutoresizingMaskIntoConstraints = false
let itemView = MenuItemView(item,
normalTextFont,
selectedTextFont,
normalTextColor,
selectedTextColor)
itemView.isUserInteractionEnabled = true
itemView.translatesAutoresizingMaskIntoConstraints = false
let tap = UITapGestureRecognizer(target: self, action: #selector(titleTapAction(_:)))
label.addGestureRecognizer(tap)
stackView.addArrangedSubview(label)
label.heightAnchor.constraint(equalTo: stackView.heightAnchor).isActive = true
menuItemViews.append(label)
itemView.addGestureRecognizer(tap)
stackView.addArrangedSubview(itemView)
itemView.heightAnchor.constraint(equalTo: stackView.heightAnchor).isActive = true
menuItemViews.append(itemView)
}

currentIndex = 0
Expand Down Expand Up @@ -347,7 +347,7 @@ public class TridentMenuView: UIView {
}

currentIndex = index
let value:CGFloat = offsetX > CGFloat(titles.count - 1) * externalScrollView.bounds.width ? -1 : 1
let value: CGFloat = offsetX > CGFloat(titles.count - 1) * externalScrollView.bounds.width ? -1 : 1
scrollRate = value * (offsetX - CGFloat(currentIndex) * scrollViewWidth) / scrollViewWidth
layoutSlider(scrollRate)
}
Expand Down
2 changes: 1 addition & 1 deletion TridentDemo/DetailViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class DetailViewController: UIViewController {
.normalTextColor(UIColor.gray),
.selectedTextColor(UIColor.blue),
.normalTextFont(UIFont.systemFont(ofSize: 15.0, weight: .regular)),
.selectedTextFont(UIFont.systemFont(ofSize: 15.0, weight: .bold)),
.selectedTextFont(UIFont.systemFont(ofSize: 17.0, weight: .bold)),
.switchStyle(switchStyle),
.sliderStyle(
SliderViewStyle(parts:
Expand Down

0 comments on commit 582732f

Please sign in to comment.