How to horizontally align underscore without repeating it? - iphone

I'm using an underscore character below each form field and related label in my view to logically seperate them. I've added the underscore in a UILabel but I'm looking for a way to repeat the _ without having to manually type it many times in the text property of the label in IB. Any idea on how to do this ?
I tried checking/unchecking "adjust to fit" but it's not working.
Thx for helping
Stephane

This is vague question, but if you want a line, I think you should rather insert generic UIView with backgroundColor that imitates the line.
UIView *line = [[UIView alloc] initWithFrame:CGRectMake(linex, liney, thickness, length)];
line.backgroundColor = [UIColor blackColor];
[view addSubview:line];
[line release];
No matter how you'd look at this, this is much better solution than inserting undersocres.

Related

How can I make UI Text Field look like this in xcode 6?

This is the requirement
This is what I've been able to do
It seems the first pic is using only one field, but how can that be?
Simply add your UITextFields in a UIView with backgroundColor [UIColor whiteColor].
Also add a UIView with UIView with backgroundColor [UIColor grayColor] and height 1.0 px and width as your wish in between the two UITextFields.(This will act like the line seperator)
Now send the outer UIView to the function given below.
+(void)setCurvedEdges:(UIView*)view
{
[view.layer setCornerRadius:5.0];
[view setClipsToBounds:YES];
}
Hope this will help you to achieve your goal.

Get X and Y coordinates of a word in UITextView

I asked a developer (TwoLivesLeft, the creators of Codea) how they did syntax highlighting in their app. He replied :
#TD2 the Codea editor is implemented using a UITextView. The
highlighting is done by overlaying subviews in the appropriate
positions — usually UILabels. They are dequeued from a re-use pool,
similar to the way UITableViewCells work. During scrolling, the lines
requiring re-highlighting pull markers out of the pool and lines that
have moved off screen dump their markers back into the pool.
Can anyone explain how I would get the x and y of a certain word?
UITextView conforms to UITextInput, of which a detailed description can be found here.
Take a look at the required methods "textRangeFromPosition:toPosition:", "positionFromPosition:offset:", "positionFromPosition:inDirection:offset:", and some of the other geometric-based methods in the UITextInput Protocol. Those might provide the functionality you are looking for.
I have not actually tried to make sure these work the way you want them too, but that looks like its about what you need.
Let me know if you need any more help!
UPDATE:
Here is some sample code of how to do this. I ended up getting the "firstRectForRange:" method to work. This code basically takes the last three letters of the UITextView "textStuff" and highlights it green.
UITextView *textStuff = [[UITextView alloc] init];
textStuff.frame = CGRectMake(2.0, 200.0, 200.0, 40.0);
textStuff.text = #"how are you today?";
textStuff.textColor = [UIColor blackColor];
UITextPosition *Pos2 = [textStuff positionFromPosition: textStuff.endOfDocument offset: nil];
UITextPosition *Pos1 = [textStuff positionFromPosition: textStuff.endOfDocument offset: -3];
UITextRange *range = [textStuff textRangeFromPosition:Pos1 toPosition:Pos2];
CGRect result1 = [textStuff firstRectForRange:(UITextRange *)range ];
NSLog(#"%f, %f", result1.origin.x, result1.origin.y);
UIView *view1 = [[UIView alloc] initWithFrame:result1];
view1.backgroundColor = [UIColor colorWithRed:0.2f green:0.5f blue:0.2f alpha:0.4f];
[textStuff addSubview:view1];
Result of running this code:
#bddicken's https://stackoverflow.com/a/11487125/3549781 works like a charm. But the problem is, it doesn't work on iOS 7+ if the text contains a newline "\n". After a lot of searching I found a solution for that.
You have to ensure the layout of textView before calling firstRectForRange: by
[textView.layoutManager ensureLayoutForTextContainer:textView.textContainer];
Courtesy : UITextView firstRectForRange not working when there's new line characters in the mix
P.S : At first I added this as a comment to #bddicken's answer. As most people don't read comments I added this as an answer.

Best way to add a large chunk of text to a UIScrollView?

What is the best way to display a large chunk of text (taken from a .txt file in the app) in a UIScrollView so a user can scroll about it? Length of the text is variable.
On Interface Builder open the Attributes Inspector (if not already open - command-1) and uncheck "Editable".
Also notice there's a Scroll View section below. Make sure "Scrolling" is checked.
Hope this helps somebody (the post is a year old so I guess by now the one who posted it doesn't need this info).
I came here looking for an answer and found that all answers are bad - or flat out wrong.
The proper way to do this is using UITextView by itself. Since it is a descendant of UIScrollView, it has scrolling built-in and lots of features for adjusting formatting such as the insets etc.
If you intend to only show text, you need to explicitly disable editing. You do this by setting the "editable" property to false.
And if you want to disable the text selection mechanism, set the "selectable" property to false.
In newer versions of iOS, UITextView has added support for NSTextContainer which gives you even greater control over formatting.
One way I had working for me is to create UILabel, set text and then set content size of scrollview by it size.
Here is an example
Quote:
// alocate and initialize scroll
UIScrollView *myScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 460.0f)];
// alocate and initialize label
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 460.0f)];
// add long text to label
myLabel.text = #"Lorem ipsum... long text here";
// set line break mode to word wrap
myLabel.lineBreakMode = UILineBreakModeWordWrap;
// set number of lines to zero
myLabel.numberOfLines = 0;
// resize label
[myLabel sizeToFit];
// set scroll view size
myScroll.contentSize = CGSizeMake(myScroll.contentSize.width, myLabel.frame.size.height);
// add myLabel
[myScroll addSubview:myLabel];
// add scroll view to main view
[self.view addSubview:myScroll];
Usage of the UITextView into the UIScrollView. I could not recommend this because UITextView is the subclass of UIScrollView. Apple is also recommending the same.
Use UILabel in this case as a sub-view,
Put the UITextView into the UIScrollView.

