How can I detect taps on a particular part (substring) of a UILabel? - ios

I am new to iOS development. I dont know whether this questions has been asked already or not,
I tried searching for the solution on stackoverflow but didn't get any results.
Question :
I have a UILabel called myLabel with the text: "Click here to proceed"
Now the problem is I want to perform an action when user taps only "Click".
I know how to use UITapGestureRecognizer, but it responds to the whole UILabel. Is it possible to just detect when user taps only on string "Click"?

You could check the location of the touch to see if it is on the word "Click". This may not be completely accurate though and may break if you change the text in your label.
What you could do is first get the location of the click by using:
CGPoint location = [gesture locationInView:gesture.view];
This gets the location of the gesture in your view.
Once you have this, you can either have the size of the word "Click" hard-coded or you can create an identical label to your gesture label to get the size, such as:
UILabel *sizeLabel = //create label with same font.
[sizeLabel setText:#"Click"];
float width = [sizeLabel sizeThatFits:CGSizeMake(MAXFLOAT,MAXFLOAT)].width;
You could also do this when creating the actual label and store this width in a static variable so that you only have to do it once and you don't have to create an extra label (ie set the text of your real label to #"Click", get the width, and then set it to the real text).
Once you have the width, you and the location of the tap, you can check if the word "Click" was tapped by comparing:
if (location.x < width) {
//Put your tap code in here.
}

Related

Set UiTextField cursor on top Swift IOS

I have a TextField in my storyboard with a height of 100 .
When I'm clicking the TextField the cursor is placed in center :
I would like to set the cursor position in the top left corner.
I tried :
let startPosition: UITextPosition = textField.beginningOfDocument
But it seems that the position is already at the beginning and when the height is extended the cursor stay in center.
How can it be done ?
Seems like you want a multiline UITextField. In that case you might want to take a look at UITextView, which should allow for what you want (docs)

How can I check out-of-sight characters in UITextField?

I have a UITextField that shrinks and expands when user input text. If the textfield's width reach the screen width, I want it to be right-aligned so user can see the last input characters. In other circumstance, I want it to be left-aligned.
Because the textfield's maximum width is not exactly the same with screen, I need to find a way to check if it has characters out of visible area.
Is there anyway to achieve this?
You could try checking the width of the string that is in the field and comparing it to the width of the field itself. It would look something like this -
CGSize textSize = [self.field.text sizeWithAttributes:#{NSFontAttributeName:self.field.font}];
// If the text is larger than the field
if (textSize.width > self.field.bounds.size.width) {
// There is text that is not visible in the field.
}
This is fairly rough, but should get you close.

How to trace which image is currently seen in my Uiscrollview?

I am new to iOS.
I am working on UIScrollView based application. I have 3 images in UIScrollView.
I have 2 UIButtons which have selector method for Facebook and Twitter sharing.
I want to share image which is currently seen in UIScrollView to Facebook & Twitter.
I am stuck at extracting image at current scroll point.
After Googling I think something like UIPageController is the solution for me.
But I do not have any idea about how to implement it.
Please help me sort it out. Thank You!
I would say you have multiple options here, this is really a design question.
An easy (but also not very elegant) approach would be to fetch the current offset of the scroll view using its contentOffset property (depending on whether your scroll view scrolls horizontically or vertically, you have ease to use the x or y value of the contentOffset).
Next, you get the position values from your image views (again either myImageView.frame.origin.x or myImageView.frame.origin.y) and you can then calculate which of the image views is currently in the offset of the scroll view.
You can use a page controller (basically, you allow the user to end its scrolling on specific index).
Or when the user touches a button, you can retrieve the contentOffset of the scrollView and compare this value with the origin of the frame your images.
Assume, you created your images with these frames:
image1.frame = CGRectMake(0,0,320,250);
image2.frame = CGRectMake(0,250,320,250);
image3.frame = CGRectMake(0,500,320,250);
Now when you get the action:
-(IBAction)share:(id)sender {
CGPoint offset = self.scrollView.contentOffset;
if (offset.y < 250) {
//First image selected
}else if (offset.y > 500) {
//Third image selected
} else {
//Second image selected
}
}

Can't make content resistance work in a table view cell

I have a UITableViewController that has a static field that displays a location.
The style this static field is "right detail".
The left side of the cell shows the city chosen by the user and the right side has an disclosure indicator with the label "Item Location".
When the user first navigates to this controller the left label is set to "No location" and everything looks fine, like this: (I can't post images since I don't have the rep)
"No location Item Location >"
The problem is that if I set a long city name (such as "Rancho Santa Margarita, California) the "Item Location" label shrinks and becomes filled with ellipsis (...)
"Rancho Santa Margarita, California It... >"
What I want to happen is that when the user picks a long city name then the city label will take as much space as it can without causing the "Item Location" label to shrink.
So basically it will look something like this:
"Rancho Santa Margarita, ... Item Location >"
Using the interface builder I tried setting both the Horizontal Content Hugging priority and Compression Resistance Priority of the right detail label (Item Location) to 1000 and I even set Horizontal Content Hugging priority and Compression Resistance priority of the city label to 1, but this doesn't seem to work. (it still looks like the second screenshot).
One more thing, when the user selects a new location I use the following method to update the location text:
- (void)setLocationText:(NSString *)text {
self.locationField.textLabel.text = text;
[self.locationField setNeedsLayout];
}
Where locationField is an outlet of the location UITableViewCell.
Without the code to setNeedsLayout the city text label doesn't grow to fit the city name, so that's the reason why I used this.
I'm using XCode 4.6 and targeting iOS 6.1
Do you guys know what could be the issue?
Thanks in advance,
Ido
Another option to using autolayout, is to subclass UITableViewCell with two label outlets and implement layoutSubviews
- (void)layoutSubviews {
[super layoutSubviews];
CGSize rightLabelSize = [self.rightLabel sizeThatFits:self.contentView.frame.size];
CGRect rightLabelFrame = CGRectZero;
rightLabelFrame.origin.x = self.contentView.frame.size.width - rightLabelSize.width;
rightLabelFrame.size = rightLabelSize;
self.rightLabel.frame = rightLabelFrame;
CGRect leftLabelFrame = CGRectZero;
leftLabelFrame.size.width = self.contentView.frame.size.width - rightLabelSize.width;
self.leftLabel.frame = rightLabelFrame;
}
Note that this does not add padding or really care much about the height of the labels, so you would need to modify to account for those things.
If you don't like subclassing, then you could construct and size the labels before you return the cells in your controllers tableView:cellForRowAtIndexPath: protocol method.

UITextView increment character to the left

I have a UITextView that has a fixed width and height. I pre-populate the entire textfield with blanks.
I would like to insert a character with the push of a button that will erase the last blank character, insert my string character and then place the cursor at the beginning of the newly inserted string. I am trying to achieve inserting special fonts right to left and bottom to top.
It is working with the first button push and on the second button push the new value is inserted in the correct position to the left, however, the cursor will not move to the left after the second button push, it remains to the right after the second string insert.
Here is my code...
-(IBAction)chartP:(id)sender {
NSRange currentRange = myChart.selectedRange;
if (currentRange.length == 0) {
currentRange.location--;
currentRange.length++;
}
myChart.text = [myChart.text stringByReplacingCharactersInRange:currentRange
withString:[NSString string]];
currentRange.length = 0;
myChart.selectedRange = currentRange;
myChart.text = [myChart.text stringByAppendingString:#"p"];
myChart.selectedRange = NSMakeRange(myChart.selectedRange.location -1, 0);
}
Can someone assist me with what I am missing here to continually increment to the left with my string inserts?
How about flipping the text area:
myChart.transform = CGAffineTransformMakeScale(-1,-1);
It sounds like you are trying to implement right-to-left text direction by faking it. Why not do the real thing?
Here's a question that covers the topic:
Change the UITextView Text Direction
If you need bottom-to-top entry, and you have the ability to use a custom font, perhaps you can apply a transformation to the UITextView and y-flip it. Look at the transform property of UIView. (Things like the text selection loupe may break, but it's worth a try.)

Resources