Skip to content

Commit b7b55b5

Browse files
committed
[NSMutableAttributedString] setColorForText(_:_)
1 parent b049f27 commit b7b55b5

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

DJSwiftHelpers.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
6220B4C325ADDDC40064B9EB /* Collection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6220B4C225ADDDC40064B9EB /* Collection.swift */; };
2929
6220B4C425ADDDC40064B9EB /* Collection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6220B4C225ADDDC40064B9EB /* Collection.swift */; };
3030
6220B4C525ADDDC40064B9EB /* Collection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6220B4C225ADDDC40064B9EB /* Collection.swift */; };
31+
6241494328F338EB00161522 /* NSMutableAttributedString.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6241494228F338EB00161522 /* NSMutableAttributedString.swift */; };
3132
625380722594BCE900C58B2F /* CLLocationCoordinate2D.swift in Sources */ = {isa = PBXBuildFile; fileRef = 625380712594BCE900C58B2F /* CLLocationCoordinate2D.swift */; };
3233
625380762594BDDD00C58B2F /* CLLocationCoordinate2D.swift in Sources */ = {isa = PBXBuildFile; fileRef = 625380712594BCE900C58B2F /* CLLocationCoordinate2D.swift */; };
3334
6253807A2594BDDD00C58B2F /* CLLocationCoordinate2D.swift in Sources */ = {isa = PBXBuildFile; fileRef = 625380712594BCE900C58B2F /* CLLocationCoordinate2D.swift */; };
@@ -104,6 +105,7 @@
104105
620F6A4D250BBCE900B10032 /* UserDefaults.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UserDefaults.swift; sourceTree = "<group>"; };
105106
621B3C35255D467000A5E77F /* DJSwiftHelpers_Extension.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = DJSwiftHelpers_Extension.framework; sourceTree = BUILT_PRODUCTS_DIR; };
106107
6220B4C225ADDDC40064B9EB /* Collection.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Collection.swift; sourceTree = "<group>"; };
108+
6241494228F338EB00161522 /* NSMutableAttributedString.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NSMutableAttributedString.swift; sourceTree = "<group>"; };
107109
625380712594BCE900C58B2F /* CLLocationCoordinate2D.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CLLocationCoordinate2D.swift; sourceTree = "<group>"; };
108110
625380812594CE9B00C58B2F /* Double.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Double.swift; sourceTree = "<group>"; };
109111
6266547E25F2765C00696303 /* DJSwiftHelpers.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = DJSwiftHelpers.framework; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -234,6 +236,7 @@
234236
isa = PBXGroup;
235237
children = (
236238
62BFAA93273ECDE600DAC879 /* Gradient.swift */,
239+
6241494228F338EB00161522 /* NSMutableAttributedString.swift */,
237240
62E392372710620F008C9C89 /* UIActivity.swift */,
238241
6267B87826A07017009A31B1 /* UIApplication.swift */,
239242
6278E8432861A8590004DF29 /* UIColor.swift */,
@@ -438,6 +441,7 @@
438441
isa = PBXSourcesBuildPhase;
439442
buildActionMask = 2147483647;
440443
files = (
444+
6241494328F338EB00161522 /* NSMutableAttributedString.swift in Sources */,
441445
62F315EB27A186F0003D2906 /* Task.swift in Sources */,
442446
6267B87C26A07017009A31B1 /* UIApplication.swift in Sources */,
443447
620F6A39250BB78200B10032 /* String.swift in Sources */,

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,15 @@ Return the emoji flag for the given Locale
198198
var emojiFlag: String
199199
```
200200

201+
### NSMutableAttributedString
202+
203+
Change the colour of certain text within an `NSMutableAttributedString`
204+
205+
```swift
206+
func setColorForText(textToFind: String, withColor color: UIColor)
207+
```
208+
209+
201210
### SafariActivityView
202211

203212
A SwiftUI view to display the share sheet with `Open in Safari`
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//
2+
// NSMutableAttributedString.swift
3+
// DJSwiftHelpers
4+
//
5+
// Created by Darren Jones on 09/10/2022.
6+
// Copyright © 2022 Dappological Ltd. All rights reserved.
7+
//
8+
9+
#if os(iOS)
10+
import UIKit
11+
12+
#if !IS_EXTENSION
13+
public
14+
extension NSMutableAttributedString {
15+
16+
/**
17+
Change the colour of text within an `NSMutableAttributedString`
18+
- Parameters:
19+
- textToFind: The text that you want to colour
20+
- withColour: The `UIColor` you want to colour the text
21+
*/
22+
func setColorForText(textToFind: String, withColor color: UIColor) {
23+
let range: NSRange = self.mutableString.range(of: textToFind, options: .caseInsensitive)
24+
self.addAttribute(NSAttributedString.Key.foregroundColor, value: color, range: range)
25+
}
26+
}
27+
#endif // !IS_EXTENSION
28+
#endif // os(iOS)

0 commit comments

Comments
 (0)