Blurry UILabel as programmatic subview of UITableViewCell contentView

I am adding a UILabel instance as a subview of my custom UITableViewCell instance's contentView.
When I select the cell, the row is highlighted blue, except for the background of the label. The label text is sharp.
When I set the label and content view backgroundColor property to [UIColor clearColor], the label text becomes blurry.
How do I set the label background color to be clear, to allow the row highlight to come through, while still keeping the label text sharp?
One suggestion I read elsewhere was to round the label's frame values, but this did not have any effect.
CODE
Here is a snippet of my custom UITableViewCell subview's -setNeedsLayout method:
UILabel *_objectTitleLabel = [[UILabel alloc] initWithFrame:CGRectNull];
_objectTitleLabel.text = [self.awsObject cleanedKey];
_objectTitleLabel.font = [UIAppDelegate defaultObjectLabelFont];
_objectTitleLabel.highlightedTextColor = [UIColor clearColor]; //[UIAppDelegate defaultLabelShadowTint];
_objectTitleLabel.backgroundColor = [UIColor clearColor]; //[UIAppDelegate defaultWidgetBackgroundTint];
_objectTitleLabel.frame = CGRectMake(
kCellImageViewWidth + 2.0 * self.indentationWidth,
0.5 * (self.tableView.rowHeight - 1.5 * kCellLabelHeight) + kCellTitleYPositionNudge,
contentViewWidth,
kCellLabelHeight
);
_objectTitleLabel.frame = CGRectIntegral(_objectTitleLabel.frame);
_objectTitleLabel.tag = kObjectTableViewCellTitleSubviewType;
//NSLog(#"_objectTitleLabel: %#", NSStringFromCGRect(_objectTitleLabel.frame));
[self.contentView addSubview:_objectTitleLabel];
[_objectTitleLabel release], _objectTitleLabel = nil;
...
self.contentView.backgroundColor = [UIAppDelegate defaultWidgetBackgroundTint];
self.contentView.clearsContextBeforeDrawing = YES;
self.contentView.autoresizesSubviews = YES;
self.contentView.clipsToBounds = YES;
self.contentView.contentMode = UIViewContentModeRedraw;
The issue is sub-pixel rendering, which occurs when your origin (which is a float value) has a non-zero fractional component. Round to the nearest whole number and you should be fine.
In my case, having set shouldRasterize = YES on the CGLayer of the view containing the UILabel was the culprit. Removing that line made the text nice and crisp.
Ok found the problem, Make sure your parent view's coordinates are rounded as well.
I ran into this problem myself today, and read somewhere that non-integer values for the origin and size of the UILabel's frame can cause this (I know they're floats, but you know what I mean). There has got to be a more elegant solution, but this quick hack appears to have solved the problem for me:
self.valueLabel.frame = CGRectMake((int) frame.origin.x, (int) frame.origin.y, (int) frame.size.width, (int) frame.size.height);
If you find a better solution, please let me know, I'd love to replace this hack with something a bit more tasteful.
Another cause of garbled/blurry text is cell reuse. If you are de-queuing a reusable cell then it may redraw with different dimensions somewhere else and again be re-used when it gets to your cell with the garbled text.
To ensure the cells are unique be sure to allocate a new cell for the indicies where the text is garbled, and mark that UITableViewCell instance with a different reuse identifier. This is only practical of course if you're dealing with a very small number of cells and if you know exactly which cells are causing problems.
Setting shouldRasterize to YES may introduce blurriness. Set the rasterization scale and that should eliminate the blurriness. [self.layer setRasterizationScale:[[UIScreen mainScreen] scale]];
Sometimes the reason for the blurriness you have mentioned can be that labels's frame is beyond the cell frame.
Even if you see all of your text you have put inside the label on your cell, the actual label size can be bigger than the cell frame.
To check if that is the reason for the effect you see I would suggest to check/print all the data you have about labels size/location after it is instantiated and than check in the delegate method tableView:heightForRowAtIndexPath: that this fit into the cell height you are returning for the cell.
Hope it will help in your case.
Use round(); C functions are provided for a reason.
#define roundCGRectValues (frame) \
frame = CGRectMake(round(frame.origin.x),round(frame.origin.y),round(frame.size.width),round(frame.size.height));
All you need.
Does -setNeedsLayout get called even for dequeued reusable cells? If so, the cell will already have the label added to the content view, and you will draw it twice, making it blurry. You can inefficiently solve this by removing all of the content view's subviews before you add your subview:
for (UIView *subview in [[self contentView] subviews]) {
[subview removeFromSuperview];
}
A better solution would be to provide properties on your cell subclass to let you modify the content of a reused cell as-needed, rather than rebuilding its view hierarchy from scratch.

can i use highlighted the table view cell without default blue color in objective c?

I doing a project where i need to use highlighted table view color not default.
Of course you can.
There are two methods to achieve this:
Use UITableViewCell's selectedBackgroundView and selectedTextColor properties
Subclass UITableViewCell and implement the drawInRect and setSelected:animated: methods
The latter option gives you more flexibility and much better performance, but it might be slightly harder if you haven't used CoreGraphics before.
UPDATE In response to the OP's comment:
Here's how you can use the selectedBackgroundView property:
UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 60)];
[bgView setBackgroundColor:[UIColor redColor]];
[cell setSelectedBackgroundView:bgView];
[bgView release];
I haven't tried this myself, but it should work.
Try referring to this demo - maybe you could have a look at it and get some help.