Xcode: Position Images Inside UITextView - objective-c

I have a string that is added to the UITextView, and I would like to add images after a certain world in that string.
Example:
#property (strong, nonatomic) IBOutlet UITextView *textview;
textview.text = #"This is an icon of a cat. And this is an
icon of a car. Here is more random text.";
This is an example of what I want:
How do I append and position images after a certain word?
I have an array that detects certain words:
NSArray *detectWords = #[#"cat", #"car"];
So I can detect when certain words show up in the text of a textview, but I'm not sure how to add an image right after those words.
Also, the string of words will vary in different situations (not static), so pre-positioning everything on the interface builder is not an option for me.
Thanks in advance!

Check out this library it might help you
https://github.com/dzog/ImgGlyph

Like this way, you can also do this,I hope it will help u
Here is how to put an image in the right portion of a UITextField:
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 500, 20)];
textField.rightViewMode = UITextFieldViewModeWhileEditing;
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 15, 15)];
imageView.image = [UIImage imageNamed:#"Dog.png"];
textField.rightView = imageView;

Related

UIImageView places on UIButton

I want to place an UIImageView on UIButton programmatically (with constraints). The UIButton is implemented from Storyboard. I don´t find example from the net. I tried this code:
//Test Image button
UIImageView* iqImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"iq1"]];
iqImageView.frame = CGRectMake(0, 0, self.iqCategoryButton.frame.size.width, self.iqCategoryButton.frame.size.height);
iqImageView.contentMode = UIViewContentModeRedraw;
[self.iqCategoryButton addSubview:iqImageView];
The problem is at this moment, the frame´s size of the button is not equal as it will be seen on the screen (due to constraints).

Image and Text locations in UIButton

Im trying to put a UIImage and text into a UIButton, by using UIEdgeInset to locate them.
The problem is, changing one of the edgeinset setting will cause both the image and the text to change the position.
Which means, I put the image to the correct position, then I change the titleEdgeInset values, suddenly not only the text but also the image changed the position.
Any one can help out please?
According the Apple's documentation this won't happen. Did you check to make sure you have negative insets and not positive?
The insets you specify are applied to the title rectangle after that rectangle has been sized to fit the button’s text. Thus, positive inset values may actually clip the title text.
This property is used only for positioning the title during layout.
The button does not use this property to determine
intrinsicContentSize and sizeThatFits:.
I hope this may helps you
#property (strong, nonatomic) IBOutlet UIButton *Imagetext;
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:#"Click Me"];
NSTextAttachment *ImageAttachment = [[NSTextAttachment alloc] init];
ImageAttachment.image = [UIImage imageNamed:#"add.png"];
NSAttributedString *attributedWithImage = [NSAttributedString attributedStringWithAttachment:ImageAttachment];
[attributedString replaceCharactersInRange:NSMakeRange(6, 0) withAttributedString:attributedWithImage];
// [attributedString appendAttributedString:attributedWithImage];
[self.Imagetext setAttributedTitle:attributedString forState:UIControlStateNormal];

Providing *hint* in text field in ios 7 as edittext field in android

I am creating a feedback form in an ios7 application and want to place few UITextFields which contain hints in the respective UITextField for name, phone number, email address, etc. I am using xcode 5 and ios7 for programming and creating application without use of storyboard.
The hint property is available in android for edit text field, but I am unable to find any such property in UITextField in ios 7.
Make use of the Placeholder text property.
Here's an example:
UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(10, 10, 100, 50)];
textField.font = [UIFont fontWithName:#"Arial" size:20];
textField.placeholder = #"Your hint here.";
If you are using a storyboard or XIB, this property can be edited there also.
Also, if you want to use custom attributes for the placeholder, you can use the attributedPlaceholder property:
NSAttributedString *attribString = [[NSAttributedString alloc] initWithText:#"Your hint here." attributes:#{NSFontAttributeName: [UIFont fontWithName:#"HelveticaNeue" size:16.0]}];
textField.attributedPlaceholder = attribString;

UILabel Highlighting Text

Can anyone quickly help me? I've been playing with this for hours and don't understand why this isn't working?
I'm trying to update highlighted text in a selected label (which is referenced in an array of UILabels previously defined).
This method is called by a receiving IBAction from a UISlider in the view interface.
However, when I retrieve the selected UILabel object from the array and set its HIGHLIGHTED property, there's no corresponding reaction on the view interface. I'm under the impression that its supposed to automatically redraw the view with text highlighted using the code below.
PS: My connections seem to all be correct the Interface Builder (i.e., IBOutlet UILabels are properly mapped/connected and the UISlider which triggers this method is connected through IBAction).
Am I missing something?
- (IBAction) changeHighlightedLabel: (id)sender
{
// Setup
UILabel *selectedLabel = [[UILabel alloc] init];
selectedLabel.highlightedTextColor = [UIColor greenColor];
// Interpret slider value and round to integer
UISlider *temp = sender;
float unroundedTempValue = [temp value];
float roundedTempValue = roundf(unroundedTempValue);
// Select the UILabel object from the UI Label array based on slider valuer
selectedLabel = [uiLabelArray objectAtIndex:roundedTempValue];
// Highlight the selected label
selectedLabel.highlighted = YES;
}
I've also tried substituting...
selectedCountryLabel = [[uiCountryLabelArray objectAtIndex:roundedTempValue] isHighlighted];
... for the last line. Still doesn't work.
Any feedback or help please? Thanks.
You are creating a UILabel and setting highlightedTextColor property to that first and then you are overwriting that with a UILabel from array. Since you are not setting any highlightedTextColor this time, the highlighted property will not work on label.
Change it as follows.
- (IBAction) changeHighlightedLabel: (id)sender
{
// Interpret slider value and round to integer
UISlider *temp = sender;
float unroundedTempValue = [temp value];
float roundedTempValue = roundf(unroundedTempValue);
// Select the UILabel object from the UI Label array based on slider valuer
selectedLabel = [uiLabelArray objectAtIndex:roundedTempValue];
// Highlight the selected label
selectedLabel.highlightedTextColor = [UIColor greenColor];
selectedLabel.highlighted = YES;
}

UILabel Not Released And Wrong Position Display

What I am trying to do is display a label on loading view for first time. When User will tap an object, that label should disappear and three circles should appear on its position. But what is happening is that my label is not hidden when I am trying to release it, as I will be needing it no more. Also it is not present at position I want.
What I have done is:
#interface AnimateCircle : UIViewController
{
UILabel *text;
CALayer *smallCircle1;
CALayer *smallCircle2;
CALayer *smallCircle3;
}
and then declared property for label and synthesized it. Please note that I have added AnimateCircle as subview of a view. So my subview is starting from small circles as indicated in 2nd figure.
In viewDidLoad, I wrote:
text = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, 310, 100)];
text.text = #"Tap the Red Button To Start The Timer...!";
text.font = [UIFont fontWithName:#'SnellRoundHand-Bold" size:8];
[self.view addSubview:text];
[text release];
At tap Gesture, I call a method in which I release my label text to hide it.
[text removeFromSuperView];
and draw three circles at its position. Just showing position...
smallCircle1.frame = CGRectMake(100, 15, 30, 30);
smallCircle2.frame = CGRectMake(140, 15, 30, 30);
smallCircle3.frame = CGRectMake(180, 15, 30, 30);
Failed after struggling for 2 hours. Please can someone tell me that what am I missing? Thanks a lot...
You must actually add the 3 circles to the view.
Use self.view.layer addSublayer:.