Skip to content

Commit c2bb927

Browse files
committed
Fix autolayout intrinsicContentSize bug
1 parent 1dbf757 commit c2bb927

8 files changed

Lines changed: 136 additions & 24 deletions

Atributika.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
1B4638582259FFE400ED5BA9 /* AttributedLabel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1B38369F1F97C224001DB333 /* AttributedLabel.swift */; };
2222
1B4638592259FFE400ED5BA9 /* AttributedLabel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1B38369F1F97C224001DB333 /* AttributedLabel.swift */; };
2323
1B46385A2259FFE400ED5BA9 /* AttributedLabel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1B38369F1F97C224001DB333 /* AttributedLabel.swift */; };
24+
1B80548A23A8ACDF002DCA63 /* IBViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1B80548923A8ACDF002DCA63 /* IBViewController.swift */; };
25+
1B80548C23A8ACF1002DCA63 /* IB.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 1B80548B23A8ACF1002DCA63 /* IB.storyboard */; };
2426
1BB55AD11E5C73EF00CE6EAD /* AttributedText.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1BB55ACC1E5C73EF00CE6EAD /* AttributedText.swift */; };
2527
1BB55AD21E5C73EF00CE6EAD /* AttributedText.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1BB55ACC1E5C73EF00CE6EAD /* AttributedText.swift */; };
2628
1BB55AD31E5C73F000CE6EAD /* AttributedText.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1BB55ACC1E5C73EF00CE6EAD /* AttributedText.swift */; };
@@ -105,6 +107,8 @@
105107
1B2F83991E5C98A4009AFBCA /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
106108
1B2F839B1E5C98A4009AFBCA /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
107109
1B38369F1F97C224001DB333 /* AttributedLabel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = AttributedLabel.swift; path = Sources/AttributedLabel.swift; sourceTree = "<group>"; };
110+
1B80548923A8ACDF002DCA63 /* IBViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IBViewController.swift; sourceTree = "<group>"; };
111+
1B80548B23A8ACF1002DCA63 /* IB.storyboard */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; path = IB.storyboard; sourceTree = "<group>"; };
108112
1BB55ACC1E5C73EF00CE6EAD /* AttributedText.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = AttributedText.swift; path = Sources/AttributedText.swift; sourceTree = "<group>"; };
109113
1BB55ACD1E5C73EF00CE6EAD /* NSAttributedString+Utils.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "NSAttributedString+Utils.swift"; path = "Sources/NSAttributedString+Utils.swift"; sourceTree = "<group>"; };
110114
1BB55ACE1E5C73EF00CE6EAD /* NSScanner+Swift.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "NSScanner+Swift.swift"; path = "Sources/NSScanner+Swift.swift"; sourceTree = "<group>"; };
@@ -194,6 +198,8 @@
194198
1BDDF5681E683A8F006374F6 /* Snippet.swift */,
195199
1B2F83911E5C98A4009AFBCA /* SnippetsViewController.swift */,
196200
1BDDF56A1E6841EA006374F6 /* AttributedLabelDemoViewController.swift */,
201+
1B80548923A8ACDF002DCA63 /* IBViewController.swift */,
202+
1B80548B23A8ACF1002DCA63 /* IB.storyboard */,
197203
1B2F838F1E5C98A4009AFBCA /* AppDelegate.swift */,
198204
1B2F83961E5C98A4009AFBCA /* Assets.xcassets */,
199205
1B2F83981E5C98A4009AFBCA /* LaunchScreen.storyboard */,
@@ -523,6 +529,7 @@
523529
buildActionMask = 2147483647;
524530
files = (
525531
1B2F839A1E5C98A4009AFBCA /* LaunchScreen.storyboard in Resources */,
532+
1B80548C23A8ACF1002DCA63 /* IB.storyboard in Resources */,
526533
1B2F83971E5C98A4009AFBCA /* Assets.xcassets in Resources */,
527534
);
528535
runOnlyForDeploymentPostprocessing = 0;
@@ -583,6 +590,7 @@
583590
isa = PBXSourcesBuildPhase;
584591
buildActionMask = 2147483647;
585592
files = (
593+
1B80548A23A8ACDF002DCA63 /* IBViewController.swift in Sources */,
586594
1BDDF5691E683A8F006374F6 /* Snippet.swift in Sources */,
587595
1B2F83921E5C98A4009AFBCA /* SnippetsViewController.swift in Sources */,
588596
1B2F83901E5C98A4009AFBCA /* AppDelegate.swift in Sources */,

Demo/AppDelegate.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,15 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
2323
let tbc = UITabBarController()
2424

2525
let vc1 = SnippetsViewController()
26+
vc1.title = "Snippets"
2627

2728
let vc2 = AttributedLabelDemoViewController()
29+
vc2.title = "AttributedLabel"
30+
31+
let vc3 = UIStoryboard(name: "IB", bundle: nil).instantiateViewController(withIdentifier: "ib")
32+
vc3.title = "Storyboard"
2833

29-
tbc.viewControllers = [UINavigationController(rootViewController: vc1), UINavigationController(rootViewController: vc2)]
34+
tbc.viewControllers = [UINavigationController(rootViewController: vc1), UINavigationController(rootViewController: vc2), UINavigationController(rootViewController: vc3),]
3035

3136
window = UIWindow(frame: UIScreen.main.bounds)
3237
window?.rootViewController = tbc

Demo/AttributedLabelDemoViewController.swift

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,6 @@ class AttributedLabelDemoViewController: UIViewController {
4444
"Here's how a similar one was solved 😄 \nhttps://medium.com/@narcelio/solving-decred-mockingbird-puzzle-5366efeaeed7\n"
4545
]
4646

47-
init() {
48-
super.init(nibName: nil, bundle: nil)
49-
title = "AttributedLabel"
50-
}
51-
52-
required init?(coder aDecoder: NSCoder) {
53-
fatalError("init(coder:) has not been implemented")
54-
}
55-
5647
override func viewDidLoad() {
5748
super.viewDidLoad()
5849
view.addSubview(tableView)
@@ -127,7 +118,7 @@ class TweetCell: UITableViewCell {
127118

128119
var tweet: String? {
129120
didSet {
130-
let all = Style.font(.systemFont(ofSize: 20))
121+
let all = Style.font(UIFont.preferredFont(forTextStyle: .body))
131122
let link = Style("a")
132123
.foregroundColor(.blue, .normal)
133124
.foregroundColor(.brown, .highlighted)

Demo/IB.storyboard

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="15702" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
3+
<device id="retina6_1" orientation="portrait" appearance="light"/>
4+
<dependencies>
5+
<deployment identifier="iOS"/>
6+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="15704"/>
7+
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
8+
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
9+
</dependencies>
10+
<scenes>
11+
<!--View Controller-->
12+
<scene sceneID="1UQ-0b-UMO">
13+
<objects>
14+
<viewController storyboardIdentifier="ib" useStoryboardIdentifierAsRestorationIdentifier="YES" id="B3p-Ka-8fb" customClass="IBViewController" customModule="Demo" customModuleProvider="target" sceneMemberID="viewController">
15+
<view key="view" contentMode="scaleToFill" id="lOE-87-mnQ">
16+
<rect key="frame" x="0.0" y="0.0" width="414" height="896"/>
17+
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
18+
<subviews>
19+
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="6vT-71-D5X">
20+
<rect key="frame" x="0.0" y="662" width="414" height="200"/>
21+
<subviews>
22+
<view contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" placeholderIntrinsicWidth="150" placeholderIntrinsicHeight="20" translatesAutoresizingMaskIntoConstraints="NO" id="k16-by-tft" customClass="AttributedLabel" customModule="Atributika">
23+
<rect key="frame" x="132" y="0.0" width="150" height="156"/>
24+
<color key="backgroundColor" systemColor="systemOrangeColor" red="1" green="0.58431372550000005" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
25+
</view>
26+
</subviews>
27+
<color key="backgroundColor" systemColor="systemBrownColor" red="0.63529411759999999" green="0.51764705879999995" blue="0.36862745099999999" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
28+
<constraints>
29+
<constraint firstItem="k16-by-tft" firstAttribute="leading" relation="greaterThanOrEqual" secondItem="6vT-71-D5X" secondAttribute="leading" constant="30" id="cFy-da-OK3"/>
30+
<constraint firstItem="k16-by-tft" firstAttribute="centerX" secondItem="6vT-71-D5X" secondAttribute="centerX" id="g4y-7o-aVq"/>
31+
<constraint firstAttribute="height" constant="200" id="iQQ-GR-iNC"/>
32+
<constraint firstItem="k16-by-tft" firstAttribute="top" secondItem="6vT-71-D5X" secondAttribute="top" id="oSm-jH-xZP"/>
33+
<constraint firstAttribute="bottom" secondItem="k16-by-tft" secondAttribute="bottom" constant="44" id="xpB-86-C8P"/>
34+
</constraints>
35+
</view>
36+
</subviews>
37+
<color key="backgroundColor" systemColor="systemBackgroundColor" cocoaTouchSystemColor="whiteColor"/>
38+
<constraints>
39+
<constraint firstItem="6vT-71-D5X" firstAttribute="leading" secondItem="gp0-HC-Ma7" secondAttribute="leading" id="ZsX-v7-NJK"/>
40+
<constraint firstItem="gp0-HC-Ma7" firstAttribute="bottom" secondItem="6vT-71-D5X" secondAttribute="bottom" id="hVb-jE-z7b"/>
41+
<constraint firstItem="gp0-HC-Ma7" firstAttribute="trailing" secondItem="6vT-71-D5X" secondAttribute="trailing" id="jef-Y5-H2h"/>
42+
</constraints>
43+
<viewLayoutGuide key="safeArea" id="gp0-HC-Ma7"/>
44+
</view>
45+
<connections>
46+
<outlet property="attributedLabel" destination="k16-by-tft" id="1XE-iB-F5T"/>
47+
</connections>
48+
</viewController>
49+
<placeholder placeholderIdentifier="IBFirstResponder" id="Cof-ZH-xC1" userLabel="First Responder" customClass="UIResponder" sceneMemberID="firstResponder"/>
50+
</objects>
51+
<point key="canvasLocation" x="139" y="138"/>
52+
</scene>
53+
</scenes>
54+
</document>

Demo/IBViewController.swift

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//
2+
// IBViewController.swift
3+
// Demo
4+
//
5+
// Created by Pavel Sharanda on 12/17/19.
6+
// Copyright © 2019 Atributika. All rights reserved.
7+
//
8+
9+
import UIKit
10+
import Atributika
11+
12+
class IBViewController: UIViewController {
13+
14+
override func viewDidLoad() {
15+
super.viewDidLoad()
16+
17+
let font: UIFont
18+
if #available(iOS 11.0, *) {
19+
let systemFont = UIFont.systemFont(ofSize: 16)
20+
let metrics = UIFontMetrics(forTextStyle: .body)
21+
font = metrics.scaledFont(for: systemFont)
22+
} else {
23+
font = UIFont.systemFont(ofSize: 16)
24+
}
25+
26+
let button = Style("button")
27+
.underlineStyle(.styleSingle)
28+
.font(font)
29+
.foregroundColor(.black, .normal)
30+
.foregroundColor(.red, .highlighted)
31+
32+
attributedLabel.attributedText = "<button>Need to register?</button>".style(tags: button)
33+
}
34+
35+
@IBOutlet private var attributedLabel: AttributedLabel!
36+
}

Demo/Snippet.swift

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,18 @@ func stringWithLink() -> NSAttributedString {
5454
return str
5555
}
5656

57+
func stringWithBoldItalic() -> NSAttributedString {
58+
59+
let baseFont = UIFont.systemFont(ofSize: 12)
60+
let descriptor = baseFont.fontDescriptor.withSymbolicTraits([.traitItalic, .traitBold])
61+
let font = descriptor.map { UIFont(descriptor: $0, size: baseFont.pointSize) } ?? baseFont
62+
63+
let a = Style("a").font(font).foregroundColor(.blue)
64+
let str = "<a href=\"https://en.wikipedia.org/wiki/World_of_Dance_(TV_series)\" target=\"_blank\">World of Dance</a>".style(tags: a)
65+
.attributedString
66+
return str
67+
}
68+
5769
func stringWithManyDetectables() -> NSAttributedString {
5870

5971
let links = Style.foregroundColor(.blue)
@@ -139,6 +151,23 @@ func stringWithUnorderedList() -> NSAttributedString {
139151
.attributedString
140152
}
141153

154+
func stringWithOrderedList() -> NSAttributedString {
155+
var counter = 0
156+
let transformers: [TagTransformer] = [
157+
TagTransformer.brTransformer,
158+
TagTransformer(tagName: "ol", tagType: .start) { _ in
159+
counter = 0
160+
return ""
161+
},
162+
TagTransformer(tagName: "li", tagType: .start) { _ in
163+
counter += 1
164+
return "\(counter > 1 ? "\n" : "")\(counter). "
165+
}
166+
]
167+
168+
return "<ol><li>Coffee</li><li>Tea</li><li>Milk</li></ol>".style(tags: [], transformers: transformers).attributedString
169+
}
170+
142171
func stringWithHref() -> NSAttributedString {
143172
return "Hey\r\n<a style=\"text-decoration:none\" href=\"http://www.google.com\">Hello\r\nWorld</a>s".style(tags:
144173
Style("a").font(.boldSystemFont(ofSize: 45)).foregroundColor(.red)
@@ -227,12 +256,14 @@ func allSnippets() -> [NSAttributedString] {
227256
stringWithHashTagAndMention(),
228257
stringWithPhone(),
229258
stringWithLink(),
259+
stringWithBoldItalic(),
230260
stringWithManyDetectables(),
231261
stringWith3Tags(),
232262
stringWithGrams(),
233263
stringWithStrong(),
234264
stringWithTagAndHashtag(),
235265
stringWithUnorderedList(),
266+
stringWithOrderedList(),
236267
stringWithHref(),
237268
stringWithBoldItalicUnderline(),
238269
stringWithImage(),

Demo/SnippetsViewController.swift

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,6 @@ class SnippetsViewController: UIViewController {
2323

2424
private var snippets = allSnippets()
2525

26-
init() {
27-
super.init(nibName: nil, bundle: nil)
28-
title = "Snippets"
29-
}
30-
31-
required init?(coder aDecoder: NSCoder) {
32-
fatalError("init(coder:) has not been implemented")
33-
}
34-
3526
override func viewDidLoad() {
3627
super.viewDidLoad()
3728
view.addSubview(tableView)

Sources/AttributedLabel.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,6 @@ open class AttributedLabel: UIView {
158158
return textView.sizeThatFits(size)
159159
}
160160

161-
open override var intrinsicContentSize: CGSize {
162-
return textView.intrinsicContentSize
163-
}
164-
165161
//MARK: - DetectionAreaButton
166162
private class DetectionAreaButton: UIButton {
167163

0 commit comments

Comments
 (0)