How to override UIWebView links request action with your own custom method

How to override UIWebView links request action with your own custom method

Ivan Kalaica

If you have a Web View in your view you can call a custom action when the user taps on a link in that UIWebView instance.

Here’s how…

Set the delegate of that UIWebView class instance on your instance of UIViewController class. Now just copy & paste the following code into your class.  This code implements the UIWebView instance and adds a call to the custom action (method).

- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
if(navigationType == UIWebViewNavigationTypeLinkClicked) {
if (overrideLinksSwitch.on == TRUE) {
[self myMethodAction];
[myWebView stopLoading];
return YES;
}
else {
<>return YES;
}
}
return YES;
}

You can download an example app here.

http://surgeworksmobile.com/iphone/how-to-override-uiwebview-links-request-action-with-your-own-custom-method
Posted by 오늘마감

댓글을 달아 주세요

Creating a custom Table View Cell programmatically

Ohhk, quite a lot of things done but this one is something which I have used most often. Custom cells can sometimes greatly push ahead the usability of your application. In this post I am going to create a test project which will demonstrate how to create custom cells and use them appropriately to provide better usability. The application will finally look like this:


So to start open xcode and create a new project, chose the template as “Navigation Based” and name it as “CustomCellTestProject”. What template you chose does not matter, refer my previous posts to find how you can start working on any template.
First thing we will do is create a customCell. Right click on Classes and add a new UITableViewCell subclass. Name it as “CustomCell”. Now open CustomCell.h and add the following code:
#import

@interface CustomCell : UITableViewCell {

UILabel *primaryLabel;

UILabel *secondaryLabel;

UIImageView *myImageView;

}

@property(nonatomic,retain)UILabel *primaryLabel;

@property(nonatomic,retain)UILabel *secondaryLabel;

@property(nonatomic,retain)UIImageView *myImageView;

@end


We create the three elements and added them to the contentView of our cell.
synthesize all the three elements in CustomCell.m as we are going to access these elements from other classes.

@synthesize primaryLabel,secondaryLabel,myImageView;


Here we have simply added a primary label to display the primary text, a secondary label and an imageView. These elements will be created and added into the content view of our custom cell. So open CustomCell.m and add the following code

- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier {

if (self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier]) {

// Initialization code

primaryLabel = [[UILabelalloc]init];

primaryLabel.textAlignment = UITextAlignmentLeft;

primaryLabel.font = [UIFontsystemFontOfSize:14];

secondaryLabel = [[UILabelalloc]init];

secondaryLabel.textAlignment = UITextAlignmentLeft;

secondaryLabel.font = [UIFontsystemFontOfSize:8];

myImageView = [[UIImageView alloc]init];

[self.contentView addSubview:primaryLabel];

[self.contentView addSubview:secondaryLabel];

[self.contentView addSubview:myImageView];

}

returnself;

}



Now, we have already added the UI elements into our cell but you must have noticed, we have not yet defined how these elements will appear inside cell. Go ahead and add the following code for that:

- (void)layoutSubviews {

[superlayoutSubviews];

CGRect contentRect = self.contentView.bounds;

CGFloat boundsX = contentRect.origin.x;

CGRect frame;

frame= CGRectMake(boundsX+10 ,05050);

myImageView.frame = frame;

frame= CGRectMake(boundsX+70 ,520025);

primaryLabel.frame = frame;

frame= CGRectMake(boundsX+70 ,3010015);

secondaryLabel.frame = frame;

}


You can do anything in this method to define the lay out of cell. I have simply assigned frames to all the elements.
You can also find a method in CustomCell.m

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {

[super setSelected:selected animated:animated];

// Configure the view for the selected state

}

This method can be used to define how your cell should react when it is selected. You can describe what should be the highlight color or may be you want to flash one of the labels anything of your choice. I am leaving this method as it is.
We are done with creating our custom cell and now we have to use it. Open RootViewController.m and import CustomCell.h at the top.


#import “CustomCell.h”

I am going to create 5 cells here, you can just use your own logic of specifying number of cells and data. So change the following method to look like this:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

return 5;

}

Now we are going to use our custom cell. If you look at the cellForRow method, you will find that a UItableViewCell has been created and re used. Now all we have to do is to replace this cell with our new cell. Change the code inside this method to look like this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @”Cell”;

CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {

cell = [[[CustomCellallocinitWithFrame:CGRectZeroreuseIdentifier:CellIdentifier] autorelease];

}

// Set up the cell…

switch (indexPath.row) {

case 0:

cell.primaryLabel.text = @"Meeting on iPhone Development";

cell.secondaryLabel.text = @"Sat 10:30";

cell.myImageView.image = [UIImage imageNamed:@"meeting_color.png"];

break;

case 1:

cell.primaryLabel.text = @"Call With Client";

cell.secondaryLabel.text = @"Planned";

cell.myImageView.image = [UIImage imageNamed:@"call_color.png"];

break;

case 2:

cell.primaryLabel.text = @"Appointment with Joey";

cell.secondaryLabel.text = @"2 Hours";

cell.myImageView.image = [UIImage imageNamed:@"calendar_color.png"];

break;

case 3:

cell.primaryLabel.text = @"Call With Client";

cell.secondaryLabel.text = @"Planned";

cell.myImageView.image = [UIImage imageNamed:@"call_color.png"];

break;

case 4:

cell.primaryLabel.text = @"Appointment with Joey";

cell.secondaryLabel.text = @"2 Hours";

cell.myImageView.image = [UIImage imageNamed:@"calendar_color.png"];

break;

default:

break;

}

return cell;

}


