I was wondering is there any way to get the point of the selected text in UITextView ?
Thanks!
For iOS5 and above : Now UITextField and UITextView conform to UITextInput protocol so it is possible :)
Selecting the last 5 characters before the caret would be like this:
//Get current selected range , this example assumes is an insertion point or empty selection
UITextRange *selectedRange = [textField selectedTextRange];
NSLog("Start: %d <> End: %d", selectedRange.start, selectedRange.end);
//Calculate the new position, - for left and + for right
UITextPosition *newPosition = [textField positionFromPosition:selectedRange.start offset:-5];
//Construct a new range using the object that adopts the UITextInput, our textfield
UITextRange *newRange = [textField textRangeFromPosition:newPosition toPosition:selectedRange.start];
Yes, first get the active selection like this: myTextView.selectedRange and then pass it to this method [myTextView firstRectForRange:range] This will give you the first bounding rectangle (which is the entire selection if it is one line, and the area of the selection on the first line if it is multi-line) as a CGRect.
Related
I am using a textview and I want to fix the height to 30. I also want the text to scroll upwards when the text wraps to a new line. This also needs to be done dynamically. Any suggestions?
You will need to implement a couple of things:
You will need to implement a couple of UITextView delegate methods. Probably the easiest (at this point) for you would be to implement the
-(void)textViewDidChange:(UITextView *)textView{
NSRange range;
range.location = [textView.text length] - 1;
range.length = 1;
[textView scrollRangeToVisible:range];
}
method. With the code I have put in there. Let me know if this works for your needs :-)
Using my custom arabic keyboard on UItextView inputView, I m filling my textView with the arabic text but cannot get the written text align to right....Need help to align text to right.
-(BOOL)textViewShouldBeginEditing:(UITextView *)textView{
if(showCustomKeyboard==NO){
[textView resignFirstResponder];
textView.inputView=nil;
[textView becomeFirstResponder];
return YES;
}
else{
[textView resignFirstResponder];
if(customKeyboard==nil){
customKeyboard=[[CustomKeyboard alloc] initWithFrame:CGRectMake(0, 264, 320, 216)];
[customKeyboard setDelegate:self];
}
if([[UIApplication sharedApplication] respondsToSelector:#selector(inputView)]){
if (textView.inputView == nil) {
textView.inputView = customKeyboard;
[textView becomeFirstResponder];
}
}
self.customKeyboard.currentField=textView;
[textView becomeFirstResponder];
}
return YES;
}
You can set the writing direction of a UITextView using the setBaseWritingDirection selector:
UITextView *someTextView = [[UITextView] alloc] init];
[someTextView setBaseWritingDirection:UITextWritingDirectionLeftToRight forRange:[someTextView textRangeFromPosition:[someTextView beginningOfDocument] toPosition:[someTextView endOfDocument]]];
The code is a little tricky because UITextView supports having different parts of the text with different writing directions. In my case, I used [someTextView textRangeFromPosition:[someTextView beginningOfDocument] toPosition:[someTextView endOfDocument]] to select the full text range of the UITextView. You can adjust that part if your needs are different.
You may also want to check whether the text in your UITextView is LTR to RTL. You can do that with this:
if ([someTextView baseWritingDirectionForPosition:[someTextView beginningOfDocument] inDirection:UITextStorageDirectionForward] == UITextWritingDirectionLeftToRight) {
// do something...
}
Note that I specified the start of the text using [someTextView beginningOfDocument] and searched forward using UITextStorageDirectionForward. Your needs might differ.
If you subclass UITextView replace all these code samples with "self" and not "someTextView", of course.
I recommend reading about the UITextInput protocol, to which UITextView conforms, at http://developer.apple.com/library/ios/#documentation/uikit/reference/UITextInput_Protocol/Reference/Reference.html.
Warning about using the textAlignment property in iOS 5.1 or earlier: if you use it with this approach together with setting the base writing direction, you will have issues because RTL text when aligned left in a UITextView actually aligns to the right visually. Setting text with an RTL writing direction to align right will align it to the left of the UITextView.
Try textAlignment property.
textView.textAlignment = UITextAlignmentRight;
Take a look at UITextView Class Reference.
EDIT: Maybe CATextLayer can help you, someone suggests to use this class to customize text, but I've never used it personally...
Otherwise, you can force your textView to reverse your input in UITextFieldDelegate method:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
The text field calls this method whenever the user types a new character in the text field or deletes an existing character.
Here you can replace your input with a new NSString where you put the characters from right to left.
Hope this makes sense...
Ah... Do not forget to set
textView.textAlignment = UITextAlignmentRight;
to move your prompt on the right.
Try this code:
yourtextview.textAlignment = UITextAlignmentRight;
Hope this helps you.
Something which no one mentioned here or on any other post is that make sure you have not called sizeToFit for TextView. It simple aligns the textView (not text) to the left which gives the illusion that text is left to right instead of right to left.
If you are creating UI from Storyboard, the set constraint to Lead or Trailing space and value of First Item will be Respect Language Direction
in swift you can use this code
textView.makeTextWritingDirectionRightToLeft(true)
I have some text coming from server. It may be single line or multiline text. I have to display the text on UILabel, which is no problem for me. The problem is, I have to display UIButton on finding a particular substring of the same text. For example the text is Nitish\n435-234-6543\nIndia which is being displayed as follows :
Nitish
435-234-6543
India
So, when I find 435-234-6543 I have to display UIButton on 435-234-6543.
Notes:
The text is dynamic - coming from server. Above is only an example.
UIButton will be a subview of UILabel.
I tried different ways like OHAttributedLabel, rectForLetterAtIndex and this too. But not getting success. What my idea is, to create the button when substring is found and to set the frame of button based on NSRange of substring. Is this a possibility? How can it be done? Or is there some other way to do this?
I guess it is the approach I am worried about.
-->I have tried to calcluate position but didnt get success. Seems lots of work to calcualate position. One immediate solution come to my mind is why are you not taking one or two labels and one button.
Suppose. You got dynamic string from webservice is: "Nitesh-56789-Test".
Find range of string suppose i.e. "56789".
String before starting location of that range assign to one label i.e. assign "Nitesh" to lable one.
Now add one custom button with our searched string as a text(56789).
Now make sure in main string there something after our substring or not. Here I mean after our search string "56789" still "Test" remain so assign it to third lable.
Here you have to figue out frame of all labels and button using dynamic height width calculation by using sizeWithFont method.
1) Easy solution:
Make your UILabel a UITextView and use the property dataDetectorTypes to have phone numbers as links automatically.
2) More involved solution:
There is a convenient method to determine the size any text will need to be drawn:
CGSize size = [label.text sizeWithFont:label.font
constrainedToSize:CGSizeMake(label.frame.size.width, CGFLOAT_MAX)
lineBreakMode:UILineBreakModeWordWrap];
You could now determine which field is the phone number by splitting the string into its lines with:
NSArray *comp = [label.text componentsSeparatedByString:#"\n"];
and then checking which one is numeric. Now you would have to calculate the exact frame from the height of your size variable, maybe like this:
CGFloat positionOfNumber; // the index of your line in comp cast to CGFloat
CGFloat buffer = 10; // fiddle with this
CGFloat buttonHeight = (size.height- 2*buffer)/[comp length];
CGFloat buttonY = buffer + positionOfNumber * buttonHeight;
CGRect buttonFrame = CGRectMake(0, buttonY, label.frame.size.width, buttonHeight);
For those who want perfect solution here's the solution on how to get CGRect of a substring.
I am new to iphone;
what i did is creating a 10 labels with corresponding text fields.
when i click on text field keypad is open.
But it covers the bottom 4 textfields.
I can't able to enter text to those fields.
i need when i click textfield in front of keypad screen moves up.
now i can able to enter in those textfields.
How can i done this.
Thank u in advance.
As Jumhyn wrote, make the UIViewController the delegate of all the text fields. Then assign the text fields tags from 0 to 9, from top to bottom. Then implement the following method in the controller:
- (void)textFieldDidEndEditing:(UITextField *)textField {
self.view.bounds = CGRectOffset(self.view.bounds, 0, textField.tag * 50);
[[self.view viewWithTag: textField.tag + 1] becomeFirstResponder];
}
The idea is that the first line shifts all the text fields up, depending on which text field has been edited, in the second line you activate the next text field. You will have to modify the code to better fit your layout, but it should give you the idea.
Make the UIViewController that handles the text views a UITextFieldDelegate:
#interface TextFieldViewController : UIViewController <UITextFieldDelegate> {
then in the textFieldShouldBeginEditing: method in the .m file, add the code that moves the text fields up. For example:
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
myTextField3.frame = CGRectMake(myTextField3.frame.origin.x, myTextField3.frame.origin.y - /*whatever you want to subtract from the y value*/, myTextField3.frame.size.width, myTextField3.frame.size.height);
myTextField4.frame = CGRectMake(myTextField4.frame.origin.x, myTextField4.frame.origin.y - /*whatever you want to subtract from the y value*/, myTextField4.frame.size.width, myTextField4.frame.size.height);
myTextField5.frame = CGRectMake(myTextField5.frame.origin.x, myTextField5.frame.origin.y - /*whatever you want to subtract from the y value*/, myTextField5.frame.size.width, myTextField5.frame.size.height);
myTextField6.frame = CGRectMake(myTextField6.frame.origin.x, myTextField6.frame.origin.y - /*whatever you want to subtract from the y value*/, myTextField6.frame.size.width, myTextField6.frame.size.height);
return YES;
}
I want to create a UITextField with the placeholder aligned to left and the text is aligned to center. I search a bit and I found placeholderRectForBounds: function the apple documents says override if you want to but doesnt say how? I tried categorize the UITextField however It wasnt a success. any one has an idea?
thanks
At default set the text alignment as left.
When you start typing on the textfield this delegate will be called
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
//here again set the alignment value to center
textField.textAlignment=UITextAlignmentCenter;
}
This delegate will be called when you focus out of textfield
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
//Here check text in the textfield. if its nill set alignment as left.
//Becoz if there is no text in textfield placeholder will be shown.
}