Friday, February 22, 2019

Swift read poem app

Copy poems in notepad and save to txt file with UTF-8 Encoding.
Drags files in left tree folder project, check Copy Items is needed.
Declare a label, tableView, imageview, array poems ‘s name.
var tableView: UITableView!
var qua2=["Bài một","Bài hai","Bài ba","Bài bốn","Bài năm","Bài sáu","Bài bảy","Bài tám"]
let image = UIImage(named: "hoa")
var ima: UIImageView!
var la: UILabel!
Set location, color text, add to view in viewDidLoad.
let c = UIScreen.mainScreen().bounds.size.height
la = UILabel(frame: CGRect(x: 120, y: 10, width: 200, height: 30))
let t = "My poems"
let a=NSMutableAttributedString(string: t, attributes: [NSForegroundColorAttributeName: UIColor.orangeColor(), NSFontAttributeName: UIFont(name: "Georgia", size: 18.0)!])
la.attributedText = a
tableView = UITableView(frame: CGRect(x: 10, y: 70, width: 325, height: c-50))
let the = tableView
        the.registerClass(UITableViewCell.classForCoder(), forCellReuseIdentifier: "iden")
        the.dataSource = self
        the.delegate = self
view.backgroundColor = UIColor.whiteColor()
view.addSubview(la)
view.addSubview(the)
Copy function to create tableView to outside viewDidLoad.
func numberOfSections(in tableView: UITableView) ->Int {
return1
    }
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) ->Int {
return qua2.count
    }
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("iden", forIndexPath: indexPath)
        cell.textLabel?.text = qua2[indexPath.row]
        cell.textLabel?.font = UIFont(name:"Futura", size:18)
        cell.textLabel?.textColor=UIColor.blueColor()
        cell.imageView?.image = image
        cell.accessoryType = .DisclosureIndicator
return cell
    }
func tableView(tableView: UITableView,
                   didSelectRowAtIndexPath indexPath: NSIndexPath) {
let row = qua2[indexPath.row]
let so = indexPath.row
let vc = fo()
        vc.so=so
        vc.tenbai=row
self.presentViewController(vc, animated: true, completion: nil)
    }
See last function, we pass poems ‘s name and number line to reading class.
Copy this function to remove status bar.
Override  func prefersStatusBarHidden() ->Bool {
return true
    }
Run to see result.
Create new class to read text, name it fo.
Declare 2 label to show name of poem, author, 1 textView to show poem, imageview to set background, 2 variable to receive data pass through.
var so=0
var tenbai = " "
let im = UIImage(named: "nen")
var la: UILabel!
var la2: UILabel!
var ima: UIImageView!
var gi:UITextView!
var ar = ["t1","t2","t3","t4","t5","t6","t7","t8"]
var tacgia = ["Teika","Jakuren","Komachi","Saigyo","Ise","Shunzei","Shotetsu","Sosei"]
Set location in viewDidLoad.
let c = UIScreen.mainScreen().bounds.size.height
let r = UIScreen.mainScreen().bounds.size.width
la = UILabel(frame: CGRect(x: 80, y: 80, width: 100, height: 30))
la2 = UILabel(frame: CGRect(x: 140, y: 250, width: 100, height: 30))
la.text = tenbai
la2.text = tacgia[so]
la2.textColor = UIColor.darkGrayColor()
la.textColor = UIColor.magentaColor()
ima = UIImageView(frame: CGRect(x: 0, y: 0, width: r, height: c))
ima.image = im
gi = UITextView(frame: CGRect(x:50, y: 120, width: 250, height: 160))
view.backgroundColor = UIColor.whiteColor()
la.center = CGPoint(x: view.center.x, y:90)
We add imageview to make it become background picture.
view.addSubview(ima)
view.addSubview(gi)
view.addSubview(la)
view.addSubview(la2)
Copy these lines to read file txt in to viewDidLoad.
let path2 = NSBundle.mainBundle().pathForResource(ar[so], ofType: "txt")!
var rea2 : String = ""
var  t = ""
do {
try rea2 = NSString(contentsOfFile: path2, encoding: NSUTF8StringEncoding) asString
            t=rea2
   }
catchlet error asNSError {
// print("ERROR : reading from file \(fileName) : \(error.localizedDescription)")
        }
gi.text = t
gi.font = UIFont.italicSystemFontOfSize(17)
gi.textColor=UIColor.blueColor()
gi.textAlignment = NSTextAlignment.Center
gi.backgroundColor=UIColor.clearColor()
We set text to textView, color text, make it transparent so we can see background picture.
Now we want to swipe back to previous screen, copy to outside viewDidLoad.
func tap (g:UIGestureRecognizer) {
self.dismissViewControllerAnimated(true, completion: nil)
}
In side viewDidLoad add these lines.
let t2 = UIPanGestureRecognizer(target:self, action:#selector(fo.tap(_:)))
view.addGestureRecognizer(t2)

Run to see result.

No comments:

Post a Comment