I have added some dummy data. You can use your data source to provide data for primaryLabel and secondaryLabel. Please note that I have used three images here. You can use any image of your choice. All you need to to do is copy the images and paste it into your project root folder (which in this case is CustomCellTestProject folder). After pasting the files, in xcode right click on Resources (or any group), select Add >> ExistingFiles and then select all the images you want to add in you project. Once added you can simply use them by their names.
Thats it, go ahead and run the project. You will find something wrong when you compare you simulator screen with mine. If you noticed in the CustomCell.m, I have given some frames to the UI elements. You need to make sure that the height of your cell large enough to accomodate all the elements. So add this following code and you fixed the issue:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

return50;

}


this is not my post the original post is here


http://blog.webscale.co.in/?p=284

Posted by 오늘마감

댓글을 달아 주세요

How to override UIWebView links request action with your own custom method

How to override UIWebView links request action with your own custom method

If you have a Web View in your view you can call a custom action when the user taps on a link in that UIWebView instance.

Here’s how…

Set the delegate of that UIWebView class instance on your instance of UIViewController class. Now just copy & paste the following code into your class.  This code implements the UIWebView instance and adds a call to the custom action (method).

- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
if(navigationType == UIWebViewNavigationTypeLinkClicked) {
if (overrideLinksSwitch.on == TRUE) {
[self myMethodAction];
[myWebView stopLoading];
return YES;
}
else {
<>return YES;
}
}
return YES;
}

You can download an example app here.

http://surgeworksmobile.com/iphone/how-to-override-uiwebview-links-request-action-with-your-own-custom-method
Posted by 오늘마감

댓글을 달아 주세요

[Tip] Custom font 쓰는 법

 

 

꼬맹이 공부시키려고 한자 학습 게임을 간단하게 만들려고  윈도 트루타입 폰트를 쓰려고 했더니,

 

기본적으로, UIFont 객체가 Glypse data를 갖고 있지 않아서,

 

임의로 쓸수가 없게 되어있더만요.

 

이리 저리 좀 뒤져보고 했는데, 아마 시한부일거라고는 생각되지만, 2.2.1, 3.0 Beta 2까지 잘 돕니다.

 

전체 소스는 아직 좀 정리를 해야되지만, 핵심 코드를 공개합니다.

 

* test.ttf라는 폰트를 사용할 때.

1. 폰트파일을 xcode 프로젝트로 긁어 넣는다.

2. 아래와 같은 방식으로 메모리 로딩후 사용한다.

NSString *fontPath = [[NSBundle mainBundle] pathForResource: ofType:];
CGDataProviderRef fontDataProvider = CGDataProviderCreateWithFilename([fontPath UTF8String]);
CGFontRef _cgFont = CGFontCreateWithDataProvider(fontDataProvider);
CGDataProviderRelease(fontDataProvider);
20);

CGAffineTransform transform = CGAffineTransformMake(0.0, 1.0, 0.0);
CGContextSetTextMatrix(context, transform);

NSString *str = ;
CGGlyph _glyphs[[str length]];
unichar _chars[[str length]];
for(i = CGFontGetGlyphsForUnichars(_cgFont, _chars, _glyphs, [str length]);
CGContextShowGlyphsAtPoint(context, 20, _glyphs, [str length]);
CGFontRelease(_cgFont);


위에서 CGFontGetGlyphsForUnichars는 헤더파일에도 선언되지않은 함수라서 warning이 나올 수 있다.

따라서, 아래와 같이 선언을 해두면 warning이 나오지 않는다.

size_t);

문자색을 지정할 때는 위의 예제와 같이 CGContextSetFillColorWithColor로 선언하며, 이 때 사용이 간편한 UIColor를 사용하여 CGColorRef를 사용하는 것이 좋다.

저는 일단, FontManager란걸 만들어놓고, AppDelegate에서 한번에 로딩해서 쓰는 방식으로 해서,

 

UIView, UITextView, UITableViewCell등에서 사용해본 결과 만족할 만 했습니다.

 

다만, 저, CGFontGetGlyphsForUnichars란 함수가 언제까지 남아있을 지가 문제죠.

 

지금 만들고 있는 어플중 스크린샷 하나씩 해봤습니다.

 

많은 참조 되시길.

 

아, 여긴 아직 정회원이 아니라 쓰는 데 제약이 많아서.. 혹시 물어보실 분은 제 블로그에 써주세요.

 

http://starlab.tistory.com

 

 


출처 : http://blog.naver.com/PostView.nhn?blogId=gonagi&logNo=150045840793
Posted by 오늘마감

댓글을 달아 주세요