GithubHelp home page GithubHelp logo

glaszig / sztextview Goto Github PK

View Code? Open in Web Editor NEW
681.0 19.0 102.0 151 KB

A drop-in UITextView replacement which gives you: a placeholder.

License: MIT License

Objective-C 94.01% Ruby 3.19% Swift 2.80%
objective-c

sztextview's Introduction

SZTextView

Carthage compatible

A drop-in UITextView replacement which gives you: a placeholder.
Technically it differs from other solutions in that it tries to work like UITextField's private _placeholderLabel so you should not suffer ugly glitches like jumping text views or loads of custom drawing code.

Requirements

An iOS project.

Installation

Either clone this repo and add the project to your Xcode workspace, use CocoaPods or Carthage.

CocoaPods

Add this to you Podfile:

pod 'SZTextView'

Carthage

Add this line to your Cartfile:

github "glaszig/SZTextView"

Usage

SZTextView *textView = [SZTextView new];
textView.placeholder = @"Enter lorem ipsum here";
textView.placeholderTextColor = [UIColor lightGrayColor];
textView.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:18.0];

Analogously you can use the attributedPlaceholder property to set a fancy NSAttributedString as the placeholder:

NSMutableAttributedString *placeholder = [[NSMutableAttributedString alloc] initWithString:@"Enter lorem ipsum here"];
[placeholder addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,2)];
[placeholder addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(2,4)];
[placeholder addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(6,4)];

textView.attributedPlaceholder = placeholder;

Both properties placeholder and attributedPlaceholder are made to stay in sync. If you set an attributedPlaceholder and afterwards set placeholder to something else, the set text gets copied to the attributedPlaceholder while trying to keep the original text attributes.
Also, placeholder will be set to attributedPlaceholder.string when using the attributedPlaceholder setter.

A simple demo and a few unit tests are included.

Animation

The placeholder is animatable. Just configure the double property fadeTime to the seconds you'd like the animation to take.

User Defined Runtime Attributes

If you prefer using Interface Builder to configure your UI, you can use UDRA's to set values for placeholder and placeholderTextColor.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

License

Published under the MIT license.

Note

I've developed this component for Cocktailicious. You should check it out *shamelessplug*.
Please let me now if and how you use this component. I'm curious.

sztextview's People

Contributors

adrian2112 avatar ahalls avatar benlachman avatar dark2torch avatar dependabot[bot] avatar erf avatar fannt avatar glaszig avatar jasondinh avatar junjie avatar msmollin avatar paulz avatar ryanbertrand avatar tcirwin avatar vlas-voloshin avatar yazzpro avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

sztextview's Issues

selectable cause crash

self.textView.selectable = NO;
And add a button to set textView text directly,like below

  • (IBAction)changeAction:(id)sender {
    self.textView.text = @"sjldfjlksjdflksjdf";
    }

This will be crashed and the console log:
2015-11-24 22:42:24.134 SZTextView[3336:137753] keyPath = attributedText, change = {
kind = 1;
new = "";
}
2015-11-24 22:42:24.134 SZTextView[3336:137753] keyPath = text, change = {
kind = 1;
new = sjldfjlksjdflksjdf;
}
This can be solved but I don't know it's good or not:

    if (![newAttributedText isEqual:[NSNull null]]) {
        [self setPlaceholderVisibleForText:newAttributedText.string];
    }

Crash after become first responder

Exception -[NSConcreteTextStorage attributesAtIndex:effectiveRange:]: Range or index out of bounds raises when -becomeFirstResponder was invoked.
I got this crash report from Crashlytics but could never reproduce...
Any ideas please?

AttributedPlaceholder foregroundColor property overwritten

Problem:
It seems that when layouting a cell with a SZTextView in it before it's displayed for the first time, the foregroundColor of the AttributedPlaceholder is overwritten by the library.

Temporary solution:
Changing the property again after layoutSubviews() is called.

Details:
Bug found using iOS 13 and Xcode 11.4 (11E146) using an iPhone 8

When input

This is an useful project, but when i use it and input Chinese, the scrollView scroll to wrong place

textContainerInset is available after iOS 7

UITextView.h
// Inset the text container's layout area within the text view's content area
@Property(nonatomic, assign) UIEdgeInsets textContainerInset NS_AVAILABLE_IOS(7_0);

But, it is not concerned in SZTextView.

Word wrapping not working for the placeholder

Your code is not wordwrapping the placeholder text. I set my Placeholder text as "Why do you recommend this product?". But as you see in the image, it's getting cut off at "Why do you recommend this".

Can you please fix this.

image

Placeholder alignment

The placeholder property is ignoring the textAlignment property.

Update: The only way to make this work right now is to use attributedPlaceholder along with NSParagraphStyle.

Usage with Interface Builder

I am using SZTextView with CocoaPods in one of the projects I am working on. I added the SZTextView via InterfaceBuilder in a XIB.

When I build the app, I am getting the following warnings with a red exclamation mark. My XCode Version is 6.3(6D570).

error: IB Designables: Failed to render instance of SZTextView: Failed to load designables from path (null)
error: IB Designables: Failed to update auto layout status: Failed to load designables from path (null)

Please suggest.

Input marker set back to index 0 when autocomplete is active and textView is touched

Having an issue as described in the subject line. If you start inputting a word and autocorrect offers a suggestion, tap the textview again and it will always move the cursor index back to 0.

For example:

  1. Receive focus to text view.
  2. Start typing "Cj".
  3. Autocomplete should offer "Check" for word suggestion.
  4. Tap text view again to "accept" recommendation.

Result:

"Check" appears in the text view, but the cursor index is set back to the beginning.

use of attributedText = crash

Hi,

First thank you for your work on that great drop in replacement !
But for compatibility purposes can it still work when we try to assign a string value to attributedText ?

self.textView.text = "Simple text" // work
self.textView.attributedText = NSAttributedString(string: "NSAttributedString") // crash

probably because the all placeholder, kvo... is based on that attributed string ?
Well that would be really nice of you :)

Cheers

Failed to load designable from path(null)

I use your lib in storyboard (with iO7 and Objective-C) when i add your controller and imbue class to SZTextView i got this error

Base.lproj/Main.storyboard: error: IB Designables: Failed to update auto layout status: Failed to load designables from path (null)

Base.lproj/Main.storyboard: error: IB Designables: Failed to render instance of SZTextView: Failed to load designables from path (null)

how to fix it?

_placeholderTextView contentInset and contentOffset

My project needs an UITextView with placeholder and header view.
So, I start to use SZTextView for placeholder.
To make a space for adding header view, I set contentInset of SZTextView.
And there, problem is occurred.

Placeholder's string is placed below as much as contentInset.top.
When I delete set contentInset and contentOffset of _placeholderTextView, it's working as I expected.

So, is there anybody who experienced similar problem??

Tags for CocoaPods

Hey, I'd like to add this library to CocoaPods (I couldn't find it there despite the readme says so). Can you please add version tags?

[super becomeFirstResponder] breaks SZTextView

SZTextView gives me an EXC_BAD_ACCESS error when becoming first responder.
In specific, the call [super becomeFirstResponder] in the original implementation presents the problem. (line 5 below)

Any ideas how to fix this in iOS 8.3?

Stack trace:

Thread 1
Queue : com.apple.main-thread (serial)
#0  0x36481f46 in objc_msgSend ()
#1  0x2b714cd0 in -[UITextView canBecomeFirstResponder] ()
#2  0x2b5f674a in -[UIResponder becomeFirstResponder] ()
#3  0x2b5f6ad6 in -[UIView(Hierarchy) becomeFirstResponder] ()
#4  0x2b714ad6 in -[UITextView becomeFirstResponder] ()
#5  0x000343ec in -[SZTextView becomeFirstResponder] at ****/Classes/SZTextView.m:224
#6  0x2b7149d6 in -[UITextInteractionAssistant(UITextInteractionAssistant_Internal) setFirstResponderIfNecessary] ()
#7  0x2b7142d6 in -[UITextInteractionAssistant(UITextInteractionAssistant_Internal) oneFingerTap:] ()
#8  0x2b6fa06a in _UIGestureRecognizerSendActions ()
#9  0x2b5a90f8 in -[UIGestureRecognizer _updateGestureWithEvent:buttonEvent:] ()
#10 0x2b9c2f9a in ___UIGestureRecognizerUpdate_block_invoke661 ()
#11 0x2b572180 in _UIGestureRecognizerRemoveObjectsFromArrayAndApplyBlocks ()
#12 0x2b57005e in _UIGestureRecognizerUpdate ()
#13 0x2b5a74d8 in -[UIWindow _sendGesturesForEvent:] ()
#14 0x2b5a6ddc in -[UIWindow sendEvent:] ()
#15 0x2b57cfe4 in -[UIApplication sendEvent:] ()
#16 0x2b7f38fa in _UIApplicationHandleEventFromQueueEvent ()
#17 0x2b57b9f8 in _UIApplicationHandleEventQueue ()
#18 0x27eedfae in __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ ()
#19 0x27eed3be in __CFRunLoopDoSources0 ()
#20 0x27eeba24 in __CFRunLoopRun ()
#21 0x27e38200 in CFRunLoopRunSpecific ()
#22 0x27e38012 in CFRunLoopRunInMode ()
#23 0x2f717200 in GSEventRunModal ()
#24 0x2b5dca58 in UIApplicationMain ()
#25 0x00024e54 in main at ****/main.m:15

Xcode 6, IOS simulator

Hi there, I appreciate this isn't in the list of supported configs necessarily, but thought you might want to know - when I run in the simulator of xcode 6 (GM seed), I'm seeing the keyboard not appearing at all when the view is tapped (on ios 7 or ios 8). (Though the text view still gets focus and accepts keyboard input from the Mac).
(Haven't seen any problems on device, or or previous versions of xcode).

Again, could just be an Apple / IOS simulator problem, just thought I'd let you know...

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.