-
Notifications
You must be signed in to change notification settings - Fork 0
/
UILabelExtension.swift
37 lines (29 loc) · 1.38 KB
/
UILabelExtension.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
//
// UILabelExtension.swift
// Copyrobo
//
// Created by imac on 7/9/18.
// Copyright © 2018 CopyRobo. All rights reserved.
//
import UIKit
extension UILabel {
func boldSubString(searchText: String) {
guard let labelText = self.text else {
return
}
// bold attribute
let boldAttr = [NSAttributedStringKey.font: UIFont.boldSystemFont(ofSize: self.font.pointSize)]
// check if label text contains search text
if let matchRange: Range = labelText.lowercased().range(of: searchText.lowercased()) {
// get range start/length because NSMutableAttributedString.setAttributes() needs NSRange not Range<String.Index>
let matchRangeStart: Int = labelText.distance(from: labelText.startIndex, to: matchRange.lowerBound)
let matchRangeEnd: Int = labelText.distance(from: labelText.startIndex, to: matchRange.upperBound)
let matchRangeLength: Int = matchRangeEnd - matchRangeStart
// create mutable attributed string & bold matching part
let newLabelText = NSMutableAttributedString(string: labelText)
newLabelText.setAttributes(boldAttr, range: NSMakeRange(matchRangeStart, matchRangeLength))
// set label attributed text
self.attributedText = newLabelText
}
}
}