IOS考试题3字体变大变小
swift写法,oc待续
FontSizePicker.swift
import UIKitenum FontSize : Int {case FontSizeSmall = 0, FontSizeMiddle, FontSizeBig, FontSizeSuperBig}protocol FontSizePickerDelegate : NSObjectProtocol {func fontSizePicker(picker: FontSizePicker, didSelectSize size: FontSize)}class FontSizePicker: UIView {var delegate: FontSizePickerDelegate! {didSet {self.buttonClick(button: self.btns[0])}}private var selectedButton: FontSizeButton!lazy private var btns = [FontSizeButton]()lazy private var topLine = UIImageView(image: UIImage(named: "正文字号-滑条红"))lazy private var bottomLine = UIImageView(image: UIImage(named: "正文字号-滑条"))lazy private var slider = UIImageView(image: UIImage(named: "正文字号-滑块"))override init(frame: CGRect) {super.init(frame: frame)// 创建4个buttonself.setupButton(icon: "正文字号-小(默认)", highIcon: "正文字号-小", tag: FontSize.FontSizeSmall)self.setupButton(icon: "正文字号-中(默认)", highIcon: "正文字号-中", tag: FontSize.FontSizeMiddle)self.setupButton(icon: "正文字号-大(默认)", highIcon: "正文字号-大", tag: FontSize.FontSizeBig)self.setupButton(icon: "正文字号-大+(默认)", highIcon: "正文字号-大+", tag: FontSize.FontSizeSuperBig)// 添加其他3个控件self.addSubview(self.bottomLine)self.addSubview(self.topLine)self.addSubview(self.slider)}required init?(coder: NSCoder) {fatalError("init(coder:) has not been implemented")}func setupButton(icon: String, highIcon: String, tag: FontSize) {// 创建按钮let btn = FontSizeButton(frame: CGRect.zero)btn.tag = tag.rawValuebtn.setImage(UIImage(named: icon), for: .normal)btn.setImage(UIImage(named: highIcon), for: .selected)btn.addTarget(self, action: #selector(buttonClick(button:)), for: .touchUpInside)self.addSubview(btn)// 添加到数组中self.btns.append(btn)}@objc func buttonClick(button: FontSizeButton) {if (selectedButton != nil){selectedButton.isSelected = false}button.isSelected = trueself.selectedButton = buttonself.setNeedsLayout()self.delegate.fontSizePicker(picker: self, didSelectSize: FontSize(rawValue: button.tag)!)}override func layoutSubviews() {super.layoutSubviews()// 1.按钮let btnCount = self.btns.countlet btnW: CGFloat = self.frame.size.width / CGFloat(btnCount)let btnY: CGFloat = 5let btnH: CGFloat = 30for i in 0..<btnCount {let btn = self.btns[i]let btnX: CGFloat = CGFloat(i) * btnW//btn.frame = CGRect(btnX, btnY, btnW, btnH)btn.frame = CGRect(x: btnX, y: btnY, width: btnW, height: btnH)}// 2.白条let firstBtnCenterX = self.btns[0].center.xlet lastBtnCenterX = self.btns[self.btns.count - 1].center.xlet bottomLineX: CGFloat = firstBtnCenterX - self.slider.frame.size.width * 0.5let bottomLineMaxX: CGFloat = lastBtnCenterX + self.slider.frame.size.width * 0.5let bottomLineW = bottomLineMaxX - bottomLineXlet bottomLineY = btnY + btnH + 15self.bottomLine.frame = CGRect(x: bottomLineX, y: bottomLineY, width: bottomLineW, height: 3)// 3.滑块if (selectedButton != nil) {self.slider.center.x = self.selectedButton.center.xself.slider.center.y = self.bottomLine.center.yself.topLine.frame = self.bottomLine.frameself.topLine.frame.size.width = self.slider.center.x - self.topLine.frame.origin.x}}class FontSizeButton : UIButton {override init(frame: CGRect) {super.init(frame: frame)self.adjustsImageWhenHighlighted = falseself.imageView?.contentMode = UIView.ContentMode.scaleAspectFit}required init?(coder: NSCoder) {fatalError("init(coder:) has not been implemented")}override var isHighlighted: Bool {set {}get {return false}}}}
ViewController.swift
//// ViewController.swift// 003-考试题3//// Created by 鲁军 on /5/30.//import UIKitclass ViewController: UIViewController , FontSizePickerDelegate {@IBOutlet weak var label: UILabel!override func viewDidLoad() {super.viewDidLoad()let picker = FontSizePicker(frame: CGRect.zero)//picker.frame = CGRect(0, 150, 320, 80)picker.frame = CGRect(x: 0, y: 150, width: 320, height: 80)picker.delegate = selfpicker.backgroundColor = UIColor.systemTealself.view.addSubview(picker)debugPrint(self.view.frame)debugPrint(self.view.bounds)}func fontSizePicker(picker: FontSizePicker, didSelectSize size: FontSize) {switch size {case .FontSizeSmall:self.label.font = UIFont.systemFont(ofSize: 11)case .FontSizeMiddle:self.label.font = UIFont.systemFont(ofSize: 15)case .FontSizeBig:self.label.font = UIFont.systemFont(ofSize: 18)case .FontSizeSuperBig:self.label.font = UIFont.systemFont(ofSize: 25)}}}
如果觉得《IOS考试题3字体变大变小》对你有帮助,请点赞、收藏,并留下你的观点哦!