Skip to content

Commit

Permalink
Fix scrolling and trim list items
Browse files Browse the repository at this point in the history
  • Loading branch information
An Wu committed Apr 13, 2016
1 parent 6702c4a commit dd17db5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
4 changes: 2 additions & 2 deletions MusicBook/MusicToolbox/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
<rect key="frame" x="0.0" y="28" width="600" height="60"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="xtf-yQ-2XS" id="Y3E-EI-Gy8">
<rect key="frame" x="0.0" y="0.0" width="600" height="59.5"/>
<rect key="frame" x="0.0" y="0.0" width="600" height="60"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" ambiguous="YES" misplaced="YES" text="Explanation" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="0wC-7O-F8r">
Expand Down Expand Up @@ -108,7 +108,7 @@
<textView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" editable="NO" selectable="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Mvd-0E-vXG">
<rect key="frame" x="20" y="64" width="560" height="536"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<mutableString key="text">Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda.</mutableString>
<string key="text">Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda.</string>
<fontDescription key="fontDescription" type="system" weight="light" pointSize="15"/>
<textInputTraits key="textInputTraits" autocapitalizationType="sentences"/>
</textView>
Expand Down
22 changes: 20 additions & 2 deletions MusicBook/MusicToolbox/WordDetailViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,35 @@ class WordDetailViewController: UIViewController {

self.navigationItem.title = word.spelling!

explanationBox.textContainerInset = UIEdgeInsetsMake(20, 30, 0, 30)
explanationBox.textContainerInset = UIEdgeInsetsMake(15, 30, 0, 30)

let mutableAttributedString = NSMutableAttributedString()
word.explanations!.enumerate().forEach({ (index, explanationObj) in
let explanation = explanationObj as! WordExplanation
let attributedString = try? NSAttributedString(data: ("\(index + 1). " + explanation.encodedStr! + "<br /><br />" + "<style>* {font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif; font-size: 16px; line-height: 150%;}</style>").dataUsingEncoding(NSUTF8StringEncoding)!, options:[NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: NSUTF8StringEncoding], documentAttributes: nil)

let attributedString = try? NSAttributedString(data: ("\(index + 1). " + cleanEncodedStr(explanation.encodedStr!) + "<br /><br />" + "<style>* {font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif; font-size: 16px; line-height: 150%;}</style>").dataUsingEncoding(NSUTF8StringEncoding)!, options:[NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: NSUTF8StringEncoding], documentAttributes: nil)
mutableAttributedString.appendAttributedString(attributedString!)
})

explanationBox.attributedText = mutableAttributedString
explanationBox.showsVerticalScrollIndicator = false
explanationBox.scrollEnabled = false
}

func cleanEncodedStr(encodedStr: String) -> String {

return encodedStr.componentsSeparatedByString("<li>").count == 2 ?
encodedStr
.stringByReplacingOccurrencesOfString("<li>", withString: "")
.stringByReplacingOccurrencesOfString("</li>", withString: "")
.stringByReplacingOccurrencesOfString("<ul>", withString: "")
.stringByReplacingOccurrencesOfString("</ul>", withString: "")
: encodedStr

}

override func viewDidAppear(animated: Bool) {
explanationBox.scrollEnabled = true
}

override func didReceiveMemoryWarning() {
Expand Down

0 comments on commit dd17db5

Please sign in to comment.