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;
}
Related
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];
I am trying to create editable transparent NSTextField in a semi transparent window:
What I have noticed is that whenever the field is editable there is a white "selection like" background drawn even though the element is not actually selected.
Additional observable symptoms:
This highlight is not present when the field is set as non-editable.
If there are multiple fields only the first one has the highlight.
The highlight is not present if the text is not set programmatically
Following code was used to generate the field:
f = [[NSTextField alloc] initWithFrame:b2];
f.backgroundColor = [NSColor clearColor];
f.drawsBackground = YES;
f.bordered = NO;
f.bezeled = NO;
f.focusRingType = NSFocusRingTypeNone;
f.textColor = [NSColor whiteColor];
f.editable = YES;
f.selectable = YES;
f.backgroundColor = [NSColor clearColor];
f.allowsEditingTextAttributes = YES;
f.stringValue = #"Foo";
[self.contentView addSubview:f];
Additional observations (potentially a separate problem):
When field is not the first field on the screen and the initial text is set programmatically and removed by editing the field there is a shadow of the text:
I can't seem to find any documentation on this I wonder if any of you have had this happen and potentially have a solution or a pointer to docs I might have not stumbled upon.
part 1: removing highlight
there are two options here depending on the behavior you are looking for
option 1 - nil first responder
TextField is not first responder
No highlighted text
No Cursor at the end of text
Assuming you are using an NSWindow, set the first responder to nil after calling makeKeyAndOrderFront
[self.window makeKeyAndOrderFront:self];
[self.window makeFirstResponder:nil];
It appears as though makeKeyAndOrderToFront: looks for the first NSResponder in the window willing to accept first responder. Then becomeFirstResponder is called on that responder; leading to option 2
option 2 - override becomeFirstResponder
TextField is first responder
No highlighted text
Cursor appears at the trailing edge of text
Subclass NSTextfield and override it's becomeFirstResponder method
#implementation BPTextField
- (BOOL)becomeFirstResponder {
BOOL isResponder = [super becomeFirstResponder];
//Get Field editor, set selected range
NSText* fieldEditor = [[self window] fieldEditor:YES forObject:self];
[fieldEditor setSelectedRange:NSMakeRange(fieldEditor.string.length ,0)];
return isResponder;
}
#end
I prefer this option from a usability perspective
part 2: removing shadow
option 1 - add a solid background color
I'm not clear ; ) on why this is the case, but if you add a solid background color, the text will update.
option 2 - override textDidChange
override textDidChange:notification in your textfield
#implementation BPTextField
- (void)textDidChange:(NSNotification *)notification {
[super textDidChange:notification];
[self setNeedsDisplay:YES];
}
#end
Final notes
You'll notice that the text looks bad, or rigid. Adding a background color to the textfield, or to the superview's layer will fix this.
This is an answer to part 2 of the question.
The shadow artifact is from rendering window's shadow which is not updated when the text in the NSTextField changes.
If the window's hasShadow method returns "NO" the text's shadow will not create shadow for the text either.
The UITextFields in my app have placeholder text defined (in Interface Builder), and I cannot cause these fields to acquire focus (i.e. show the keyboard and allow editing) when I tap on the area occupied by the placeholder text. If I tap on the textfields in an area just outside the that of placeholder text (though still within the bounds of the textfiled itself), it acts as normal (i.e. the keyboard pops up and I can edit the content of the textfield). How can I fix this?
Thanks.
EDIT 1
Ok, I think I've got it. I'm also setting a blank view to the "leftView" property of these UITextFields. If I remove this, you can touch the UITextFields in the area of the placeholder text and it reacts as expected; I need this view for the leftView though. If you change the background color of this spacer view to red, you can see that it doesn't get in the way at all, so I don't know what's going wrong.
Why does this code cause this problem?
Thanks.
+(UIView*)getTextFieldLeftSpacerViewWithBackgroundColor:(UIColor*)backgroundColor andHeight:(CGFloat)height
{
UIView *leftWrapper = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 8.0f, height)];
leftWrapper.autoresizingMask = UIViewAutoresizingNone;
[leftWrapper setOpaque:YES];
if(backgroundColor){leftWrapper.backgroundColor = backgroundColor;}
else{leftWrapper.backgroundColor = [UIColor clearColor];}
return [leftWrapper autorelease];
}
+(void)setTextFieldLeftSpacerForTextFieled:(UITextField*)textField
{
if(textField)
{
UIView *spacer = [MYViewController getTextFieldLeftSpacerViewWithBackgroundColor:nil andHeight:textField.bounds.size.height];
textField.leftView = spacer;
textField.leftViewMode = UITextFieldViewModeAlways;
}
}
Just ran into the same problem and didn't want to subclass, just had to use :
leftWrapper.userInteractionEnabled = NO;
I abandoned this approach. Instead of using an invisible view to offset the text, I opted to subclass UITextField and provide offset CGRects for the bounds of the text within theUITextField. The following SO post was very helpful:
Indent the text in a UITextField
I have a subclass of UILabel, which is supposed to update its text when the user types something. Naturally, as the length of text increases, the size of the label must adjust to accommodate the text. I called the sizeToFit method, and while the label adjusts its width correctly, the bottom of the text is cut off. The problem is that the text includes subscripts and superscripts , and the label is not adjusting itself with the subscripts in consideration (for example, with H₂O the bottom of the two is cut off).
Can I override sizeToFit or sizeThatFits: to increase the height of the label?
EDIT:
- (void) addCompound {
self.currentLabel = [[FormulaLabel alloc] initWithFrame:CGRectMake(10, 10, 100, 50)];
[self addSubview:self.currentLabel];
[self.currentLabel sizeToFit];
// Right now self.currentlabel.text = "". However, I've confirmed thru NSLogging that letters are added to self.currentLabel.text as the user types on the keyboard. Also, the text displays properly (as long as it's within the original frame) when I remove [sel.currentLabel sizeToFit]
}
You should override the UILabel method (CGSize)sizeThatFits:(CGSize)size in your subclass like example below. I just add 10pt to the height calculated by UILabel to accommodate the subscript.
#implementation ESKLabel
- (CGSize)sizeThatFits:(CGSize)size
{
CGSize theSize = [super sizeThatFits:size];
return CGSizeMake(theSize.width, theSize.height + 10);
}
#end
Sample output:
self.eskLabel.text = #"Hello Long² Long\u2082 World";
NSLog(#"CGSize: %#", NSStringFromCGSize(self.eskLabel.frame.size));
[self.eskLabel sizeToFit];
NSLog(#"CGSize: %#", NSStringFromCGSize(self.eskLabel.frame.size));
From the NSLog:
This GDB was configured as "x86_64-apple-darwin".sharedlibrary apply-load-rules all Attaching to process 864.
2012-01-06 23:34:21.949 Stackoverflow4[864:f803] CGSize: {85, 61}
2012-01-06 23:34:21.951 Stackoverflow4[864:f803] CGSize: {302, 44}
kill
quit
This should to the trick:
self.eskLabel.adjustsFontSizeToFitWidth = YES;
downPreView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
downPreView.tag = indexPath;
downPreView.frame = CGRectMake(75,28, 215,60);
downPreView.progress = 0.0;
[cell.contentView addSubview:downPreView];
Now how do I start animating downPreView with a tag of 2, etc?
Thanks!
You can grab a view with the viewWithTag method. Like [cell.contentView viewWithTag:1]
Update
First of all. You cant animate a progressView, you can show it, hide it, and you can edit it's progress level. If you want to edit it's progress, you have to set it like [downPreView setProgress:0.1]. If it's set to 1 the progrees indicator will be at the end.
However if you want just a spinner, you can use UIActivityIndicatorView.
If you cant get the cell, then you can use it's parent view. Like [self.view viewWithTag:1]
After this you have to cast UIProgressView on it to avoid warnings.
UIProgressView *progressView = (UIProgressView *)[self.view viewWithTag:1];
progressView.progress = 0.1;
For more info about UIActivityIndicatorView please see the documentation.
You cannot access an object by it's tag, you have to retain the object if you want access. Say if you have an array of progress views... you can enumerate it and ask
if (object.tag == tagWanted) {
doSomething;
}