Skip to content
This repository has been archived by the owner on Sep 5, 2023. It is now read-only.

Commit

Permalink
Adjust format of NSAttributedString on AttributeCell
Browse files Browse the repository at this point in the history
  • Loading branch information
jncosideout committed Oct 28, 2020
1 parent e17c19a commit 46b7a92
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,31 +100,28 @@ class AttributeBLTNPageItem: BLTNPageItem {
var attributeView: AttributeView?

override func makeHeaderViews(with interfaceBuilder: BLTNInterfaceBuilder) -> [UIView]? {
if let attributeV = attributeView {
var views = [UIView]()

let viewB = UIView()
viewB.backgroundColor = iconImageBackgroundColor
let containerA = UIView()
containerA.backgroundColor = iconImageBackgroundColor

if let attributeV = attributeView {
viewB.addSubview(attributeV)
containerA.addSubview(attributeV)

constrain(attributeV, viewB) { (attributeV, viewB) in
viewB.width == attributeV.width
viewB.height == attributeV.height
attributeV.edges == viewB.edges
constrain(attributeV, containerA) { (attributeV, containerA) in
containerA.width == attributeV.width
containerA.height == attributeV.height
attributeV.edges == containerA.edges

}

var views: [UIView] = [viewB]
var views: [UIView] = [containerA]

if let attribute = attribute {
let descriptionLabel = UILabel()
descriptionLabel.numberOfLines = 0
descriptionLabel.textAlignment = .center
descriptionLabel.font = UIFont.boldSystemFont(ofSize: 17)

descriptionLabel.text = attribute.descriptionLong ?? attribute.descriptionShort ?? "empty descriptions"
descriptionLabel.text = attribute.descriptionLong ?? attribute.descriptionShort ?? "empty description"
views.append(descriptionLabel)
}
return views
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import Cartography
func configure(_ attribute: Attribute) {
self.layer.cornerRadius = 5
self.attribute = attribute
//setIconImageView(imageURL: attribute.iconUrl)
setIconImageView(imageURL: attribute.iconUrl)
if let label = attribute.name, let description = attribute.descriptionShort ?? attribute.title {
let text = formatAttributedText(label: label, description: description)
descriptionShort.attributedText = text
Expand Down Expand Up @@ -72,20 +72,26 @@ extension AttributeView: formatAttributedString {
var boldWordsPattern: String { return "(_\\w+_)" }

func formatAttributedText(label: String, description: String) -> NSMutableAttributedString? {
let headline = UIFont(descriptor: UIFontDescriptor.preferredFontDescriptor(withTextStyle: UIFont.TextStyle.headline), size: UIFontDescriptor.preferredFontDescriptor(withTextStyle: UIFont.TextStyle.headline).pointSize)

let value = NSAttributedString(string: description)
let headline = UIFont(descriptor: UIFontDescriptor.preferredFontDescriptor(withTextStyle: UIFont.TextStyle.headline), size: UIFontDescriptor.preferredFontDescriptor(withTextStyle: UIFont.TextStyle.caption1).pointSize)
var bold: [NSAttributedString.Key: Any] = [:]
if #available(iOS 13.0, *) {
bold = [NSAttributedString.Key.foregroundColor: UIColor.label, NSAttributedString.Key.font: headline]
} else {
bold = [NSAttributedString.Key.foregroundColor: UIColor.black, NSAttributedString.Key.font: headline]
}

let body = UIFont(descriptor: UIFontDescriptor.preferredFontDescriptor(withTextStyle: UIFont.TextStyle.body), size: UIFontDescriptor.preferredFontDescriptor(withTextStyle: UIFont.TextStyle.caption2).pointSize)
var regular: [NSAttributedString.Key: Any] = [:]
if #available(iOS 13.0, *) {
regular = [NSAttributedString.Key.foregroundColor: UIColor.label, NSAttributedString.Key.font: body]
} else {
regular = [NSAttributedString.Key.foregroundColor: UIColor.black, NSAttributedString.Key.font: body]
}
let combination = NSMutableAttributedString()
combination.append(NSAttributedString(string: label + "\n", attributes: bold))

combination.append(makeWordsBold(for: value))
let descrip = NSAttributedString(string: description, attributes: regular)
combination.append(descrip)

return combination
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
</subviews>
<color key="backgroundColor" systemColor="systemBackgroundColor" cocoaTouchSystemColor="whiteColor"/>
</stackView>
<imageView clipsSubviews="YES" userInteractionEnabled="NO" contentMode="scaleAspectFit" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="nutriscore-c" translatesAutoresizingMaskIntoConstraints="NO" id="Ngw-Ux-P6K">
<imageView clipsSubviews="YES" userInteractionEnabled="NO" contentMode="scaleAspectFit" horizontalHuggingPriority="251" verticalHuggingPriority="251" translatesAutoresizingMaskIntoConstraints="NO" id="Ngw-Ux-P6K">
<rect key="frame" x="5" y="5" width="100" height="50"/>
<constraints>
<constraint firstAttribute="width" constant="100" id="MQ6-td-zb5"/>
Expand All @@ -54,7 +54,4 @@
<point key="canvasLocation" x="-56.5" y="-226.5"/>
</view>
</objects>
<resources>
<image name="nutriscore-c" width="389" height="210"/>
</resources>
</document>

0 comments on commit 46b7a92

Please sign in to comment.