https://github.com/dagronf/dsfmaxlengthdisplaytextfield
An `NSTextField` that specifies a maximum count after which text is highlighted to indicate an overflow
https://github.com/dagronf/dsfmaxlengthdisplaytextfield
macos maxlength nstextfield twitter
Last synced: 2 months ago
JSON representation
An `NSTextField` that specifies a maximum count after which text is highlighted to indicate an overflow
- Host: GitHub
- URL: https://github.com/dagronf/dsfmaxlengthdisplaytextfield
- Owner: dagronf
- Created: 2022-06-17T09:14:19.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2022-08-24T09:52:51.000Z (almost 3 years ago)
- Last Synced: 2025-03-23T04:31:40.684Z (2 months ago)
- Topics: macos, maxlength, nstextfield, twitter
- Language: Swift
- Homepage:
- Size: 198 KB
- Stars: 5
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# DSFMaxLengthDisplayTextField
An `NSTextField` that specifies a maximum count after which text is highlighted to indicate an overflow


## Why?
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.
## API
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
For example, the string `a=🧖🏼♀️, b=💆♂️, c=🙆🏾` represents 13 characters
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 |
|----|----|----|----|----|----|----|----|----|----|----|----|----|
| a | = | 🧖🏼♀️ | , | | b | = | 💆♂️ | , | | c | = | 🙆🏾 |### Settable properties
| Property | Type | Description |
|-------------------------------|-----------|-------------------------------------------------------|
| `maxCharacters` | `Int` | The maximum number of characters allowed |
| `overflowTextColor` | `NSColor` | The color to use for 'overflow' characters |
| `overflowTextBackgroundColor` | `NSColor` | The background color to use for 'overflow' characters |
| `underlineOverflowCharacters` | `Bool` | Whether to underline overflow characters |### Observable properties
These properties are KVO observable.
| Property | Type | Description |
|-------------------------------|--------|-----------------------------------------------------------|
| `isValid` | `Bool` | Does the text fit within the maximum characters specified |
| `characterCount` | `Int` | Total character count |
| `charactersAvailable` | `Int` | The number of characters available before overflow occurs |
| `overflowCharacterCount` | `Int` | The number of overflow characters |### Callbacks
| Parameter | Type | Description |
|----------------------------|------------------|------------------------------------|
| `contentChangedCallback` | `() -> Void` | Called when the content changes |
| `isValidChangedCallback` | `(Bool) -> Void` | Called when the validity changes |### Combine (macOS 10.15+)
| Parameter | Description |
|----------------------|--------------------------------------------------|
| `validityPublisher` | Publishes when the validity of the field changes |Combine Example Usage
```swift
@IBOutlet weak var titleField: DSFMaxLengthDisplayTextField!
...
titleField.validityPublisher
.removeDuplicates()
.sink { newValue in
Swift.print("Title Validity changed: \(newValue)")
}
```## License
```
MIT LicenseCopyright (c) 2022 Darren Ford
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
```