UILabel in Custom Cell doesn't wrap - objective-c

I want to show a string with more words in a label.
Here is my code in the viewDidLoad:
startLabel.lineBreakMode = NSLineBreakByWordWrapping;
startLabel.numberOfLines = 0;
startLabel.text = string;
I also connected the label to the interface builder and synthesized it in my viewController.
But when the string is too long just the words who totally fit in the label get displayed and the rest gets cut off.
What am I doing wrong here?

Related

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;
}

UITextField -- Adding UIView for leftView - Can't get Focus

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

find the location {x,y} of text in uilabel

I have a string coming from server which I am displaying on UILabel multiligne. It is within that string, I am identifying some particular substring. I want to place a button on that substring(button will be a subview of UILabel). For this I require substring coordinates. I went through this but I am not able to understand it. Suppose my complete string is abc, 567-324-6554, New York. I want 567-324-6554 to be displayed on button for which I need its coordinates.
How can I use above link to find coordinates of substring?
Thanks,
UILabel doesn't have any methods for doing this. You can do it with UITextView, because it implements the UITextInput protocol. You will want to set the text view's editable property to NO.
Something like this untested code should work:
- (CGRect)rectInTextView:(UITextView *)textView stringRange:(CFRange)stringRange {
UITextPosition *begin = [textView positionFromPosition:textView.beginningOfDocument offset:stringRange.location];
UITextPosition *end = [textView positionFromPosition:begin offset:stringRange.length];
UITextRange *textRange = [textView textRangeFromPosition:begin toPosition:end];
return [textView firstRectForRange:textRange];
}
That should return a CGRect (in the text view's coordinate system) that covers the substring specified by stringRange. You can set the button's frame to this rectangle, if you make the button a subview of the text view.
If the substring spans multiple lines, the rectangle will only cover the first line.

How do you change the coordinates of a UIImageView?

I have made a UIImageView show an image of my choosing in Interface Builder.
In code, I want to find a way to change the x and y coordinates of this image view, during the program.
Is it possible to do that?
I've already made a variable
IBOutlet UIImageView *Alan;
but am not sure what to do with it.
Change view's frame property:
Alan.frame = (CGRect){{newX, newY}, Alan.frame.size};
// same as
// Alan.frame = CGRectMake(newX, newY, Alan.frame.size.width, Alan.frame.size.height);
or if you want to set position of view center:
Alan.center = CGPointMake(newX, newY);
P.S. Note also that per objective-c naming guidelines instance variable names should start with lowercase.

highlight and make note in UIwebview

I am deveoping an ebook app for ipad/iphone which loads epub file. I would like to know how to highlight a text with a background color and make notes similar to iBook or Kindle in UIWebView and NOT UIView. I am able to drag select the text by long tapping. But I can only see "copy" option. I would like to add highlight and add note to that menu. How to hightlight the text with a background color?... My document is loaded in UIWebView.
Thank you.
What you need to do is add the note and highlight options to your shared menu. In your viewcontroller viewdidload (or anytime after your view has been added). The following adds two menu items with two selector methods to your menu.
UIMenuItem *customMenuItem1 = [[UIMenuItem alloc] initWithTitle:#"Highlight" action:#selector(doHighlight:)];
UIMenuItem *customMenuItem2 = [[UIMenuItem alloc] initWithTitle:#"Note" action:#selector(doNote:)];
//[myArray addObject:customMenuItem2];
NSMutableArray *myArray = [[NSMutableArray alloc]initWithObjects:customMenuItem1,customMenuItem2, nil ] ;
[UIMenuController sharedMenuController].menuItems = myArray;
You may also want to use a bit of javascript to determine the rect that holds your selection, then overlay a translucent uiview over the text.
So, within your doNote method, load up javascript and run it, see the below javascript for an example of what you may want to do.
NSString *rectString = [webView stringByEvaluatingJavaScriptFromString:rectStringText];
The following will get a rect around the text, though it will inherently be larger than your text: if you've highlighted at char 10 on line 2 through char 50 on line 5, it will return a rect that highlights char 0 line 2 through endline line 5.
function selectEl(x,y){
document.designMode = "on";
var el = document.elementFromPoint(x, y);
var rect = el.getBoundingClientRect();
var vx = rect.left;
var width = rect.right - vx;
var height = rect.bottom - rect.top;
var vy = rect.top;
var range = document.createRange();
range.selectNodeContents(el);
var sel = document.getSelection();
sel.removeAllRanges();
sel.addRange(range);
document.designMode = "off";
rectString = '{{'+vx+','+vy+'},{'+width+','+height+'}}';
}
So, now that you have a rectangle string, you can create a CRect from the string returned and uiview to overlay within your webview. [Note, you will have to figure out how to deal with zooming and saving this data for your next load]