String+Extenstion.swift 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. //
  2. // String+Extenstion.swift
  3. // o2app
  4. //
  5. // Created by 刘振兴 on 2017/8/18.
  6. // Copyright © 2017年 zone. All rights reserved.
  7. //
  8. import Foundation
  9. import UIKit
  10. extension String {
  11. func subString(from: Int, to: Int? = nil) -> String {
  12. if from >= self.length {
  13. return self
  14. }
  15. let startIndex = self.index(self.startIndex, offsetBy: from)
  16. if to == nil {
  17. return String(self[startIndex..<self.endIndex])
  18. }else {
  19. if from >= to! {
  20. return String(self[startIndex..<self.endIndex])
  21. }else {
  22. let endIndex = index(self.startIndex, offsetBy: to!)
  23. return String(self[startIndex..<endIndex])
  24. }
  25. }
  26. }
  27. /// 计算文本的高度
  28. func textHeight(fontSize: CGFloat, width: CGFloat) -> CGFloat {
  29. return self.boundingRect(with: CGSize(width: width, height: CGFloat(MAXFLOAT)), options: .usesLineFragmentOrigin, attributes: [.font: UIFont.systemFont(ofSize: fontSize)], context: nil).size.height
  30. }
  31. // MARK: - URL允许的字符
  32. var urlEscaped: String {
  33. return self.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)!
  34. }
  35. // MARK:- 获取字符串的CGSize
  36. func getSize(with fontSize: CGFloat) -> CGSize {
  37. let str = self as NSString
  38. let size = CGSize(width: UIScreen.main.bounds.width, height: CGFloat(MAXFLOAT))
  39. return str.boundingRect(with: size, options: .usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: fontSize)], context: nil).size
  40. }
  41. // MARK:- 获取文本图片
  42. func getTextImage(_ size:CGSize,textColor tColor:UIColor,backColor bColor:UIColor,textFont tFont:UIFont) -> UIImage? {
  43. let label = UILabel(frame: CGRect(origin:CGPoint(x:0,y:0), size: size))
  44. label.textAlignment = .center
  45. label.textColor = tColor
  46. label.font = tFont
  47. label.text = self
  48. label.backgroundColor = bColor
  49. UIGraphicsBeginImageContextWithOptions(label.frame.size, true, 0)
  50. guard let context = UIGraphicsGetCurrentContext() else { return nil }
  51. label.layer.render(in: context)
  52. let image = UIGraphicsGetImageFromCurrentImageContext()
  53. UIGraphicsEndImageContext()
  54. return image
  55. }
  56. subscript(r: Range<Int>) -> String {
  57. get {
  58. let startIndex = self.index(self.startIndex, offsetBy: r.lowerBound)
  59. let endIndex = self.index(self.startIndex, offsetBy: r.upperBound)
  60. return String(self[startIndex..<endIndex])
  61. }
  62. }
  63. subscript(r: ClosedRange<Int>) -> String {
  64. get {
  65. let startIndex = self.index(self.startIndex, offsetBy: r.lowerBound)
  66. let endIndex = self.index(self.startIndex, offsetBy: r.upperBound)
  67. return String(self[startIndex...endIndex])
  68. }
  69. }
  70. static func randomString(length:Int) -> String {
  71. let charSet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
  72. var c = charSet.map { String($0) }
  73. var s:String = ""
  74. for _ in (1...length) {
  75. s.append(c[Int(arc4random()) % c.count])
  76. }
  77. return s
  78. }
  79. }