-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcustomText.swift
67 lines (59 loc) · 2.62 KB
/
customText.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//
// customText.swift
//
// Created by Nicolò Candiani on 03/09/17.
//
import UIKit
extension String{
func customText() -> NSMutableAttributedString {
let fontSuper:UIFont? = UIFont(name: "Helvetica", size:10)
let font = UIFont(name: "Helvetica", size: 18)
let attString:NSMutableAttributedString = NSMutableAttributedString(string: self, attributes: [NSAttributedStringKey.font:font!])
var indexA = Array(repeating: 0, count: 10)
var indexB = Array(repeating: 0, count: 10)
var indexC = Array(repeating: 0, count: 10)
var indexD = Array(repeating: 0, count: 10)
var x = 0
var z = 0
var y = 0
var w = 0
for a in 0..<self.count{
let index = self.index(self.startIndex, offsetBy: a)
if self[index] == "{"{
indexA[x] = a
debugPrint(indexA[x])
x+=1
}
if self[index] == "}"{
indexB[z] = a
debugPrint(indexB[z])
z+=1
}
if self[index] == "£"{
indexC[y] = a
y+=1
}
if self[index] == "$"{
indexD[w] = a
w+=1
}
}
for a in 0..<10{
if indexA[a] != 0 || indexB[a] != 0 {
for b in indexA[a]+1..<indexB[a]{
attString.setAttributes([NSAttributedStringKey.font:fontSuper!,NSAttributedStringKey.baselineOffset:10], range: NSRange(location:b,length:1))
}
}
if indexC[a] != 0 || indexD[a] != 0 {
for b in indexC[a]+1..<indexD[a]{
attString.setAttributes([NSAttributedStringKey.font:fontSuper!,NSAttributedStringKey.baselineOffset:-5], range: NSRange(location:b,length:1))
}
}
}
attString.mutableString.replaceOccurrences(of: "{", with: "", options: NSString.CompareOptions.caseInsensitive, range: NSRange(location: 0, length: attString.length))
attString.mutableString.replaceOccurrences(of: "}", with: "", options: NSString.CompareOptions.caseInsensitive, range: NSRange(location: 0, length: attString.length))
attString.mutableString.replaceOccurrences(of: "£", with: "", options: NSString.CompareOptions.caseInsensitive, range: NSRange(location: 0, length: attString.length))
attString.mutableString.replaceOccurrences(of: "$", with: "", options: NSString.CompareOptions.caseInsensitive, range: NSRange(location: 0, length: attString.length))
return attString
}
}