|
1 | 1 | # DSFMaxLengthDisplayTextField
|
2 | 2 |
|
3 |
| -A description of this package. |
| 3 | +<p align="center"> |
| 4 | + <img src="https://img.shields.io/github/v/tag/dagronf/DSFMaxLengthDisplayTextField" /> |
| 5 | + <img src="https://img.shields.io/badge/macOS-10.11+-red" /> |
| 6 | + <img src="https://img.shields.io/badge/License-MIT-lightgrey" /> |
| 7 | + <a href="https://swift.org/package-manager"> |
| 8 | + <img src="https://img.shields.io/badge/spm-compatible-brightgreen.svg?style=flat" alt="Swift Package Manager" /> |
| 9 | + </a> |
| 10 | +</p> |
| 11 | +<p align="center"> |
| 12 | + |
| 13 | +An `NSTextField` that specifies a maximum count after which text is highlighted to indicate an overflow |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | +## Why? |
| 20 | + |
| 21 | +I always liked the Twitter approach for indicating that text was oversized - it allows you to easily curate your content appropriately to fit the available size. |
| 22 | + |
| 23 | +## API |
| 24 | + |
| 25 | +This control uses the concept of a "grapheme cluster" for its character count - this means that emojis/complex characters represent a single character within the string |
| 26 | + |
| 27 | +For example, the string `a=🧖🏼♀️, b=💆♂️, c=🙆🏾` represents 13 characters |
| 28 | + |
| 29 | +| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | |
| 30 | +|----|----|----|----|----|----|----|----|----|----|----|----|----| |
| 31 | +| a | = | 🧖🏼♀️ | , | | b | = | 💆♂️ | , | | c | = | 🙆🏾 | |
| 32 | + |
| 33 | +<!-- **Note:** In UTF8, this length is 48, UTF16 the length is 26 --> |
| 34 | + |
| 35 | +### Settable properties |
| 36 | + |
| 37 | +| Property | Type | Description | |
| 38 | +|-------------------------------|-----------|-------------------------------------------------------| |
| 39 | +| `maxCharacters` | `Int` | The maximum number of characters allowed | |
| 40 | +| `overflowTextColor` | `NSColor` | The color to use for 'overflow' characters | |
| 41 | +| `overflowTextBackgroundColor` | `NSColor` | The background color to use for 'overflow' characters | |
| 42 | +| `underlineOverflowCharacters` | `Bool` | Whether to underline overflow characters | |
| 43 | + |
| 44 | +### Observable properties |
| 45 | + |
| 46 | +These properties are KVO observable. |
| 47 | + |
| 48 | +| Property | Type | Description | |
| 49 | +|-------------------------------|--------|-----------------------------------------------------------| |
| 50 | +| `isValid` | `Bool` | Does the text fit within the maximum characters specified | |
| 51 | +| `characterCount` | `Int` | Total character count | |
| 52 | +| `charactersAvailable` | `Int` | The number of characters available before overflow occurs | |
| 53 | +| `overflowCharacterCount` | `Int` | The number of overflow characters | |
| 54 | + |
| 55 | +### Callbacks |
| 56 | + |
| 57 | +| Parameter | Type | Description | |
| 58 | +|----------------------------|------------------|------------------------------------| |
| 59 | +| `contentChangedCallback` | `() -> Void` | Called when the content changes | |
| 60 | +| `isValidChangedCallback` | `(Bool) -> Void` | Called when the validity changes | |
| 61 | + |
| 62 | +### Combine (macOS 10.15+) |
| 63 | + |
| 64 | +| Parameter | Description | |
| 65 | +|----------------------|--------------------------------------------------| |
| 66 | +| `validityPublisher` | Publishes when the validity of the field changes | |
| 67 | + |
| 68 | +<details> |
| 69 | +<summary>Combine Example Usage</summary> |
| 70 | + |
| 71 | +```swift |
| 72 | +@IBOutlet weak var titleField: DSFMaxLengthDisplayTextField! |
| 73 | +... |
| 74 | +titleField.validityPublisher |
| 75 | + .removeDuplicates() |
| 76 | + .sink { newValue in |
| 77 | + Swift.print("Title Validity changed: \(newValue)") |
| 78 | + } |
| 79 | +``` |
| 80 | + |
| 81 | +</details> |
| 82 | + |
| 83 | +## License |
| 84 | + |
| 85 | +``` |
| 86 | +MIT License |
| 87 | +
|
| 88 | +Copyright (c) 2022 Darren Ford |
| 89 | +
|
| 90 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 91 | +of this software and associated documentation files (the "Software"), to deal |
| 92 | +in the Software without restriction, including without limitation the rights |
| 93 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 94 | +copies of the Software, and to permit persons to whom the Software is |
| 95 | +furnished to do so, subject to the following conditions: |
| 96 | +
|
| 97 | +The above copyright notice and this permission notice shall be included in all |
| 98 | +copies or substantial portions of the Software. |
| 99 | +
|
| 100 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 101 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 102 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 103 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 104 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 105 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 106 | +SOFTWARE. |
| 107 | +``` |
0 commit comments