@@ -718,6 +718,7 @@ - (void)editText:(NSString *)text
718
718
// Brings up the keyboard if needed
719
719
[self presentKeyboard: YES ];
720
720
}
721
+
721
722
- (void )didCommitTextEditing : (id )sender
722
723
{
723
724
if (!self.textInputbar .isEditing ) {
@@ -760,11 +761,6 @@ - (BOOL)canShowTypingIndicator
760
761
return YES ;
761
762
}
762
763
763
- - (BOOL )canShowAutoCompletion
764
- {
765
- return NO ;
766
- }
767
-
768
764
- (CGFloat )heightForAutoCompletionView
769
765
{
770
766
return 0.0 ;
@@ -1549,6 +1545,95 @@ - (void)registerPrefixesForAutoCompletion:(NSArray *)prefixes
1549
1545
_registeredPrefixes = [[NSArray alloc ] initWithArray: array];
1550
1546
}
1551
1547
1548
+ - (void )didChangeAutoCompletionPrefix : (NSString *)prefix andWord : (NSString *)word
1549
+ {
1550
+ // No implementation here. Meant to be overriden in subclass.
1551
+ }
1552
+
1553
+ - (BOOL )canShowAutoCompletion
1554
+ {
1555
+ // Let's keep this around for a bit, for backwards compatibility.
1556
+ return NO ;
1557
+ }
1558
+
1559
+ - (void )showAutoCompletionView : (BOOL )show
1560
+ {
1561
+ // Reloads the tableview before showing/hiding
1562
+ if (show) {
1563
+ [self .autoCompletionView reloadData ];
1564
+ }
1565
+
1566
+ self.autoCompleting = show;
1567
+
1568
+ // Toggles auto-correction if requiered
1569
+ [self slk_enableTypingSuggestionIfNeeded ];
1570
+
1571
+ CGFloat viewHeight = show ? [self heightForAutoCompletionView ] : 0.0 ;
1572
+
1573
+ if (self.autoCompletionViewHC .constant == viewHeight) {
1574
+ return ;
1575
+ }
1576
+
1577
+ // If the auto-completion view height is bigger than the maximum height allows, it is reduce to that size. Default 140 pts.
1578
+ CGFloat maximumHeight = [self maximumHeightForAutoCompletionView ];
1579
+
1580
+ if (viewHeight > maximumHeight) {
1581
+ viewHeight = maximumHeight;
1582
+ }
1583
+
1584
+ CGFloat contentViewHeight = self.scrollViewHC .constant + self.autoCompletionViewHC .constant ;
1585
+
1586
+ // On iPhone, the auto-completion view can't extend beyond the content view height
1587
+ if (SLK_IS_IPHONE && viewHeight > contentViewHeight) {
1588
+ viewHeight = contentViewHeight;
1589
+ }
1590
+
1591
+ self.autoCompletionViewHC .constant = viewHeight;
1592
+
1593
+ [self .view slk_animateLayoutIfNeededWithBounce: self .bounces
1594
+ options: UIViewAnimationOptionCurveEaseInOut|UIViewAnimationOptionLayoutSubviews|UIViewAnimationOptionBeginFromCurrentState
1595
+ animations: NULL ];
1596
+ }
1597
+
1598
+ - (void )acceptAutoCompletionWithString : (NSString *)string
1599
+ {
1600
+ [self acceptAutoCompletionWithString: string keepPrefix: YES ];
1601
+ }
1602
+
1603
+ - (void )acceptAutoCompletionWithString : (NSString *)string keepPrefix : (BOOL )keepPrefix
1604
+ {
1605
+ if (string.length == 0 ) {
1606
+ return ;
1607
+ }
1608
+
1609
+ SLKTextView *textView = self.textView ;
1610
+
1611
+ NSUInteger location = self.foundPrefixRange .location ;
1612
+ if (keepPrefix) {
1613
+ location += self.foundPrefixRange .length ;
1614
+ }
1615
+
1616
+ NSUInteger length = self.foundWord .length ;
1617
+ if (!keepPrefix) {
1618
+ length += self.foundPrefixRange .length ;
1619
+ }
1620
+
1621
+ NSRange range = NSMakeRange (location, length);
1622
+ NSRange insertionRange = [textView slk_insertText: string inRange: range];
1623
+
1624
+ textView.selectedRange = NSMakeRange (insertionRange.location , 0 );
1625
+
1626
+ [self cancelAutoCompletion ];
1627
+
1628
+ [textView slk_scrollToCaretPositonAnimated: NO ];
1629
+ }
1630
+
1631
+ - (void )cancelAutoCompletion
1632
+ {
1633
+ [self slk_invalidateAutoCompletion ];
1634
+ [self slk_hideAutoCompletionViewIfNeeded ];
1635
+ }
1636
+
1552
1637
- (void )slk_processTextForAutoCompletion
1553
1638
{
1554
1639
if (self.isTransitioning ) {
@@ -1617,13 +1702,7 @@ - (void)slk_handleProcessedWord:(NSString *)word range:(NSRange)range
1617
1702
return [self cancelAutoCompletion ];
1618
1703
}
1619
1704
1620
- [self slk_showAutoCompletionView: [self canShowAutoCompletion ]];
1621
- }
1622
-
1623
- - (void )cancelAutoCompletion
1624
- {
1625
- [self slk_invalidateAutoCompletion ];
1626
- [self slk_hideAutoCompletionViewIfNeeded ];
1705
+ [self didChangeAutoCompletionPrefix: self .foundPrefix andWord: self .foundWord];
1627
1706
}
1628
1707
1629
1708
- (void )slk_invalidateAutoCompletion
@@ -1635,81 +1714,11 @@ - (void)slk_invalidateAutoCompletion
1635
1714
[self .autoCompletionView setContentOffset: CGPointZero ];
1636
1715
}
1637
1716
1638
- - (void )acceptAutoCompletionWithString : (NSString *)string
1639
- {
1640
- [self acceptAutoCompletionWithString: string keepPrefix: YES ];
1641
- }
1642
-
1643
- - (void )acceptAutoCompletionWithString : (NSString *)string keepPrefix : (BOOL )keepPrefix
1644
- {
1645
- if (string.length == 0 ) {
1646
- return ;
1647
- }
1648
-
1649
- SLKTextView *textView = self.textView ;
1650
-
1651
- NSUInteger location = self.foundPrefixRange .location ;
1652
- if (keepPrefix) {
1653
- location += self.foundPrefixRange .length ;
1654
- }
1655
-
1656
- NSUInteger length = self.foundWord .length ;
1657
- if (!keepPrefix) {
1658
- length += self.foundPrefixRange .length ;
1659
- }
1660
-
1661
- NSRange range = NSMakeRange (location, length);
1662
- NSRange insertionRange = [textView slk_insertText: string inRange: range];
1663
-
1664
- textView.selectedRange = NSMakeRange (insertionRange.location , 0 );
1665
-
1666
- [self cancelAutoCompletion ];
1667
-
1668
- [textView slk_scrollToCaretPositonAnimated: NO ];
1669
- }
1670
-
1671
1717
- (void )slk_hideAutoCompletionViewIfNeeded
1672
1718
{
1673
1719
if (self.isAutoCompleting ) {
1674
- [self slk_showAutoCompletionView: NO ];
1675
- }
1676
- }
1677
-
1678
- - (void )slk_showAutoCompletionView : (BOOL )show
1679
- {
1680
- // Reloads the tableview before showing/hiding
1681
- [self .autoCompletionView reloadData ];
1682
-
1683
- self.autoCompleting = show;
1684
-
1685
- // Toggles auto-correction if requiered
1686
- [self slk_enableTypingSuggestionIfNeeded ];
1687
-
1688
- CGFloat viewHeight = show ? [self heightForAutoCompletionView ] : 0.0 ;
1689
-
1690
- if (self.autoCompletionViewHC .constant == viewHeight) {
1691
- return ;
1720
+ [self showAutoCompletionView: NO ];
1692
1721
}
1693
-
1694
- // If the auto-completion view height is bigger than the maximum height allows, it is reduce to that size. Default 140 pts.
1695
- CGFloat maximumHeight = [self maximumHeightForAutoCompletionView ];
1696
-
1697
- if (viewHeight > maximumHeight) {
1698
- viewHeight = maximumHeight;
1699
- }
1700
-
1701
- CGFloat contentViewHeight = self.scrollViewHC .constant + self.autoCompletionViewHC .constant ;
1702
-
1703
- // On iPhone, the auto-completion view can't extend beyond the content view height
1704
- if (SLK_IS_IPHONE && viewHeight > contentViewHeight) {
1705
- viewHeight = contentViewHeight;
1706
- }
1707
-
1708
- self.autoCompletionViewHC .constant = viewHeight;
1709
-
1710
- [self .view slk_animateLayoutIfNeededWithBounce: self .bounces
1711
- options: UIViewAnimationOptionCurveEaseInOut|UIViewAnimationOptionLayoutSubviews|UIViewAnimationOptionBeginFromCurrentState
1712
- animations: NULL ];
1713
1722
}
1714
1723
1715
1724
0 commit comments