9 Notes for me in November Swift 4.1
Note: November-2018
1. Selection color for selected in tableView and collectView
-- " cell.selectionStyle = UITableViewCellSelectionStyle.none "
-- that code can change color if you select table view too like
-- " cell.selectionStyle = UITableViewCellSelectionStyle.blue ", ".red / .gray " .etc
2. Or in .xib/.nib setting selection color for selected in tableView and collectView.
--
-- Change selection from default to none.
--
3. For Update Carthage fast use cache.
-- $ carthage update --cache-builds --platform iOS
-- or
-- $ carthage update --platform iOS --cache-builds
4. Use navigation barbutton
-- Error: button bar cannot change color
-- Don't
--
--Do
--
5. Code "Swift" for Thumbnail video
func createThumbnailOfVideoFromRemoteUrl(url: URL) -> UIImage? {
let asset = AVAsset(url: url)
let assetImgGenerate = AVAssetImageGenerator(asset: asset)
assetImgGenerate.appliesPreferredTrackTransform = true
let time = CMTimeMakeWithSeconds(1.0, 600)
do {
let img = try assetImgGenerate.copyCGImage(at: time, actualTime: nil)
let thumbnail = UIImage(cgImage: img)
return thumbnail
} catch {
print(error.localizedDescription)
return nil
}
}
-- Note: for url from Api and local storage, if u want URL video from local storage, must get data from -- gallery .photolibrary --> video after didfinish and check URL .
-- example :
--- local: file:///var/mobile/Media/DCIM/100APPLE/IMG_0028.MOV
--- web / Api / cloud = https://img.google.com/video/Video_20181031_c2smyp.mp4
6. Count / limit character UITextView
-- How to use, component is UITextView, UILabel and use shouldChangeTextIn
-- example: limit character < 300.
public func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
let newText = (questionTextView.text as NSString).replacingCharacters(in: range, with: text)
let numberOfChars = newText.count
self.questionCountLabel.text = "\(300 - newText.count)" // questionCountLabel -> label show
return numberOfChars < 300
}
-- Note: for UITextField use shouldChangeCharactersIn
7. Simple Video Player use AVPlayerViewController
-- Remember to import AVKit, import AVFoundation, AVFoundation framework is needed even if you use AVPlayer
import UIKit
import AVKit
import AVFoundation
@IBAction func handleVideoPlayerr(_ sender: UIButton) {
let videoURL = "your_Url_Video"
let player = AVPlayer(url: videoURL)
let vc = AVPlayerViewController()
vc.player = player
present(vc, animated: true) {
vc.player?.play()
}
8. Count / limit Character For UITextField
-- This for UITextField use shouldChangeCharactersIn
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let newLength = (textField.text?.utf16.count)! + string.utf16.count - range.length
if newLength <= 16 {
countLabel.text = "\(16 - newLength)"
return true
} else {
return false
}
}
-- Full custom text field File
9. Condition if UIScrollView in bottom ?
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let height = scrollView.frame.size.height
let contentYoffset = scrollView.contentOffset.y
let distanceFromBottom = scrollView.contentSize.height - contentYoffset
if distanceFromBottom < height {
print(" you reached end of the table")
}
}
1. Selection color for selected in tableView and collectView
-- " cell.selectionStyle = UITableViewCellSelectionStyle.none "
-- that code can change color if you select table view too like
-- " cell.selectionStyle = UITableViewCellSelectionStyle.blue ", ".red / .gray " .etc
2. Or in .xib/.nib setting selection color for selected in tableView and collectView.
--
-- Change selection from default to none.
--
3. For Update Carthage fast use cache.
-- $ carthage update --cache-builds --platform iOS
-- or
-- $ carthage update --platform iOS --cache-builds
4. Use navigation barbutton
-- Error: button bar cannot change color
-- Don't
--
--Do
--
5. Code "Swift" for Thumbnail video
func createThumbnailOfVideoFromRemoteUrl(url: URL) -> UIImage? {
let asset = AVAsset(url: url)
let assetImgGenerate = AVAssetImageGenerator(asset: asset)
assetImgGenerate.appliesPreferredTrackTransform = true
let time = CMTimeMakeWithSeconds(1.0, 600)
do {
let img = try assetImgGenerate.copyCGImage(at: time, actualTime: nil)
let thumbnail = UIImage(cgImage: img)
return thumbnail
} catch {
print(error.localizedDescription)
return nil
}
}
-- Note: for url from Api and local storage, if u want URL video from local storage, must get data from -- gallery .photolibrary --> video after didfinish and check URL .
-- example :
--- local: file:///var/mobile/Media/DCIM/100APPLE/IMG_0028.MOV
--- web / Api / cloud = https://img.google.com/video/Video_20181031_c2smyp.mp4
6. Count / limit character UITextView
-- How to use, component is UITextView, UILabel and use shouldChangeTextIn
-- example: limit character < 300.
public func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
let newText = (questionTextView.text as NSString).replacingCharacters(in: range, with: text)
let numberOfChars = newText.count
self.questionCountLabel.text = "\(300 - newText.count)" // questionCountLabel -> label show
return numberOfChars < 300
}
-- Note: for UITextField use shouldChangeCharactersIn
7. Simple Video Player use AVPlayerViewController
-- Remember to import AVKit, import AVFoundation, AVFoundation framework is needed even if you use AVPlayer
import UIKit
import AVKit
import AVFoundation
@IBAction func handleVideoPlayerr(_ sender: UIButton) {
let videoURL = "your_Url_Video"
let player = AVPlayer(url: videoURL)
let vc = AVPlayerViewController()
vc.player = player
present(vc, animated: true) {
vc.player?.play()
}
8. Count / limit Character For UITextField
-- This for UITextField use shouldChangeCharactersIn
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let newLength = (textField.text?.utf16.count)! + string.utf16.count - range.length
if newLength <= 16 {
countLabel.text = "\(16 - newLength)"
return true
} else {
return false
}
}
-- Full custom text field File
9. Condition if UIScrollView in bottom ?
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let height = scrollView.frame.size.height
let contentYoffset = scrollView.contentOffset.y
let distanceFromBottom = scrollView.contentSize.height - contentYoffset
if distanceFromBottom < height {
print(" you reached end of the table")
}
}
No comments: