Skip to content

Repository files navigation

DSFMaxLengthDisplayTextField

Swift Package Manager

An NSTextField that specifies a maximum count after which text is highlighted to indicate an overflow

animated demo

Multiline

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

12345678910111213
a=🧖🏼‍♀️,b=💆‍♂️,c=🙆🏾

Settable properties

PropertyTypeDescription
maxCharactersIntThe maximum number of characters allowed
overflowTextColorNSColorThe color to use for 'overflow' characters
overflowTextBackgroundColorNSColorThe background color to use for 'overflow' characters
underlineOverflowCharactersBoolWhether to underline overflow characters

Observable properties

These properties are KVO observable.

PropertyTypeDescription
isValidBoolDoes the text fit within the maximum characters specified
characterCountIntTotal character count
charactersAvailableIntThe number of characters available before overflow occurs
overflowCharacterCountIntThe number of overflow characters

Callbacks

ParameterTypeDescription
contentChangedCallback() -> VoidCalled when the content changes
isValidChangedCallback(Bool) -> VoidCalled when the validity changes

Combine (macOS 10.15+)

ParameterDescription
validityPublisherPublishes when the validity of the field changes
Combine Example Usage
@IBOutlet weak vartitleField:DSFMaxLengthDisplayTextField!...
titleField.validityPublisher
.removeDuplicates().sink{ newValue inSwift.print("Title Validity changed: \(newValue)")}

License

MIT License
Copyright (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.

About

An `NSTextField` that specifies a maximum count after which text is highlighted to indicate an overflow

Topics

Resources

Stars

6 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages