Skip to content

Commit

Permalink
Use attributedPlaceholder for placeholder text (#31)
Browse files Browse the repository at this point in the history
* Update to use UITextView attributedPlaceholder.

* Reformat and remove some options in the example
  • Loading branch information
collinjackson committed Mar 23, 2022
1 parent f63c79d commit e5f8a7f
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 76 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

* fix an iOS race condition that would cause errors when autofocusing
* implement autocorrect for iOS
* use `UITextView.attributedPlaceholder` to draw the iOS placeholder
* ensure that `BoxConstraints` are valid

## 2.1.1

Expand Down
34 changes: 34 additions & 0 deletions example/ios/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
**/dgph
*.mode1v3
*.mode2v3
*.moved-aside
*.pbxuser
*.perspectivev3
**/*sync/
.sconsign.dblite
.tags*
**/.vagrant/
**/DerivedData/
Icon?
**/Pods/
**/.symlinks/
profile
xcuserdata
**/.generated/
Flutter/App.framework
Flutter/Flutter.framework
Flutter/Flutter.podspec
Flutter/Generated.xcconfig
Flutter/ephemeral/
Flutter/app.flx
Flutter/app.zip
Flutter/flutter_assets/
Flutter/flutter_export_environment.sh
ServiceDefinitions.json
Runner/GeneratedPluginRegistrant.*

# Exceptions to above rules.
!default.mode1v3
!default.mode2v3
!default.pbxuser
!default.perspectivev3
14 changes: 0 additions & 14 deletions example/ios/Flutter/flutter_export_environment.sh

This file was deleted.

52 changes: 27 additions & 25 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_native_text_input/flutter_native_text_input.dart';
Expand Down Expand Up @@ -76,32 +74,36 @@ class HomePage extends StatelessWidget {
),
DemoItem(
title: 'NativeTextInput Example Usage',
child: NativeTextInput(
decoration: BoxDecoration(
border: Border.all(
color: Colors.black87,
width: 2,
child: Container(
height: 30,
child: NativeTextInput(
decoration: BoxDecoration(
border: Border.all(
color: Colors.black87,
width: 2,
),
),
),
style: const TextStyle(
fontSize: 16,
color: Colors.black54,
),
textCapitalization: TextCapitalization.sentences,
placeholder: "placeholder",
placeholderColor: Colors.black12,
iosOptions: IosOptions(
autocorrect: true,
cursorColor: Colors.black87,
keyboardAppearance: Brightness.dark,
placeholderStyle: const TextStyle(
fontWeight: FontWeight.bold,
style: const TextStyle(
fontSize: 16,
color: Colors.black54,
),
minHeightPadding: 4,
textCapitalization: TextCapitalization.sentences,
placeholder: "placeholder",
placeholderColor: Colors.black12,
iosOptions: IosOptions(
autocorrect: true,
cursorColor: Colors.black87,
keyboardAppearance: Brightness.dark,
placeholderStyle: const TextStyle(
fontWeight: FontWeight.bold,
),
),
keyboardType: KeyboardType.defaultType,
onChanged: _onChangeText,
onSubmitted: _onSubmittedText,
focusNode: _focusNode,
),
keyboardType: KeyboardType.defaultType,
onChanged: _onChangeText,
onSubmitted: _onSubmittedText,
focusNode: _focusNode,
),
),
Center(
Expand Down
2 changes: 2 additions & 0 deletions ios/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,5 @@ Icon?
.tags*

/Flutter/Generated.xcconfig
/Flutter/ephemeral/
/Flutter/flutter_export_environment.sh
34 changes: 19 additions & 15 deletions ios/Classes/NativeTextInput.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#import "NativeTextInput.h"
#import "NativeTextInputDelegate.h"

@interface UITextView(Placeholder)
@property(nullable, nonatomic, copy) NSAttributedString *attributedPlaceholder;
@end

@implementation NativeInputField {
UITextView* _textView;

Expand Down Expand Up @@ -55,16 +59,21 @@ - (instancetype)initWithFrame:(CGRect)frame
_delegate = [[NativeTextInputDelegate alloc] initWithChannel:_channel arguments:args ];
_textView.delegate = _delegate;

_textView.text = args[@"placeholder"];
_textView.textColor = _delegate.placeholderFontColor;
_textView.font = _delegate.placeholderFont;

if (![args[@"text"] isEqualToString:@""]) {
_textView.text = args[@"text"];
_textView.textColor = _delegate.fontColor;
_textView.font = _delegate.font;
_textView.text = args[@"text"];
_textView.textColor = _delegate.fontColor;
_textView.font = _delegate.font;
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
if (args[@"placeholder"] && ![args[@"placeholder"] isKindOfClass:[NSNull class]]) {
if (_delegate.placeholderFont) {
[attributes setObject:_delegate.placeholderFont forKey:NSFontAttributeName];
}
if (_delegate.placeholderFontColor) {
[attributes setObject:_delegate.placeholderFontColor forKey:NSForegroundColorAttributeName];
}
_textView.attributedPlaceholder = [[NSAttributedString alloc] initWithString:args[@"placeholder"]
attributes:attributes];
}

_containerWidth = [args[@"width"] floatValue];

__weak __typeof__(self) weakSelf = self;
Expand Down Expand Up @@ -100,12 +109,7 @@ - (void)onFocus:(FlutterMethodCall*)call result:(FlutterResult)result {

- (void)onUnFocus:(FlutterMethodCall*)call result:(FlutterResult)result {
[_textView resignFirstResponder];
if (_textView.text.length == 0) {
_textView.text = _args[@"placeholder"];
_textView.textColor = _delegate.placeholderFontColor;
_textView.font = _delegate.placeholderFont;
}


result(nil);
}

Expand Down
15 changes: 0 additions & 15 deletions ios/Classes/NativeTextInputDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,6 @@ - (UIFontWeight)fontWeightFromString:(NSString*)fontWeight {
}

- (void)textViewDidBeginEditing:(UITextView *)textView {
if ([textView.text isEqualToString:_args[@"placeholder"]]) {
textView.text = @"";
textView.textColor = _fontColor;
textView.font = self.font;
}

if (textView.textContainer.maximumNumberOfLines == 1) {
textView.textContainer.lineBreakMode = NSLineBreakByCharWrapping;
}
Expand All @@ -114,19 +108,10 @@ - (void)textViewDidBeginEditing:(UITextView *)textView {
}

- (void)textViewDidChange:(UITextView *)textView {
textView.textColor = textView.text == 0 ? _placeholderFontColor : _fontColor;
textView.font = textView.text == 0 ? self.placeholderFont : self.font;

[_channel invokeMethod:@"inputValueChanged" arguments:@{ @"text": textView.text }];
}

- (void)textViewDidEndEditing:(UITextView *)textView {
if (textView.text.length == 0) {
textView.text = _args[@"placeholder"];
textView.textColor = _placeholderFontColor;
textView.font = self.placeholderFont;
}

if (textView.textContainer.maximumNumberOfLines == 1) {
textView.textContainer.lineBreakMode = NSLineBreakByTruncatingTail;
}
Expand Down
17 changes: 10 additions & 7 deletions lib/flutter_native_text_input.dart
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,9 @@ class _NativeTextInputState extends State<NativeTextInput> {
);
channel.invokeMethod("getContentHeight").then((value) {
if (value != null && value != _contentHeight) {
_contentHeight = value;
setState(() {});
setState(() {
_contentHeight = value;
});
}
});
});
Expand Down Expand Up @@ -356,7 +357,7 @@ class _NativeTextInputState extends State<NativeTextInput> {
return ConstrainedBox(
constraints: BoxConstraints(
minHeight: _minHeight,
maxHeight: _maxHeight,
maxHeight: _maxHeight > _minHeight ? _maxHeight : _minHeight,
),
child: LayoutBuilder(
builder: (context, layout) => Container(
Expand All @@ -372,14 +373,16 @@ class _NativeTextInputState extends State<NativeTextInput> {
..setMethodCallHandler(_onMethodCall);
channel.invokeMethod("getLineHeight").then((value) {
if (value != null) {
_lineHeight = value;
setState(() {});
setState(() {
_lineHeight = value;
});
}
});
channel.invokeMethod("getContentHeight").then((value) {
if (value != null) {
_contentHeight = value;
setState(() {});
setState(() {
_contentHeight = value;
});
}
});
_channel.complete(channel);
Expand Down

0 comments on commit e5f8a7f

Please sign in to comment.