I am using the following sample code to add a link to a UITextView in a UITableViewCell, but it is not creating a link. I have done this before without issues... Has something changed with the recent iOS upgrade?
UITextView *myView = [[UITextView alloc] initWithFrame: frame];
myView.text = #"this is http://google.com link";
myView.editable = NO;
myView.dataDetectorTypes = UIDataDetectorTypeLink;
//cell is the TableView's cell
[cell.contentView addSubview:myView];
[myView release];
tried simply putting this code in a subview on a normal view and works great for me (iOS 4.2), maybe the bug is somewhere else (or maybe frame is empty)
//EDIT: and tried for content view in TableViewCell, works also great
Just to have the question (maybe) solved: you have to reset myView.dataDetectorTypes = UIDataDetectorTypeLink; every time you reload the table.
Related
I am new to Xcode, and I am currently building my first app. I've searched long and hard to try and find the proper tutorial but I can't find one. I am looking for a way to be able to insert a custom background in my UITableViewController. I can change the color, but that is it. What I am looking to do is set my PNG image behind the static cells I've created, and drop the opacity on those cells so the custom image comes through. Can someone please give me a way to do this either through the IB (storyboard) or through the coding. Much appreciated!! Thanks in advance!
I think the easier and correct way is to:
[tableView setBackgroundView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"yourimage.png"]]];
You can include this code in viewDidLoad to make it work.
for cell's background :, do this in willDisplayCell:atIndexPath: method
UIImageView *cellImageView = [[UIImageView alloc] initWithImage:[UIImageimageNamed:#"image.png"]]];
CGRect Frame = cell.backgroundView.bounds;
cellImageView.frame = newFrame;
[cell.backgroundView addSubview:cellImageView];
for tableview background , do this in viewDidLoad: method
self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"BackgroundPattern.png"]];
UIImageView *ivwCellBackground=[[UIImageView alloc]init];
[ivwCellBackground setImage:[UIImage imageNamed:CELL_BACKGROUNGIMAGE]];
cell.backgroundColor=[UIColor clearColor];
[cell setBackgroundView:ivwCellBackground];
[ivwCellBackground release];
ivwCellBackground=nil;
Am working for am message based iPhone app. In my application i have loaded the message content in UITextView and added an UIImage on UITextView.
Now i want to select all UITextView content by holding UITextView and show the Copy option to the user. Currently when the user hold UITextView some of the content only selecting.
Any one please help me to do this? Thanks in advance.
EDIT:
In UITableView CellForRowAtIndexPath delegate
customMessageTextView = [[MessageTextView alloc] initWithFrame:CGRectZero];
customMessageTextView.tag = 100;
UIFont *font = [UIFont fontWithName:#"Helvetica" size:15];
customMessageTextView.font = font;
customMessageTextView.scrollEnabled = NO;
customMessageTextView.delegate = self;
customMessageTextView.dataDetectorTypes = UIDataDetectorTypeLink;
[cell.contentView addSubview:customMessageTextView];
[customMessageTextView sizeToFit];
for (UIGestureRecognizer *recognizer in customMessageTextView.gestureRecognizers)
{
if ([recognizer isKindOfClass:[UILongPressGestureRecognizer class]])
{
recognizer.enabled = NO;
}
}
UILongPressGestureRecognizer *myLongPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(selectAllTextFromCustomMessageTextView)];
[customMessageTextView addGestureRecognizer:myLongPressRecognizer];
[myLongPressRecognizer release];
UILongPressGestureRecognizer action:
-(void) selectAllTextFromCustomMessageTextView
{
NSLog(#"Select All Text Messages");
customMessageTextView.selectedRange = NSMakeRange(0, customMessageTextView.text.length);
}
If I understand you correctly you want to disable the standard behaviour when holding in a UITextView (i.e. the magnifying glass, etc.). Perhaps you have even disabled the editing option. If so you should just add an UILongPressGestureRecognizer to your UITextView. You might have to disable the UILongPressGestureRecognizer which is built into UITextView by default. You can find a way to do that here.
Then in your UILongPressGestureRecognizer action method you simply select all the text in the view:
[textView selectAll:self];
Notice that this will bring up the Copy/Cut/Paste menu. However, if your text view does in fact have user editing disabled the menu will only contain Copy.
Hey everyone, this seems like it should be a simple one; I really hope it is. As always, thanks in advance!
All I'm trying to do is add a background image to a grouped style tableview. I'm using the following method in viewDidLoad:
self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"background.png"]];
Unfortunately, background.png seems to be drawn not only behind the cells, but also on the backgrounds of the cells and the headers as follows:
alt text http://www.freeimagehosting.net/uploads/194449ffc1.png
I know there are a few questions on this site addressing similar issues, but I'm afraid I couldn't get anything to work. How can I set the background of UITableView (the tableview style is "Grouped") to use an image? led me to add
self.tableView.opaque = NO;
self.tableView.backgroundView = nil;
to viewDidLoad and to add
cell.backgroundView = nil;
cell.opaque = NO;
in configuring my cells. Needless to say, it didn't work. Adding
cell.backgroundColor = [UIColor clearColor];
didn't work either. And sadly, that was my last idea. I'd really like to be able to do this without adding the background in interface builder, if possible. I rather like doing as much as I can programmatically. I hope asking that isn't too greedy. Thanks again!
*
*
*
EDIT:
*
*
*
I came across another approach to accomplishing this, but ran into another problem. I added the following code to viewDidLoad:
UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
bgView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"background.png"]];
[self.tableView.window addSubview:bgView];
[self.tableView addSubview:bgView];
[self.tableView sendSubviewToBack:bgView];
It looked promising, and it likely is, but unfortunately in gave me the following:
alt text http://www.freeimagehosting.net/uploads/c02dd68e20.png
It looks as though it either wasn't sent to the back and the headers somehow show through or the cells went to the back with it. I'm not at all sure. Any advice here would be appreciated as well.
Here's where I found the code for the edit:
Add UIView behind UITableView in UITableViewController code
I went through a similar path to what you describe here. I finally found an answer that worked for me on the following post:
How to set the background of a UITableView to an image?
It was two lines of code posted by user rkb. Here's the code:
self.tableView.backgroundColor = [UIColor clearColor];
self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithData:imageData]];
Another solution if nothing else here is working out. In ViewDidLoad:
UIImage *background = [UIImage imageNamed:#"MyBackground.png"];
self.tableView.backgroundView = [[UIImageView alloc] initWithImage:background];
This will place your static (doesn't stretch or move) background behind all of your grouped tableview elements, such as the headers and cells.
Using the previous responses(particularly from dana_a) this is how I got the same thing to work
self.tableView.backgroundColor = [UIColor clearColor];
self.parentViewController.view.backgroundcolor = [UIColor colorWithPatternImage:[UIImge imageNamed:#"bgtest2.png"]];
This is in my viewWillAppear: method in for my case a detail view controller with a grouped tableview.
I placed this code in my ViewDidLoad method and it worked beautifully.
self.termsTableView.backgroundColor = [UIColor clearColor];
self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"metal4.png"]];
I still haven't figured out why the above doesn't work or how to modify it so that it does, but by adding the subView to the window in the App Delegate instead of the tableView here I was able to get it to work.
Instead of just getting it to function and moving on I'd like to actually learn, so I'm still curious as to where I went wrong above. As such, I'll leave this as unanswered for a time and if you can tell me my error(s) I'll give you the up vote and the green check mark. Thanks!
This slider is possessed:
Slider http://gorgando.com/possessedSlider.jpg
Whenever I enter "editing mode" for the tableview or leave "editing mode" a duplicate thumb image will appear where the thumb is. It is now behaving consistently in this way. If I switch back and forth from editing mode I'll get lots of thumb images like the screenshot shows.
This is the code that I use to create the slider (in the cellForRowAtIndexPath method):
CGRect frame = CGRectMake(20, 42.0, 280.0, 22);
self.slider = [[UISlider alloc] initWithFrame:frame];
[self.slider addTarget:self action:#selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
self.slider.continuous = YES;
self.slider.minimumValue = 0.0;
self.slider.maximumValue = 5.0;
self.slider.value = 0.0;
[[interestCell contentView] addSubview:self.slider];
Appreciate any thoughts you might have!
[I just edited/updated this to reflect the questions/suggestions I received but it's still having problems].
Try adding it to the cell's contentView and not the cell itself.
Finally figured it out!
I had to move my code to the viewDidLoad method except for setting the slider to the contentView, which happens in the cellForRowAtIndexPath method still. What a strange problem that was. Thanks to those who helped and commented!
It looks like a problem which could have simple solution, but I haven't found anything what could lead the way to it. I'm using UIWebView inside of UIScrollView and tapping on statusBar (to scroll content to top) is not working.
I've made simple test application to see if it's really UIWebViews fault. And it really is.
// scrolls to top on status bar tap
UIScrollView *sv = [[UIScrollView alloc] init];
sv.frame = CGRectMake(0, 0, 320, 480);
sv.contentSize = CGSizeMake(320, 1200);
[self.view addSubview:sv];
// doesn't scroll
UIScrollView *sv = [[UIScrollView alloc] init];
sv.frame = CGRectMake(0, 0, 320, 480);
sv.contentSize = CGSizeMake(320, 1200);
UIWebView *wv = [[UIWebView alloc] init];
wv.frame = CGRectMake(0, 0, 320, 100);
[sv addSubview:wv];
[self.view addSubview:sv];
So, I think maybe there's something I could disable to make UIWebView not to mess with scrollToTop? Or some kind of workaround also would be nice.
Any ideas?
I ran into this last night until I finally found the answer buried in a comment to a comment. The idea of that original post is that when you add the UIWebView to your UIScrollingView, you use the following:
- (void) ensureScrollsToTop: (UIView*) ensureView {
((UIScrollView *)[[webView subviews] objectAtIndex:0]).scrollsToTop = NO;
}
This seemed fishy to me since the first sub-view of a UIWebView claims to be a UIScroller which is not a subclass of UIScrollView. However, since UIScroller supports the scrollsToTop property, the cast just gives us a way past the compiler warning:
Class List:
Class = UIScroller
Class = UIView
Class = UIResponder
Class = NSObject
Supported Methods:
...
Method _scrollToTop
Method setScrollsToTop:
Method scrollsToTop
...
Supported Properties:
Property scrollsToTop
EDIT:
Just another quick note about where this actually needs to occur: in the webViewDidFinishLoad callback. Calling it on UIWebView construction isn't good enough because at that time the UIWebView hasn't created it's child views yet, which are the ones causing the problem:
- (void)webViewDidFinishLoad:(UIWebView *) wv {
[self ensureScrollsToTop: wv];
}
EDIT #2:
now, in iOS 5, as noted in iPhone OS: Tap status bar to scroll to top doesn't work after remove/add back use UIWebView's new #property scrollView, making ensureScrollsToTop implementation unnecessary for projects that aren't using deployment targets lower than iOS 5.0 .
In iPhone OS, if there is more than one UIScrollView (or its subclass, for example UITableView, UIWebView) in the current viewController, the system doesn't know which UIScrollView should be scrolled to the top.
Quick fix: for all the UIScrollViews that you don't want to support scrollsToTop, just set the scrollsToTop as NO, then every thing works perfect.
self.scrollView1.scrollsToTop = NO;
self.scrollView2.scrollsToTop = NO;
self.scrollView1.scrollsToTop = YES; // by default scrollsToTop is set as YES, this line is not necessary
Webviews by default will have YES for scrollsToTop-value. I've written a simple class for this specific issue. In my app we're having multiple webviews and scrollviews. This makes it all much easier.
https://gist.github.com/hfossli/6776203
It basically sets scrollsToTop to NO on all other scrollViews than the one you are specifying.
I want to add my case, I add an UIWebView on an UIScrollView, as h4xxr said:
If there is more than one scrolling view a scrollViewDidScrollToTop message is ignored
So, I get a simply way to make it work on webView: just set the scrollView·s scrollsToTop property false.
And when tap the status bar, it won`t got intercepted by the scrollView, and the webView scrolls to the top!
UIScrollView *scrollView = [[UIScrollView alloc] init];
scrollView.frame = self.view.bounds;
scrollView.scrollsToTop = false; //igore scrollView`s scrollsToTop
[self.view addSubview:scrollView];
UIWebView *webView = [[UIWebView alloc] init];
webView.frame = scrollView.bounds;
[scrollView addSubview:webView];