This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class ViewController: UIViewController { | |
let str = """ | |
Hong Kong (CNN Business)This was supposed to be the year that Beijing would announce it had ended absolute poverty in China, fulfilling a key pledge of President Xi Jinping and one of the founding missions of the Chinese Communist Party. | |
Then the novel coronavirus pandemic tore through the global economy. | |
China shut down factories in January and February to prevent a larger outbreak and contain the virus, but at the same time it severely damaged employment and production domestically. | |
Consequently, China's economy shrank by 6.8% in the first quarter of 2020 compared to a year earlier. For the first time in decades, Beijing didn't set a GDP target at its annual meeting of its rubber-stamp parliament, the National People's Congress (NPC), in May. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private func adaptToUserInterfaceStyle() { | |
label.backgroundColor = UIColor.brown.withAlphaComponent(0.3) | |
label.textColor = UIColor.brown | |
if #available(iOS 12.0, *) { | |
if contentView.traitCollection.userInterfaceStyle == .dark { | |
label.backgroundColor = UIColor.cyan.withAlphaComponent(0.3) | |
label.textColor = UIColor.cyan | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BBInsetLabel: UILabel { | |
var textInsets = UIEdgeInsets.zero | |
override func textRect(forBounds bounds: CGRect, limitedToNumberOfLines numberOfLines: Int) -> CGRect { | |
guard text != nil else { | |
return super.textRect(forBounds: bounds, limitedToNumberOfLines: numberOfLines) | |
} | |
let insetedRect = bounds.inset(by: textInsets) | |
let textRect = super.textRect(forBounds: insetedRect, limitedToNumberOfLines: numberOfLines) | |
let newRect = textRect.inset(by: textInsets.inverted()) | |
return newRect |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ViewController: UIViewController { | |
let str = """ | |
Hong Kong (CNN Business)This was supposed to be the year that Beijing would announce it had ended absolute poverty in China, fulfilling a key pledge of President Xi Jinping and one of the founding missions of the Chinese Communist Party. | |
Then the novel coronavirus pandemic tore through the global economy. | |
China shut down factories in January and February to prevent a larger outbreak and contain the virus, but at the same time it severely damaged employment and production domestically. | |
Consequently, China's economy shrank by 6.8% in the first quarter of 2020 compared to a year earlier. For the first time in decades, Beijing didn't set a GDP target at its annual meeting of its rubber-stamp parliament, the National People's Congress (NPC), in May. | |
Since then, China has been trying to dig itself out of its economic slump, and there are some signs of recovery — though the path forward is still looking slow and painful. | |
Facing r |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ViewController: UIViewController { | |
let str = """ | |
Hong Kong (CNN Business)This was supposed to be the year that Beijing would announce it had ended absolute poverty in China, fulfilling a key pledge of President Xi Jinping and one of the founding missions of the Chinese Communist Party. | |
Then the novel coronavirus pandemic tore through the global economy. | |
China shut down factories in January and February to prevent a larger outbreak and contain the virus, but at the same time it severely damaged employment and production domestically. | |
Consequently, China's economy shrank by 6.8% in the first quarter of 2020 compared to a year earlier. For the first time in decades, Beijing didn't set a GDP target at its annual meeting of its rubber-stamp parliament, the National People's Congress (NPC), in May. | |
Since then, China has been trying to dig itself out of its economic slump, and there are some signs of recovery — though the path forward is still looking slow and painful. | |
Facing r |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ViewController: UIViewController { | |
var viewModel: [String] = [] | |
lazy var collectionView: UICollectionView = { | |
let layout = UICollectionViewFlowLayout() | |
layout.estimatedItemSize = CGSize(width: 1, height: 1) | |
let view = UICollectionView(frame: .zero, collectionViewLayout: layout) | |
view.translatesAutoresizingMaskIntoConstraints = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LabelCollectionViewCell: UICollectionViewCell { | |
var label: UILabel = { | |
let view = UILabel() | |
view.translatesAutoresizingMaskIntoConstraints = false | |
return view | |
}() | |
private lazy var labelConstraints: [NSLayoutConstraint] = [ | |
label.topAnchor.constraint(equalTo: contentView.topAnchor), | |
label.bottomAnchor.constraint(equalTo: contentView.bottomAnchor), | |
label.leadingAnchor.constraint(equalTo: contentView.leadingAnchor), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NotificationCenter.default.addObserver(self, selector: #selector(audioRouteChange(_:)), name: .AVAudioSessionRouteChange, object: nil) | |
@objc func audioRouteChange(_ notification: Notification) { | |
guard let userInfo = notification.userInfo, | |
let rv = userInfo[AVAudioSessionRouteChangeReasonKey] as? UInt, | |
let reason = AVAudioSessionRouteChangeReason(rawValue: rv) else { return } | |
switch reason { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var isHeadphonePluggedIn: Bool { | |
let audioOutputs: [AVAudioSessionPortDescription] = AVAudioSession.sharedInstance().currentRoute.outputs | |
for output in audioOutputs where output.portType == AVAudioSessionPortHeadphones { | |
print("headphone plugged in") | |
return true | |
} | |
return false | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.swift | |
// Font | |
// | |
// Created by Bob on 2019/7/9. | |
// Copyright © 2019 Hamster. All rights reserved. | |
// | |
import UIKit |
NewerOlder