Skip to content

Instantly share code, notes, and snippets.

View sanllier's full-sized avatar

Alex Gor. sanllier

  • Yandex
  • Russia, Moscow
View GitHub Profile
extension UIView {
static let swizzle: Void = {
let originalSelector = #selector(layoutSubviews)
let swizzledSelector = #selector(swizzled_layoutSubviews)
guard let originalMethod = class_getInstanceMethod(UIView.self, originalSelector) else { return }
guard let swizzledMethod = class_getInstanceMethod(UIView.self, swizzledSelector) else { return }
method_exchangeImplementations(originalMethod, swizzledMethod)
}()
@objc func veryUsefulMethod() {
guard let superclass = superclass else { return }
let objcSelector = Selector("veryUsefulMethod")
typealias CFunction = @convention(c) (AnyObject, Selector) -> Void
let impl = class_getMethodImplementation(superclass, objcSelector)
let callableImpl = unsafeBitCast(impl, to: CFunction.self)
callableImpl(self, objcSelector)
let objcSelector = Selector("veryUsefulMethod")
typealias CFunction = @convention(c) (AnyObject, Selector) -> Void
let impl = class_getMethodImplementation(type(of: self).self, objcSelector)
let callableImpl = unsafeBitCast(impl, to: CFunction.self)
callableImpl(self, objcSelector)
@objc func _needsDoubleUpdateConstraintsPass() -> Bool {
return true
}
override var intrinsicContentSize: CGSize {
  return attributedText?.size(forWidth: (engineBounds ?? bounds).width) ?? .zero
}
var engineBounds: CGRect? {
let objcSelector = "_nsis_compatibleBoundsInEngine:")
[<CustomLabel 0x7fcb8350d0d0>/UILabel _prepareForFirstIntrinsicContentSizeCalculation]
...
[<CustomLabel 0x7fcb8350d0d0>/UILabel _needsDoubleUpdateConstraintsPass] -> 1
...
[<CustomLabel 0x7fcb8350d0d0>/UILabel _prepareForSecondIntrinsicContentSizeCalculationWithLayoutEngineBounds:{{20, 20}, {25000000, 20.5}}]
@objc func _prepareForSecondIntrinsicContentSizeCalculation(withLayoutEngineBounds bounds: CGRect) {
print(bounds)
}
override var intrinsicContentSize: CGSize {
    return attributedText?.size(forWidth: (fittingSize ?? bounds).width) ?? .zero
}
var fittingSize: CGRect?
override func systemLayoutSizeFitting(
    _ targetSize: CGSize,
    withHorizontalFittingPriority horizontalFittingPriority: UILayoutPriority,
    verticalFittingPriority: UILayoutPriority) -> CGSize
{
// Прокинуть верный размер с учетом отступов
(content as? ASLabel)?.fittingSize = computeContentTargetSize(for: targetSize)
return super.systemLayoutSizeFitting(...)
}
extension NSAttributedString {
func size(forWidth width: CGFloat) -> CGSize {
return boundingRect(
with: CGSize(width: width, height: CGFloat.greatestFiniteMagnitude),
options: [.usesLineFragmentOrigin, .usesFontLeading],
context: nil
).size
}
}